-2

I am trying to get the form values when clicking on the submit button, but when I run this, I get the below error:

Notice: Undefined variable: mail in C:\xampp\htdocs\mylearning\add_user.php on line 20

Here is my code:

<html> 
<head> 
    <title> I am learning </title> 
</head> 
<body> 
    <form name="new_user" method="post" action="add_user.php">  
       <input type="text" name="mail"/> <br /> 
        <input type="text" name="name"/><br/> 
        <input type="submit" name="submitbtn"  value="continue"/> 
    </form>
 </body> 
</html>



<?php 
if(isset($_POST["submitbtn"])) 

$mail = isset($_POST['mail']);
$name1 = isset($_POST['name']); echo "Returned value from the function : $name1";  echo "Returned value from the function : $mail"; 

?>
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83

3 Answers3

1

You missed { } of if .

Try this way :

        <html> 
    <head> 
        <title> I am learning </title> 
    </head> 
    <body> 
        <form name="new_user" method="post" action="add_user.php">  
           <input type="text" name="mail"/> <br /> 
            <input type="text" name="name"/><br/> 
            <input type="submit" name="submitbtn"  value="continue"/> 
        </form>
     </body> 
    </html>  
    <?php 
    if(isset($_POST["submitbtn"])) 
    {
        $mail = isset($_POST['mail']);
        $name1 = isset($_POST['name']); echo "Returned value from the function : $name1";  echo "Returned value from the function : $mail"; 
    }
    ?>
Bindiya Patoliya
  • 2,726
  • 1
  • 16
  • 15
1

You are missing the {}, if you omitt them only the first instruction after the if will be executed inside the if condition, if you need multiple lines you need to add the curly brackets.

Other notes, you are assigning the value of isset to the $mail variable, isset returns true if the variable exists and it's not null, false otherwise, you could use the ternary operator:

$mail = isset($_POST['mail']) ? $_POST['mail'] : '';

That reads, if the post variable is not null use that, otherwise use 'an empty string, the same goes for $name.

Ende Neu
  • 15,581
  • 5
  • 57
  • 68
0
 if(isset($_POST["submitbtn"])){
 $mail = isset($_POST['mail']);
 $name1 = isset($_POST['name']); echo "Returned value from the function : $name1";
 }

The code will print 1Returned value from the function : 1 because $mail and $name1 is 1. That's what isset function returns! It checks if you have filled something in those text fields, not what you have filled.

Dionis Beqiraj
  • 737
  • 1
  • 8
  • 31