I am using this code to post some data
$('.update_btn').live("click",function()
{
var ID = $(this).attr("id");
var dataString = 'msg_id='+ ID;
$.ajax({
type: "POST",
url: "get-data.php",
data: dataString,
cache: false,
success: function(){
//alert(dataString);//is not empty
getData();
}
});
return false;
});
function getData()
{
$.ajax({
type: "GET",
url: 'get-data.php',
success: function()
{
var jq = '<?php echo $_POST['msg_id'];?>';
alert(jq);
}
});
}
and function data to retrieve the just posted data but the var jq has no post data.What could be the cause?.
Being that you’re using jQuery (or any JavaScript) you don’t embed PHP code into it…
Assuming that you’re only wanting to return the
msg_idand nothing else yourget-data.phpwould be something like this:Which would then turn your getData function to this:
Now, to expand on this — if your return is going to be some sort of an array you’ll want to use json_encode in your php… like so:
Which logically, because we’re returning a
json arrayour JavaScript also changes:Not much of a difference between the two.. But hopefully this answer has put you further on the path to an enlightened understanding of the tools you are attempting to use.. There are plenty of jQuery tutorials out there if you only google for them… Many JavaScript die hards (myself included), will urge you to learn Native JavaScript so that you can understand what is happening behind the scenes of jQuery. It is a very good idea to do, and again there is a lot of information and tutorials about it if you google for it.
I’m sure some people have some books they can recommend for your reading as well..