I am trying to run a wordpress wpdb->get_results select query and I want to get the search criteria from a textbox. I want to do it form and with ajax without refreshing the page. No matter what I do the textbox value that I enter into search textbox is always empty. if I modify my query and return all the results the select query works and the result is shown. So your help is required in this as to why I am not able to get textbox value in my search query. Below is all the code ... thanks

PHP Main Function (the $_POST['secretcode'] value never get back to the wpdb select query it is always empty ... query works if I show all results)

function wp_hello()


//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);



<form action="" method="get" id="myform">
<input type="text" name="secretcode" style="height:30px;" id="secret"/>
<button id="Submit" type="submit">SUBMIT </button>
<div id="result"></div>


jQuery(document).ready(function() {  

  jQuery("#myform").submit(function(e){//form is intercepted
        var sentdata = jQuery(this).serialize();
var secretkey = jQuery('input[name="secretcode"]').val();

jQuery.post(yes.ajaxurl,{action : 'wp_hello'},
            function( response) {//start of funciton
        var rez = JSON.parse(response);

                return false;
            } //end of function
}); // submit end here


Other enqueue and localize code of 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' ) ) );


Thanks everybody !

  • you're not doing anything with sentdata or secretkey, all you pass with your jQuery.post is action. – Milo Feb 23 '15 at 3:13
  • thanks yah even if I pass them with the action function even then the $_POST value is not picked up by the wpdb query ... please guide me what should I do ... I want the search textbox to give criteria to my select query – Zeeshan Feb 23 '15 at 3:25
  • well first of all, what does your working javascript look like then? try to get the data to your function successfully first before trying to use it in a query. don't try to do a bunch of things you don't understand all at once, take smaller steps and build on verifiably working code. – Milo Feb 23 '15 at 3:32
  • thanks Milo here is the working code and you rightly highlighted that I was not using the sent data or secret key ... I'll post the working code in my next comment but thanks you pointed me in the right direction – Zeeshan Feb 23 '15 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.