echo json_encode with 2 variables with jquery form plugin
code inside php file:
$variable1 = array( 'variable1' => "$variable1" );
$variable2 = array( 'variable2' => "$variable2" );
echo json_encode ($variable1);
code inside main page:
<span id="variable1"></span>
<span id="variable2"></span>
I 开发者_开发技巧am trying to make it so it echos both variables in their spans. doing 2 echos does not work, but the single as coded above works
using the jquery form plugin for this.
If the PHP is fetched w AJAX, then you must create one JSON object with both variables:
<?php
// The PHP page
$variable = array( 'variable1' => "$variable1",
'variable2' => "$variable2" );
// One JSON for both variables
echo json_encode($variable);
?>
And then on the main page, you can access the JSON object and display data from it inside your spans. You can do this as I illustrate, but however you do it, if you put the returned JSON in data
then you can access the 2 variables with data.variable1
and data.variable2
.... like this:
$.getJSON('yourPath/yourPage.php', function(data) {
// Inside your success callback:
$("#variable1").html(data.variable1);
$("#variable2").html(data.variable2);
});
Of course, if you're on the same page, you can use pure PHP:
<?php
$variable = array( 'variable1' => "$variable1",
'variable2' => "$variable2" );
?>
...
<span id="variable1"><?php echo $variable["variable1"]; ?></span>
<span id="variable2"><?php echo $variable["variable2"]; ?></span>
精彩评论