[英]Why does PHP file return "array", but not its contents
I am tring to send an array to PHP, PHP however, only outputs "array".我想将数组发送到 PHP,但是 PHP 只输出“数组”。 I have two files: forms1.php and forms1.js.
我有两个文件:forms1.php 和 forms1.js。 The PHP if statement never turns true.
PHP if 语句永远不会变为真。 I have tried without the if statement too.
我也试过没有 if 语句。
Thanks for pointing it out in the comments, Yes I call the function, and the alert returns entire forms1.php code.感谢您在评论中指出,是的,我调用了该函数,并且警报返回了整个 forms1.php 代码。
old title: Why does my php file not see GET["result"]?旧标题:为什么我的 php 文件看不到 GET["result"]? // this title is wrong
// 这个标题有误
forms1.js表单1.js
var nums = [1,2,3,4];
function postcars() {
$.ajax({
type : 'GET',
url : 'forms1.php',
data : {"result": nums},
success: function(resp)
{
alert(resp);
}
});
}
forms1.php表单1.php
<?php
if ($_GET["result"] != null){
$filename = $_GET["result"];
echo $filename;
}
?>
nums
is an array, elements inside might be sent as individual values and the parameter name is result[]
, make it a single string by using JSON.stringify
nums
是一个数组,里面的元素可以作为单独的值发送,参数名称是result[]
,使用JSON.stringify
使其成为单个字符串
var nums = [1,2,3,4];
function postcars() {
$.ajax({
type : 'GET',
url : 'forms1.php',
data : {"result": JSON.stringify(nums)},
success: function(resp)
{
alert(resp);
}
});
}
What I'd normally do would be to test if a particular parameter is in the request ( using isset()
or !empty()
) and then call ob_clean()
to discard any possible output buffer generated at that stage.我通常会测试请求中是否包含特定参数(使用
isset()
或!empty()
),然后调用ob_clean()
丢弃在该阶段生成的任何可能的输出缓冲区。 Process the request and then terminate the processing completely as it is a different thread.处理请求,然后完全终止处理,因为它是一个不同的线程。 The response then that is set back is only what you want to send.
然后设置回的响应只是您想要发送的。 This approach with a GET request would cause a regular page loading where there is a querystring, such as
forms1.php?result=banana
, to halt at that stage and display no further content however/.这种带有 GET 请求的方法会导致常规页面加载,其中有一个查询字符串,例如
forms1.php?result=banana
,在该阶段停止并且不显示更多内容。
<?php
if( isset( $_GET['result'] ) ){
ob_clean();
$filename = $_GET["result"];
/* do interesting stuff with variable... */
exit('Hello World! - Send a response');
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>geronimo</title>
<script src='//code.jquery.com/jquery-latest.js'></script>
</head>
<body>
<script>
var nums = [1,2,3,4];
function postcars() {
$.ajax({
type : 'GET',
url : 'forms1.php',
data : {"result": nums},
success: function(resp){
alert(resp);
}
});
};
// oops, forgot to call the ajax function
postcars();
</script>
</body>
</html>
You did not call the function postcars()
;您没有调用
postcars()
函数;
The JavaScript file should be like this: JavaScript 文件应该是这样的:
var nums = [1,2,3,4];
function postcars() {
$.ajax({
type : 'GET',
url : 'forms1.php',
data : {"result": nums},
success: function(resp)
{
alert(resp);
}
});
}
postcars();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.