1

I am new to PHP... Just trying this simple piece of code inside my about.php file (which links to index.php file via a hyperlink):

<form action ="about.php" method="POST">
<ul>
    <li>
        <label for="name"> Enter your name please:</label>
        <input type="text" name="name"/>

    </li>   
    <li>
        <label for="comments" rows="20"> Enter your comments :</label>
        <textarea id="comments" name="comments" rows="5" cols="38">
            <?php 

                $name = $_POST["name"];

                if (!empty($name)) {
                echo "Your name is ".$name."and you like ny site!";
                } else {
                echo "Please enter your name";
                }


                ?> 
        </textarea> 
    </li>   
    <li>
        <input type="submit" />
    </li>   
</ul>   

I get the following error:


Notice: Undefined index: name in D:\xampp\htdocs\stathis1\about\about.php on line 71
Please enter your name

0

4 Answers 4

1

Because $_POST["name"] is not set, you get that notice. So you need to test if it exists first, and then assign it to your $name variable, or empty string if it's not set.

You can change

$name = $_POST["name"];

to

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

Sign up to request clarification or add additional context in comments.

3 Comments

I dont get the error but still the program doesnt do what is supposed to, which is to to put the inserted name in the textarea... I believe the cursor never enters the if (!isset($name)) condition...
don't forget to close the form with </form>
you can check the content of $_POST with var_dump($_POST), and it might be a good idea to update your question with the changed code
1

use isset() instead of empty() because isset() function not triggering notice when the index not defined.

Comments

1

It's because $_POST['name'] hasn't been submitted/set yet. You need to either use an if statement or a ternary operator:

if (isset($_POST['name'])) {
    $name = $_POST['name'];

    if (!empty($name)) {
        echo 'Your name is ' . $name . ' and you like ny site!';
    } else {
        echo 'Please enter your name';
    }
}

Or, a shorter method:

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

Comments

0

Right after starting your php part add this if sentence surrounding the code, so it only executes when the "name" exists.

if(isset($_POST["name"]))

Like this:

<?php 
    if(isset($_POST["name"])){
        $name = $_POST["name"];

        if (!empty($name)) {
            echo "Your name is ".$name."and you like ny site!";
        } else {
            echo "Please enter your name";
        }
    } else {
        echo "Please enter your name";
    }
?> 

1 Comment

I get "Please enter your name " in the textbox even when push the submit button after inserting a name in the name field.... Why name doesnt exist?

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.