0

I'm sending the x jQuery variable to php file.

This is my jQuery with the ajax:

  jQuery(document).ready(function($) {

var x = jQuery('#sbering option:selected').val();
jQuery('#optionvalue').html(x);

  jQuery.ajax({
        url: frontEndAjax.ajaxurl,
        data: {
            'action':'my_ajax_function',
            'id' : x
        },
        success:function(data) {
          console.log(data);
        },
    });
});

This is the function where I'm sending the x variable:

function my_ajax_function() {

  if(isset($_REQUEST['id'])) {
    $aux = $_REQUEST['id'];
    echo "ID: " . $aux;
  }
  var_dump($aux);
...

instead of printing both $aux and var_dump($aux) on screen they're only visible in the console and $aux in php is Null.

What am I doing wrong?

  • When you say no result, do you mean it's literally returning 'No Result' from your code, or something else? What does the request say in the console? – Jacob Peattie Sep 1 '17 at 11:08
  • Add wp_die() before close function "my_ajax_function()" other wise return o with result. – ravi patel Sep 1 '17 at 11:55
  • I finally have got some result, but now I'm getting the echo $_POST['id']; only in the console and not in php. – deyan4 Sep 1 '17 at 12:57
  • Your JavaScript is only logging the response to the console, why would it print to the screen? PHP will just return the result to the success callback function. Then you need to use JS to output the the HTML somewhere. – Jacob Peattie Sep 1 '17 at 14:21
  • @JacobPeattie, I need the jQuery variable x in php for some calculations. – deyan4 Sep 1 '17 at 14:24

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.