最新编辑:事实证明,在formprocessing.php,isset($ _ POST ['submit1'])是FALSE(其中'submit1'现在是提交按钮的名称;原来它没有名字)。正如下面的代码所示,我最初并没有为此进行测试。PHP Post方法显然不工作
它确实解释了很多。但剩下的问题是为什么 isset($ _ POST ['submit1'])是FALSE。
注意:此问题已被编辑以反映更近的见解。
作为一名PHP初学者,我正在尝试将表单发布到服务器,并且它不起作用。在所有的可能性中,我忽略了一些简单的东西,但我只是没有看到它。
我有两个文件:'form.php'和'formprocessing.php',都位于同一个文件夹中。我已经包含了下面的完整代码,但首先要解释一下。文件'form.php'包含表单本身,包括'post'方法。文件'formprocessing.php'是'post'方法的目的地,也就是说,'action = formprocessing.php'。
想法是,formprocessing应该没有'formprocessing.php'加载或'form.php'重新加载(因此“event.preventDefault();”在form.php)。我知道我也可以发布到'form.php'本身,但现在我想用两个单独的文件去。
我已经从jQuery网站几乎字面上取了'form.php'的代码(http://api.jquery.com/jQuery.post/,最后一个例子)。我已将action属性中的文件名更改为'formprocessing.php',并添加了必要的JSON.stringify命令(请参阅下文)。
'formprocessing.php'中的其余代码只是简单地提取发布的输入字段的值(表单中只有一个字段),并以“form.php”的形式将其返回给JSON对象(然后在form.php中进行字符串化)。至少这是主意!
在'formprocessing.php。'生成的脚本 - 打字,说:“XXX”到输入字段后 - 如下:
<script> {"content":"xxx"} </script>
现在我的眼里,这个剧本似乎是正确的 - 但果真如此吗?这是我现在想知道的第一件事情。
编辑:我现在已经删除了formprocessing.php中的所有HTML标记。这也意味着生成的脚本简化为{“content”:“xxx”},即只有JSON对象。虽然不改变结果。
因为有些事情后来出问题了。也就是说,在'form.php'结尾处,我将从'formprocessing.php'返回的数据内容附加到div元素。但事实上,连接到div的字符串变成“{”length“:0,”prevObject“:{”0“:{},”1“:{},”2“:{},”3“ :{} “4”:{}, “5”:{}, “6”:{}, “7”:{}, “8”:{}, “长度”:9}, “选择器”: “#content”}“ - 即指示长度为0的对象。而不是从'formprocessing.php'(正如我所说的也等于表单字段的原始输入)推测的实际数据。
这里来“form.php的”的代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.post demo</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<form action="formprocessing.php" id="searchForm">
<input type="text" name="s" placeholder="Search..." />
<input type="submit" value="Search" />
</form>
<!-- the result of the search will be rendered inside this div -->
<div id="result"></div>
<script>
/* attach a submit handler to the form */
$("#searchForm").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $(this),
term = $form.find('input[name="s"]').val(),
url = $form.attr('action');
/* Send the data using post */
var posting = $.post(url, { s: term });
/* Put the results in a div */
posting.done(function(data) {
var content = $(data).find('#content');
contentstring = JSON.stringify(content);
$("#result").empty().append(contentstring);
});
});
</script>
</body>
</html>
而这里的“formprocessing.php”的代码:
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8" />
<title>Contact</title>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>
</head>
<body>
<script>
<?php
echo "alert('Hello!');";
$invoer = $_POST['s'];
echo (json_encode(array("content" => $invoer)));
?>
</script>
</body>
</html>
任何帮助非常赞赏。
是的,你是对的......我应该只是在那里使用Javascript(在PHP开始标签之前)。正如我所说,我几乎是一个初学者。 – Holland
使用firebug并查看控制台是否正确执行了ajax。 –
@ Sandeep.sarkar:好主意。是的,'formprocessing.php'中的脚本似乎是正确的。也就是说,如果我在输入字段中输入“xxx”,则生成的脚本为: 。 这似乎是正确的(我已删除警报)。另一方面,内容似乎没有回到form.php,即内容没有被附加到div元素。我将进一步用Firebug进行试验。 – Holland