Reputation: 9103
in a "wordpress" page I have this code:
function displayData(id) {
var $j = jQuery.noConflict();
$j.ajax({
type:'POST',
data:{rid:id},
url: "http://www.mywebsite.com/?page_id=123",
success: function(value) {
alert('<?php echo($_POST['rid']) ?>');
}
});
}
but the value displayed in alert is empty. How can get 'rid' value?
Thanks, julio
Upvotes: 0
Views: 838
Reputation: 2289
You're attempting to retrieve a POST value via PHP in Javascript. This is erroneous. To do correctly, the PHP/Server-side script returns the value in either raw form, HTML or json. This result is returned as data to jQuery's AJAX method. Notice the variable value
in the code below.
function displayData(id) {
var $j = jQuery.noConflict();
$j.ajax({
type:'POST',
data:{rid:id},
url: "http://www.mywebsite.com/?page_id=123",
success: function(value) {
//do something with 'value'
alert(value);
console.log('The result is ' + value);
}
});
}
Upvotes: 1
Reputation: 631
the line:
alert('<?php echo($_POST['rid']) ?>');
will be rendered before the ajax call is made - where is the 'rid' value, in the page where the javascript lives, or the response you get from the ajax call? (ie. mywebsite.com/?page_id=123).
if its from the page you're posting data too, the response from the server would need to return a string or json object (or similar) which contains the data so you can parse it in the success value.
Upvotes: 0