How to pass variables back using jquery .post from a php file?
All I want is that in php script 1 that has the .post javascript jquery lib code. I want php script 1 to send a form submission to php script 2 that would take those values and update the database. Then with php script 2 I want to send a variable back to php script 1 to let it know that the update went good or not.
on php script 1 I have a variable called success I first assign a zero to it. Then run the .post code and expect to have the success variable assigned a 1 to it.
on php script 1 after the .post I have a switch with 2 cases one if success is 1 then I have a string saying " Database updated in good terms". I then have another case where success is 0 then a string says " sorry the attempt failed please try again".
Those strings are assigned to a variable called db_status.
So I then append db_status in the div. Which displays that string message.
I know how I can use .post and get html code from the php file on the callback of .post.
I just want to know how I can pass back javascri开发者_Go百科pt or php variables.
In the PHP just echo a variable, ie echo $testVariable; and the value of $testVariable is 10, do this in your jquery
$.ajax
({
type: "POST",
url: "order.php",
dataType: "json",
data: postData,
cache: false,
success: function(testVariable)
{
alert("Order Saved");
$('#assigned_id').html(testVariable);
}
});
assigned_id now has the value of 10 in it!
What you need to do is find a way for the php script 1 to send post data through ajax.. You can do this by doing:
<?php
$variable = 0;
?>
<script type="text/javascript">
$(document).ready(function(){
$('#input').ready(function(){
$.post("phpscript2.php", {db_code: ""+ $(this).val() +""}, function(data){
if(parseInt(data) == 1){
$('.message').html('Database updated in good terms');
} else {
$('.message').html('Sorry the attempt was a fail.');
}
});
});
});
</script>
<input type="hidden" value="<?php echo $variable; ?>
<div class="message">Please wait while we process</div>
Then on PHP script 2 you would have:
<?php
$variable = $_POST['db_code'];
// Do some code with the posted data that is valued to 0...
$new_variable = 1;
echo $new_variable;
?>
This would submit the hidden input field when it loads (php loads before jquery so hidden field will have the value of hte php variable). It will send the value of the hidden field POST using $.post the way you wanted it to, and will send php script 2 the value of the variable in php script 1. Which you will then process the posted data in which ever way you want to do it. Then echo out a new variables value or even can just echo 1, then the echo will return to ajax and there we will parse the data given back, which is 1 but it was echoed so it became a string, and we need to parse it into a integer, check it with a if else statement saying if 1, do this, else do this, setting the message classed div with the success or fail messages. The great thing is that the hidden input can not be edited as it sends the value post, right when the browser loads giving the client no time to edit the value.
Thats the theory anyways. This isnt tested, and im still fairly new at the ajax scene.
Hope i helped!
As you said, the way to show the outcome of a jQuery.post(...) is to use the build in callback. This way you can always "pass back ... php variables". Remember that the PHP can never directly return the variables, it has to be rendered into a finished page or if you choose another response type a JSON structure, XML document or whatever format suits your needs.
If you would like to return a javascript block then I think you could do something like this:
$.ajax({
type: "GET",
url: "myphp1.php",
dataType: "script"
});
But you could also use dataType "html":
jQuery docs:
If html is specified, any embedded JavaScript inside the retrieved data is executed before the HTML is returned as a string. Similarly, script will execute the JavaScript that is pulled back from the server, then return the script itself as textual data.
精彩评论