1

I am trying to count data from custom wordpress database. Mysql Query works fine as I have already tested with default values directly but when sending the data from form to ajax its not sending data in php for the query.

Here is my code in function.php:

function searchdata() {
     $postcode = $_POST['$postcode'];
            global $wpdb;
            $resultdyn =  $wpdb->get_var( "SELECT count(*) FROM wp_cdata WHERE code = '$postcode'" );
            echo "<p>Result: {$resultdyn}</p>";
            echo "Search Results for: $postcode";
            exit();
}
add_action( 'wp_ajax_nopriv_searchdata', 'searchdata');
add_action( 'wp_ajax_searchdata', 'searchdata' );

function add_myjavascript(){  
    wp_register_script( 'globals', get_stylesheet_directory_uri() . "/js/search.js", array( 'jquery' ) ); 
    wp_enqueue_script( 'globals' );
    wp_localize_script( 'globals', 'yes', array( 'ajaxurl' => admin_url( '../../wp-admin/admin-ajax.php' ) ) );
}  
add_action( 'init', 'add_myjavascript' );

Here is my jquery search.js code:

jQuery(document).ready(function() {      
    jQuery("#mysearchform").submit(function(e){
         e.preventDefault();      
                 var sentdata = jQuery(this).serialize();
  // var dataString = 'postcode='+ postcode;
             $('div.cart').append('<span class="loading">LOADING</span>');
        jQuery.post(yes.ajaxurl, {action : 'searchdata'}, {postcode:$("#postcode").val()},
            function( response) {
                  jQuery("#searchresult").html(response);
                $('span.loading').remove();
                return false;
            } 
        );
    }); 
}); 

My search form code:

<form action="" id="mysearchform" method="post">
<div><input name="postcode" id="postcode" size="200" type="text" value="" placeholder="Postcode here *" />
<input id="myformsubmit" name="Submit" type="submit" value="Search" /></div>
</form>
<div id="searchresult"></div>

I don't know where i am making the mistake anybody can help ?

I am unable to get the postcode values from my search form to POST in function.

Thanks in advance.

  • Have you checked the browser's developers console for JavaScript errors? Second thing I would suggest - in the same developers tools check the network tab for the ajax post to verify the data being sent. – Picard May 10 '17 at 19:16
  • Hi Picard thanks for your reply. I have check both but didn't found any error ??? what else it could be can't figure out if something is wrong with my code – Man in black May 10 '17 at 19:53
  • I think I've found the problem - instead of $postcode = $_POST['$postcode']; it should be $postcode = $_POST['postcode']; – Picard May 10 '17 at 21:05
  • Hello Picard thanks for reply back. Yes that was a mistake sorry for that I fixed that but yet no results. Don't know whats wrong confused – Man in black May 11 '17 at 10:25
  • If there are no Javascript errors I would check then the POST request if it is fired and if the transmitted data are correct - see. – Picard May 11 '17 at 11:07
1

I have probably found the problem (after our little discussion).

For me it looks like the $postcode isn't send. Check the "Headers" tab from you screenshot - there would be info about the POST variables that were or weren't sent. If the $postcode isn't send, you get from the query probably the codes that are empty:

SELECT count(*) FROM wp_cdata WHERE code = '';

but that isn't certain because code is a MySQL reserved word which should be escaped by backticks, so you could try to change this but I think it won't help.

The main problem I think is the jQuery.post format which I believe should be like (notice the curly braces containing the arguments):

jQuery.post(
   yes.ajaxurl, 
   {action: 'searchdata', postcode: $("#postcode").val()},
   function(response) {
       jQuery("#searchresult").html(response);
       $('span.loading').remove();
       return false;
   } 
);

You can check the possible post call formats.

  • Hello Picard thanks for your support. After i replace the jquery codes still didn't work. So, I checked ajax query from networks and there i found only action: searchdata is there but postcode=xyz is not there. So, i tried with <pre>{action: 'searchdata', postcode:'123'},</pre> and checked again but no ajax query has been send for postcode. Only action form data is action: searchdata. – Man in black May 11 '17 at 20:59
  • Have you flushed the browser's cache after changing the script? If so and still doesn't work, change the arguments fragment to send some static data and check if now the postcode is transmitted over the POST - I mean do: {action: 'searchdata', postcode: 'some-code'} – Picard May 11 '17 at 21:05
  • Yes always view with flushed browser cache and tried by {action: 'searchdata', postcode: 'some-code'} but still the postcode is not in form data in networks. form only sending first post variable not the second one – Man in black May 11 '17 at 21:19
  • Seems impossible, I have no idea: Please look at this fiddle - the same code both parameters are set with the POST request. And here the screenshot – Picard May 11 '17 at 21:40
  • Hi thanks for your reply back its working now if i put {action: 'searchdata', postcode: '123'}, it comes up with action: searchdata and postcode: 123but when {action: 'searchdata', postcode: $("#postcode").val()}, no results and even ajax-admin.php is not called on networks. – Man in black May 12 '17 at 9:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.