现在我正在做一个应用程序,将谷歌地图中的一系列共存节点保存到隐藏的输入中,然后我从表单中获取数据到PHP,它输出正常,但是当我尝试读取字符串时解码它返回NULL的JSON。
现在这是我的Javascript代码:
var bounds = [];
var bound = {};
polygon.getPaths().forEach(function(punto, indice){
punto.forEach(function(puntoX, indice){
bound = {'lat': puntoX.lat(), 'lng': puntoX.lng()};
bounds.push(bound);
});
});
$('#bounds').val(JSON.stringify(bounds));
HTML code:
<form class="signup-block" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?c=create"; ?>">
<button id="save" class="button success disabled" type="submit" disabled>Crear Geocerca</button>
<br>
<h3>Datos de la geocerca</h3>
<label for="nombre">Nombre de la geocerca:</label>
<input type="text" id="nombre" name="nombre" required />
<label for="tipo">Tipo de geocerca:</label>
<input type="text" id="tipo" name="tipo" readonly />
<div id="registro"></div>
<input id="bounds" type="hidden" name="bounds" />
这是由JS
指定后的值的边界输入<input id="bounds" type="hidden" name="bounds" value="[{"lat":-2.1585226452743442,"lng":-79.8945227265358},{"lat":-2.1670138304221926,"lng":-79.9018183350563},{"lat":-2.176963037271158,"lng":-79.8945227265358},{"lat":-2.1733607458083646,"lng":-79.875468313694},{"lat":-2.1587799546129793,"lng":-79.8783865571022}]">
PHP代码:
$bounds =filter_input(INPUT_POST,"bounds", FILTER_SANITIZE_STRING);
if (!$bounds) {
$this->error['error'] = 'Parametros incorrecto';
}else{
$coords = json_decode($bounds);
echo var_dump($bounds);//Outputs something
echo var_dump($coords);//Outputs null
我已经检查过了$ bounds变量并获取了值,但它没有将它们读作JSON,我做错了什么?
这是var_dumps的输出 输出$ bounds
string(408) "[{"lat":-2.1581795660883976,"lng":-79.90096002817154},{"lat":-2.171559596852916,"lng":-79.9018183350563},{"lat":-2.176448424731824,"lng":-79.89057451486588},{"lat":-2.1662419064644722,"lng":-79.88053232431412},{"lat":-2.156206859267206,"lng":-79.8930636048317},{"lat":-2.15835110569104,"lng":-79.90164667367935}]"
$ coords的输出
NULL
答案 0 :(得分:1)
如果其他人遇到与我相同的情况,问题是HTML输出会打印所有&#34;和&#39;作为html实体,所以这是必要的第一次
$bounds = html_entity_decode($bounds);
$coords = json_decode($bounds, true);
echo var_dump($coords);
我花了很长时间以不同的方式分析页面输出
答案 1 :(得分:-1)
你可以试试这个: jsondecode($边界内,真)