0

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
2

4 Answers 4

1

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..';
Sign up to request clarification or add additional context in comments.

Comments

0

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");

Comments

0

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"];

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.