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="". But its showing error undefined index 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
<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
Example :
$tofd = isset($_POST["slct"]) ? $_POST["slct"] : null ;
Example 2 Using a function
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. So you have to handle that.
if(isset($_POST["slct"]))
$tofd = $_POST["slct"];
<?php
if (isset($_POST["slct"])){
$tofd = $_POST["slct"];
echo $tofd; }
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.