简体   繁体   English

通过POST将JSON编码的变量从PHP传递到Javascript

[英]Passing JSON-encoded variable from PHP to Javascript via POST

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. 我有一个多维数组,我想用Javascript发送到PHP脚本,解析JSON数据并在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 &rarr;"></p>
</form>

json-target.php looks like this: json-target.php看起来像这样:

<?php
    print "The value of \$_POST is ";
    print_r($_POST);
?>

And the output of $_POST is Array ( [jsonarray] => [{ ) . $_POST的输出是Array ( [jsonarray] => [{ ) I wanted to pass the contents of the $jsonarray variable to a Javascript function (please see update below). 我想将$jsonarray变量的内容传递给Javascript函数(请参阅下面的更新)。

UPDATE: I also have a simple Javascript that's supposed to parse the value received from $_POST and post the value via alert() : 更新:我还有一个简单的Javascript,它应该解析从$_POST收到的值并通过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? 有什么更好的方法呢?

JSON encoding makes extensive use of quotes. JSON编码广泛使用引号。 By simply outputting a JSON encoded string into your HTML value attribute, the quotes will interfere with the markup. 只需将JSON编码的字符串输出到HTML value属性中,引号就会干扰标记。 They need to be escaped to be put into the HTML. 它们需要转义才能放入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. 编辑:为了响应您的更新,我不确定您的JSON.parse应该做什么。 Anything encoded with json_encode() in PHP is technically a valid Javascript object, and doesn't need to be parsed any further. 在PHP中用json_encode()编码的任何东西在技术上都是有效的Javascript对象,不需要进一步解析。 If I had an object named $obj with a name property of 'hello', I could do this: 如果我有一个名为$obj ,其名称属性为'hello',我可以这样做:

<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. json_encode的输出是一个非常合适的javascript对象。

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. 使用斜杠转义$_POST数组的输出这一事实让我想到也许你的magic_quotes_gpc指令设置为打开。 If that's the case, you'll have to unescape your $_POST variables with stripslashes() . 如果是这种情况,你将不得不使用stripslashes()来取消$ _POST变量。

Took me a while to find the answer. 花了一些时间才找到答案。 Try: 尝试:

var json = JSON.parse(<?php echo stripslashes($_POST['jsonarray']) ?>);

var json=JSON.parse($('#jsonarray').val()); var json = JSON.parse($('#jsonarray')。val()); alert (json.plate_no[0]); alert(json.plate_no [0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM