0

I'm now making a contact form, but I get the error below. Undefined variable: name_result in check.php

I want to make the website like: Welcome Mr.(Ms.) name

HTML(Updated)↓

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <h1>Contact form</h1>
    <form method="POST" action="check.php">
        <div>
        <div>
        <label for="" class="font">Title:</label>
        <label for="" class="font">Name:</label>
        </div>
        <div>
            <div>
            <input type="radio" name="gender" value="Mr.">
            <input type="radio" name="gender" value="Ms.">
       <input type="text" name="name" value="" class="text"><br>
            </div>
            <br>
        <div>
        <input type="submit" value="submit" name="submit">
        </div>
    </form>
</body>
</html>

check.php(Updated)↓

<?php
    $gender = $_POST['gender'];  
    $name = $_POST['name']; 
    $name_result = '';
        if(isset($_POST['submit'])){ 
                if(isset($_POST['gender'])){
                    $name_result = 'welcome ' . $gender . $name;
                }else{
                    $name_result = 'Oops! No name typed';
                }
            }
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <h1>confirm</h1>
    <div>
    <p><?php echo $name_result; ?></p>
    </div>
</body>
</html>

Thank you in advance.

Sanae
  • 51
  • 2
  • 10

3 Answers3

2

In check.php, you should declare $name_result as an empty string. and for accessing the post variables or get data, they should have a name attribute. So, give the submit button a particular name like submit as you are using.

Also, you have a syntax error at

$name_result = 'welcome ' . $gender . $name];

<!DOCTYPE html>
    <html lang="en">
    <head>
    </head>
    <body>
        <h1>Contact form</h1>
        <form method="POST" action="check.php">
            <div>
            <div>
            <label for="" class="font">Title:</label>
            <label for="" class="font">Name:</label>
            </div>
            <div>
                <div>
                <input type="radio" name="gender" value="Mr.">Mr.
                <input type="radio" name="gender" value="Ms.">Ms.
           <input type="text" name="name" value="" class="text"><br>
                </div>
                <br>
            <div>
            <input type="submit" value="submit" name="submit">
            </div>
        </form>
    </body>
    </html>

Check.php

<?php
    $gender = $_POST['gender'];  
    $name = $_POST['name']; 
    $name_result='';
        if(isset($_POST['submit'])){ 
                if(isset($_POST['gender'])){
                    $name_result = 'welcome ' . $gender .' '. $name;
                }else{
                    $name_result = 'Oops! No name typed';
                }
            }
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <h1>confirm</h1>
    <div>
    <p><?php echo $name_result; ?></p>
    </div>
</body>
</html>
Amanjot Kaur
  • 2,028
  • 4
  • 18
  • 33
1

declare name_result variable globally at the top of the php script with blank string.

$name_result = '';

for now it is functioning as the local variable with limited scope.

Farhan
  • 253
  • 2
  • 9
  • Thank you for your answer. I'm sorry but my understanding is very little about PHP as I'm new to this industry. I tried as you mentioned, but I just got blank result in the name content. How could I display "Welcome [Mr./Ms] [name]" ? – Sanae Jan 18 '20 at 09:21
  • in this case, value for the radio button will Mr./Ms respectively instead of male & female. value attribute will pass as the post parameter – Farhan Jan 18 '20 at 09:48
  • update radio button as ** Mr. Ms. ** and print it as follows ** $name_result = 'welcome ' . $gender . ' ' . $name; ** – Farhan Jan 18 '20 at 09:51
  • just realized, you have not set any "name" attribute to your submit button. assign name attribute to submit button as well, as you are checking the condition of its value in check.php file. also, use htmlspecialchars() instead of h() to print to result with echo. ** ** – Farhan Jan 18 '20 at 10:11
  • Thank you for your answer again. Yes, there were some other people pointed out about that, and I modified that point, but it's still the same. I don't know why..:( – Sanae Jan 18 '20 at 10:17
  • have you tried to update the submit button code as above? ** ** just add name attribute with as above and you will get result for sure. – Farhan Jan 18 '20 at 10:19
  • Yes, I added the name attribute. – Sanae Jan 18 '20 at 10:21
  • Also, replace **** with **** – Farhan Jan 18 '20 at 10:22
1

give name to your submit button name="submit"

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

define $name_result as global variable

<?php
     $gender = $_POST['gender'];  
    $name = $_POST['name']; 
    $name_result="";
        if(isset($_POST['submit'])){ 
             if(isset($_POST['gender'])){
               $name_result = 'welcome ' . $gender . $name];
               }else{
                    $name_result = 'Oops! No name typed';
                }
            }
?>
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
  • Thank you very much for your answer. I have tried the code above, but the result was blank in the line of name: ... – Sanae Jan 18 '20 at 10:01
  • where you echo $name_result remove h and brackets from their – PHP Geek Jan 18 '20 at 10:04
  • Thank you for your answer again. Unfortunately, it's still blank..:( I'm trying other ways, but it doesn't work. – Sanae Jan 18 '20 at 10:15