2

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.

Image1 Image2

What am I doing wrong?

5
  • have you tried to simply change $_REQUEST['id'] to $_GET['id']? Commented Sep 2, 2017 at 8:38
  • @PeterDarmis, yes, with the same result! Commented Sep 2, 2017 at 14:21
  • Your code works fine since you get ID: ... in your console, in case you want to see output on screen try accessing directly the ajaxurl and add to it the proper query parameters since you use GET. In your case this would be ?action=my_ajax_func&id=5 Commented Sep 3, 2017 at 7:55
  • try to put var_dump inside the if and update your question with the result ;) Commented Sep 5, 2017 at 5:26
  • another thing: if you are on wordpress try enable debug mode to sede errors Commented Sep 9, 2017 at 23:06

2 Answers 2

1

the problem is in

$aux = json_decode($_POST['id'])

just use $_POST['id'] only to get the value

and json_encode(...) to submit a response

EDIT: also noticed an error in your post

jQuery.post(aurl, {id: x}, 'json');

Is wrong, because you are missing the complete function or a done() promise handling

Solution 1:

jQuery.post(aurl, {id: x}, function(response) {
    console.log(response); // just an example
}, 'json');

Solution 2:

jQuery.post(aurl, {id: x}, null, 'json');

Solution 3: Using $.ajax (which is equal to jQuery.ajax) as specified in another answer

Some useful URLs:

Ajax : http://api.jquery.com/jquery.ajax/

Post : http://api.jquery.com/jquery.post/

Get : http://api.jquery.com/jquery.get/

Sign up to request clarification or add additional context in comments.

8 Comments

I've tried both ways, but I'm getting the same Null value.
post in the question the var_dump($_POST)
I've tried everything. I've seen that Wordpress treats ajax differently. Could this be my issue?
as I said, post in the question the var_dump($_POST) and post some code from the server as well
try to replace var aurl = '/wp-content/themes/twentythirteen-child/functions.php'; with var aurl = 'http://www.ved24.no/wp-content/themes/twentythirteen-child/functions.php';
|
0

Make an AJAX post to the file like so:

$.ajax({    
    url:'address/to/your/file.php',
    type: 'post',
    data: {'JSON' : JSON},
    done: function(data) {
        // this is for testing
    }
    }).fail (function() {
        alert('error');
    }).always(function(data) {
        alert(data);                                        
        //Loading or animation until you get a response back (if needed)
    });         
});

And Your PHP file should be more like this:

<?php

if(isset($_POST['AJAX'])) {  //Checks if there is a POST request
    $data = $_POST['AJAX'];  //Then Assigns the data in the Post request to $data

    .....

} else {
    echo 'Error: No Data recieved';
}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.