简体   繁体   English

PHP:$ _POST中的未定义索引

[英]PHP: Undefined index in $_POST

I am using forms select. 我正在使用表格选择。 I just want to check what user selects by echo-ing the result on the same page so I kept the action="". 我只想通过在同一页面上回显结果来检查用户选择了什么,所以我保留了action =“”。 But its showing error undefined index slct. 但其显示错误未定义索引slct。 Can any one please help me 谁能帮帮我吗

<form action="" method="post">
<select name="slct">
<option value="yes" selected="selected"> yes </option>
<option value="no"> no </option>
</select>
<input type="button" value="Submit" />
</form>


<?php 
$tofd = $_POST["slct"];
echo $tofd; 
?>

Why its showing the error 为什么显示错误

Notice: Undefined index: slct in C:\wamp\www\Univ Assignment\Untitled-4.php on line 21

You should use button type submit NOT button 您应该使用按钮类型提交NOT按钮

<input type="submit" value="submit" />

And then test IT like 然后像测试它一样

echo (isset($_POST['slct']))? $_POST['slct'] : 'Variable undefined..';

Use PHP isset to check if its exist first 使用PHP isset检查它是否首先存在

Example : 范例:

$tofd = isset($_POST["slct"]) ? $_POST["slct"] : null ;

Example 2 Using a function 示例2:使用函数

function __POST($var)
{
    return  isset($_POST[$var]) ? $_POST[$var] : null ;
}

$tofd = __POST("slct");

If they are on the same page, initaially, $_POST would be empty because the user has not posted anything. 如果它们在同一页上,则$_POST最初为空,因为用户未发布任何内容。 So you have to handle that. 所以你必须处理。

if(isset($_POST["slct"]))
    $tofd = $_POST["slct"];
<?php
  if (isset($_POST["slct"])){
  $tofd = $_POST["slct"];
  echo $tofd; }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM