admin管理员组文章数量:1201801
I have an multi-dimensional array that I want to send to a PHP script with a Javascript that parses the JSON data and plot it on Google Maps. I'm trying to simulate it using forms:
<?php
$jsontest = array(
0 => array(
'plate_no' => 'abc111',
'longlat' => array(121.003895,14.631563),
'info' => 'first item'
),
1 => array(
'plate_no' => 'abc222',
'longlat' => array(121.103895,14.731563),
'info' => 'second item'
)
);
$jsonarray = json_encode($jsontest);
?>
<form action="json-target.php" method="post" accept-charset="utf-8">
<input type="hidden" name="jsonarray" value="<?php echo $jsonarray; ?>" id="jsonarray">
<p><input type="submit" value="Continue →"></p>
</form>
json-target.php looks like this:
<?php
print "The value of \$_POST is ";
print_r($_POST);
?>
And the output of $_POST
is Array ( [jsonarray] => [{ )
. I wanted to pass the contents of the $jsonarray
variable to a Javascript function (please see update below).
UPDATE: I also have a simple Javascript that's supposed to parse the value received from $_POST
and post the value via alert()
:
<script src="/js/json2.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var json = JSON.parse(<?php echo $_POST['jsonarray'] ?>);
for (var i = 0; i < json.length; i++) {
alert(json[i]);
}
</script>
But the output is mangled with backslash characters.
var json = JSON.parse([{\"plate_no\":\"abc111\",\"longlat\":[121.003895,14.631563],\"info\":\"first item\"},{\"plate_no\":\"abc222\",\"longlat\":[121.103895,14.731563],\"info\":\"second item\"}]);
What's a better way of doing this?
I have an multi-dimensional array that I want to send to a PHP script with a Javascript that parses the JSON data and plot it on Google Maps. I'm trying to simulate it using forms:
<?php
$jsontest = array(
0 => array(
'plate_no' => 'abc111',
'longlat' => array(121.003895,14.631563),
'info' => 'first item'
),
1 => array(
'plate_no' => 'abc222',
'longlat' => array(121.103895,14.731563),
'info' => 'second item'
)
);
$jsonarray = json_encode($jsontest);
?>
<form action="json-target.php" method="post" accept-charset="utf-8">
<input type="hidden" name="jsonarray" value="<?php echo $jsonarray; ?>" id="jsonarray">
<p><input type="submit" value="Continue →"></p>
</form>
json-target.php looks like this:
<?php
print "The value of \$_POST is ";
print_r($_POST);
?>
And the output of $_POST
is Array ( [jsonarray] => [{ )
. I wanted to pass the contents of the $jsonarray
variable to a Javascript function (please see update below).
UPDATE: I also have a simple Javascript that's supposed to parse the value received from $_POST
and post the value via alert()
:
<script src="/js/json2.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var json = JSON.parse(<?php echo $_POST['jsonarray'] ?>);
for (var i = 0; i < json.length; i++) {
alert(json[i]);
}
</script>
But the output is mangled with backslash characters.
var json = JSON.parse([{\"plate_no\":\"abc111\",\"longlat\":[121.003895,14.631563],\"info\":\"first item\"},{\"plate_no\":\"abc222\",\"longlat\":[121.103895,14.731563],\"info\":\"second item\"}]);
What's a better way of doing this?
Share Improve this question edited Sep 4, 2009 at 5:39 FrancisV asked Sep 4, 2009 at 4:50 FrancisVFrancisV 1,7095 gold badges23 silver badges37 bronze badges 1- What exactly is the question? – rojoca Commented Sep 4, 2009 at 4:55
3 Answers
Reset to default 16JSON encoding makes extensive use of quotes. By simply outputting a JSON encoded string into your HTML value
attribute, the quotes will interfere with the markup. They need to be escaped to be put into the HTML. Try this:
<input type="hidden" name="jsonarray" value="<?php echo htmlspecialchars($jsonarray,ENT_QUOTES); ?>" id="jsonarray">
Edit: In response to your update, I'm not sure what your JSON.parse is supposed to be doing. Anything encoded with json_encode()
in PHP is technically a valid Javascript object, and doesn't need to be parsed any further. If I had an object named $obj
with a name property of 'hello', I could do this:
<script type="text/javascript">
var o = <?php echo json_encode($obj); ?>;
alert(o.name);
</script>
and get an alert saying 'hello'. The output of json_encode is a perfectly suitable javascript object.
The fact that the output of your $_POST
array has been escaped with slashes leads me to think that perhaps your magic_quotes_gpc directive is set to be on. If that's the case, you'll have to unescape your $_POST variables with stripslashes()
.
Took me a while to find the answer. Try:
var json = JSON.parse(<?php echo stripslashes($_POST['jsonarray']) ?>);
var json=JSON.parse($('#jsonarray').val()); alert (json.plate_no[0]);
本文标签: Passing JSONencoded variable from PHP to Javascript via POSTStack Overflow
版权声明:本文标题:Passing JSON-encoded variable from PHP to Javascript via POST - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738575096a2100830.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论