質問
ワードプレス wpdb を実行しようとしている -> get_results 非選択クエリとテキスト ボックスから検索条件を取得したいです。それを行うしたいフォームと ajax でページを更新せず。検索に入力するテキスト ボックスの値と何に関係なくテキスト ボックスは常に空です。私のクエリを変更し、すべての結果を返す select クエリのしくみと、結果が表示されます。それでなぜ私は私の検索クエリでテキスト ボックスの値を取得することができるではないかこれであなたの助けが必要です。以下はすべて、コード… おかげで
PHP Main 関数 ($_POST [‘secretcode’] の値は決してそれを得る戻る wpdb 選択クエリには常に空… すべての結果を示すクエリは機能します)
function wp_hello() { $secretcode=$_POST['secretcode']; //main logic global $wpdb; //echo jsonencode($secretcode); $sql = "SELECT * FROM wp_store_locator WHERE sl_description = '$secretcode' "; $results = $wpdb->get_results($sql) or die(mysql_error()); $takeit = array(); foreach( $results as $result ) { $takeit[]= $result->sl_description; } echo json_encode($takeit); die(); }
html
<form action="" method="get" id="myform"> <input type="text" name="secretcode" style="height:30px;" id="secret"/> <button id="Submit" type="submit">SUBMIT </button> </form> <div id="result"></div>
JQuery
jQuery(document).ready(function() { jQuery("#myform").submit(function(e){//form is intercepted e.preventDefault(); var sentdata = jQuery(this).serialize(); var secretkey = jQuery('input[name="secretcode"]').val(); alert(sentdata); jQuery.post(yes.ajaxurl,{action : 'wp_hello'}, function( response) {//start of funciton var rez = JSON.parse(response); jQuery("#result").append(rez); return false; } //end of function ); }); // submit end here });
他エンキューと php のコードをローカライズ
add_action( 'init', 'add_myjavascript' ); function add_myjavascript(){ add_action( 'wp_ajax_wp_hello', 'wp_hello' ); add_action( 'wp_ajax_nopriv_wp_hello', 'wp_hello'); // register & enqueue a javascript file called globals.js wp_register_script( 'globals', get_stylesheet_directory_uri() . "/js/ajax-implementationn.js", array( 'jquery' ) ); wp_enqueue_script( 'globals' ); // use wp_localize_script to pass PHP variables into javascript wp_localize_script( 'globals', 'yes', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); }
おかげでみんな !
答え
secretCode
param を送信していない、
action
と呼ばれるパラメーターを送信するだけです
。
jQuery(function ($) { $("#myform").submit(function (e) { //form is intercepted e.preventDefault(); //serialize the form which contains secretcode var sentdata = $(this).serializeArray(); //Add the additional param to the data sentdata.push({ name: 'action', value: 'wp_hello' }) //set sentdata as the data to be sent $.post(yes.ajaxurl, sentdata, function (rez) { //start of funciton $("#result").append(rez); return false; } //end of function , 'json'); //set the dataType as json, so you will get the parsed data in the callback }); // submit end here });
http://stackoverflow.com/questions/28666315/ajax-form-success-data-is-always-empty