I'd like to access a PHP array using JavaScript after a successful POST.
我想在POST成功后使用JavaScript访问PHP数组。
PHP Code:
return array('success' => true);
return array('success'=> true);
Javascript Code
$('#Get-Info').submit(function() {
$.post("info.php",
function(data){
if ( data['success'] ) {
// Do things.
}
}
);
return false; });
The javascript function is definitely running, it just can't access the PHP array.
javascript函数肯定在运行,它只是无法访问PHP数组。
1 个解决方案
#1
3
Make the php return json. Not sure about this part as I'm not a php programmer, but the javascript would look like this:
让php返回json。不确定这部分,因为我不是一个php程序员,但javascript看起来像这样:
$('#Get-Info').submit(function() {
$.post("info.php",
function(data){
if ( data['success'] ) {
// Do things.
}
}, "json"
);
return false; });
The only difference being that jQuery will automatically parse the data as json, the datatype parameter. More info.
唯一的区别是jQuery会自动将数据解析为json,即datatype参数。更多信息。
If I'm not horribly wrong, this should work for the php, although it requires PHP 5.2.0:
如果我不是非常错误,这应该适用于PHP,虽然它需要PHP 5.2.0:
echo json_encode(array('success' => true));
#1
3
Make the php return json. Not sure about this part as I'm not a php programmer, but the javascript would look like this:
让php返回json。不确定这部分,因为我不是一个php程序员,但javascript看起来像这样:
$('#Get-Info').submit(function() {
$.post("info.php",
function(data){
if ( data['success'] ) {
// Do things.
}
}, "json"
);
return false; });
The only difference being that jQuery will automatically parse the data as json, the datatype parameter. More info.
唯一的区别是jQuery会自动将数据解析为json,即datatype参数。更多信息。
If I'm not horribly wrong, this should work for the php, although it requires PHP 5.2.0:
如果我不是非常错误,这应该适用于PHP,虽然它需要PHP 5.2.0:
echo json_encode(array('success' => true));