我只是学习PHP而且我一直收到一个Undefined Index错误.我正在学习的这本书有一个HTML表单和一个处理表单的PHP页面,使用以下格式:
<!-- The form fields are all set up something like this -->
<input type="text" id="howlong" name="howlong" /><br />
// The PHP starts with one line like this for each of the form fields in the HTML
$how_long = $_POST ['howlong'];
// And there is one line for each one like this to output the form data:
echo ' and were gone for ' . $how_long . '<br />';
Run Code Online (Sandbox Code Playgroud)
我正在使用的示例有大约12个表单字段.
奇怪的是,并非所有变量都会抛出此错误,但我无法看到它的模式.
我已经检查过所有HTML字段名称与我输入的PHP $ _POST变量名称相匹配,并且我已经确定当我填写表单并提交时,所有字段都填充了一些内容.有趣的是,可以为本书下载的完整代码也会引发此错误.
我意识到这段代码可能无法反映最佳实践,它来自本书的第一章,显然我是一个菜鸟:)
如果它有所作为,我在XAMPP 1.7.4和Windows 7 Home Premium上使用PHP 5.3.5.
记得在表单标签上将方法设置为POST ...
继承了我曾经尝试过的代码,它对我有用:
在名为test.php的文件中:
<html>
<body>
<form method="POST" action="testProc.php">
<input type="text" id="howlong" name="howlong" /><br/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
并在testProc.php中:
<?php
if (isset($_POST)) {
if (isset($_POST["howlong"])){
$howlong = $_POST['howlong'];
echo ' and were gone for ' . $howlong . '<br />';
}
}
?>
Run Code Online (Sandbox Code Playgroud)
就像建议一样,要使用样式表进行显示操作,我建议将表单放在表中,如下所示:
<html>
<body>
<form method="POST" action="testProc.php">
<table>
<tbody>
<tr>
<th>
<label for="howlong">How long? :</label>
</th>
<td>
<input type="text" id="howlong" name="howlong" />
</td>
</tr>
<tr>
<input type="submit" value="submit"/>
</tr>
</tbody>
</table>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
希望你能用这个......
| 归档时间: |
|
| 查看次数: |
16896 次 |
| 最近记录: |