我正在执行简单的wordpress select查询,希望使用ajax jquery执行此操作,并且不希望在按下搜索按钮并在搜索文本框中写入文本时页面刷新。我一按搜索按钮就得到零。。请在这方面提供帮助,因为如果我使用普通查询,它会使用表单提交给出结果,无论我做什么,它都会给出零。谢谢
jQuery(document).ready(function() {
jQuery("#Submityourskill").click(function(e){
event.preventDefault();
jQuery.post(yes.ajaxurl,{action : \'doit\'},
function( response) {//start of funciton
// alert(response);
//jQuery("#searchtextbox").val(response);
jQuery("#result").append(response);
jQuery("#textarea").html(response);
return false;
} //end of function
);
}); // click button function finishing here
}); //end of main loop
HTML:
<form action="" method="post">
<div><input maxlength="200" name="secretcode" size="200" type="text" value="" placeholder="Type Here !" />
<input id="Submityourskill" name="Submit" type="submit" value="Search Record" /></div>
</form>
<div id="result"></div>
PHP:
function doit() {
if(isset($_POST[\'secretcode\'])!= \'\'){
if($_POST[\'Submit\']) {
$secretcode=$_POST[\'secretcode\'];
global $wpdb;
$sql = "SELECT * FROM wp_store_locator WHERE sl_description=\'$secretcode\'";
$results = $wpdb->get_results($sql) or die(mysql_error());
foreach( $results as $result ) {
echo $result->sl_description;
}
exit();
}
}
}
add_action( \'wp_ajax_nopriv_doit\', \'doit\');
add_action( \'wp_ajax_doit\', \'doit\' );
function add_myjavascript(){
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\' ) ) );
}
add_action( \'init\', \'add_myjavascript\' );
谢谢大家。。。