Shamil Omarov
Shamil Omarov

Reputation: 86

Notice: Undefined index: name

I just installed XAMPP with PHP 7. I had a script that worked before, but after installing there is an error. I will show an example which is also not working. The problem is in $_POST, I think it can be because of configurations in XAMPP or PHP.

<?php 
echo "<form action='check.php' method=\"post\"> 
<input type=\"text\" name=\"name\" >
<input type=\"submit\" name=\"submit\" value=\"ok\">
</form>";
if (isset($_POST['submit'])){echo $_POST['name'];}
if (isset($_POST['name'])) var_dump($_POST['name']);
?>

This code doesn't return anything, but if I just add echo $_POST['name']; it returns error "Notice: Undefined index: name in D:\XAMPP\php\www\index.php on line 13". How can I fix it?

Upvotes: 2

Views: 2269

Answers (2)

Shamil Omarov
Shamil Omarov

Reputation: 86

@hherger I deleted xampp server and installed wampserver with php 5.6. Now it shows another error.

Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0

Warning: Cannot modify header information - headers already sent in Unknown on line 0

Upvotes: 2

hherger
hherger

Reputation: 1680

It seems that you have the code for all REQUEST_METHODs in the same script.
That's ok, but then you have to differentiate how you react:

  • either send out the form for the user to fill out and transmit,
  • or interprete the data transmitted by the form.

Try this code adopted from yours:

<?php
    // Check if the form has been transmitted or not
    if ($_SERVER['REQUEST_METHOD']=='POST') {
        // A form was transmitted
        if (isset($_POST['name'])) var_dump($_POST['name']);
    } else {
        // Send the form out so the user can transmit it
        echo "<form action='check.php' method=\"post\"> 
<input type=\"text\" name=\"name\" >
<input type=\"submit\" name=\"submit\" value=\"ok\">
</form>";
    }
?>

Upvotes: 0

Related Questions