2012-09-21 49 views
0

我正在使用表格选择。我只想通过在同一页面上回显结果来检查用户选择的内容,所以我保留了action =“”。但它显示错误未定义索引slct。任何一个可以帮我

<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; 
?> 

为什么它显示错误

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

如果表单提交,将显示值。 btw祝你好运。 ;) – Dev

+0

[PHP:“注意:未定义的变量”和“注意:未定义的索引”]的可能重复(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-指数) – Jocelyn

回答

0

使用PHP isset检查,如果其存在的第一

例子:

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

例2使用功能

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

$tofd = __POST("slct"); 
0

如果它们在同一页面上,初始的$_POST将是空的,因为用户没有发布任何内容。所以你必须处理。

if(isset($_POST["slct"])) 
    $tofd = $_POST["slct"]; 
1

你应该使用按钮式提交NOT按钮

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

并对其进行测试样

echo (isset($_POST['slct']))? $_POST['slct'] : 'Variable undefined..'; 
0
<?php 
    if (isset($_POST["slct"])){ 
    $tofd = $_POST["slct"]; 
    echo $tofd; } 
?>