[英]PHP $_POST error Please Help me I am learning PHP
我正在学习PHP。 这是源代码。
<?php
$text = $_POST['text'];
echo $text;
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>
这是结果。 我不知道问题出在哪里。
注意:未定义的索引:第2行上C:\\ xampp \\ htdocs \\ faisal \\ index.php中的文本
这意味着$_POST['text']
什么都没有-直到提交表单后才有。 您需要使用isset()
进行检查:
<?php
if(isset($_POST['text'])) {
$text = $_POST['text'];
echo $text;
}
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>
当您第一次进入页面时,您的特殊变量“ $ _POST”为空,这就是为什么会出现错误。 您需要检查其中是否有任何东西。
<?php
$text = '';
if(isset($_POST['text']))
{
$text = $_POST['text'];
}
echo 'The value of text is: '. $text;
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>
$_POST['text']
仅在提交表单时填充。 因此,在首次加载页面时,该页面不存在,您会收到该错误。 作为补偿,您需要在执行其余PHP之前检查是否已提交表单:
<?php
if ('POST' === $_SERVER['REQUEST_METHOD']) {
$text = $_POST['text'];
echo $text;
}
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>
您很可能必须确定表单是否已提交。
<?php
if (isset($_POST['text'])) {
$text = $_POST['text'];
echo $text;
}
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>
另外,您可以使用$_SERVER['REQUEST_METHOD']
。
if ($_SERVER['REQUEST_METHOD'] == 'POST') {...
我们必须检查用户是否单击了提交按钮,如果是,则必须设置$ test变量。 如果我们不使用isset()方法,则总是会出错。
<?php
if(isset($_POST['submit']))
{
$text = $_POST['text'];
echo $text;
}
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit">
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.