2

When i first open the site in wamp it says:

Notice: Undefined index: name in C:\wamp\www\fshije\fshije.php on line 11

Notice: Undefined index: email in C:\wamp\www\fshije\fshije.php on line 12.

But after i put some values it works and click submit the Notice table will disapear.

I could solve this if i would write inside action"anotherfile.php" and the 2 last lines of PHP at that other file normally with post changed to GET can any one explain me how to avoid the Notice tables at the start without the second option.

I am sory that i aksed this question i am new in php

Thank You

<!DOCTYPE html>
<html>
    <body>
        <?php
            echo'<form method="POST" action="">';
            echo 'Name: <input type="text" name="name"><br>';
            echo 'E-mail: <input type="text" name="email"><br>';
            echo '<input type="submit">';
            echo '</form>';

            echo $_POST['name']; echo "<br>";
            echo $_POST['email'];
        ?>
    </body>
</html>
Icarus
  • 1,627
  • 7
  • 18
  • 32
Alban Kaperi
  • 597
  • 4
  • 12
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – tlenss Apr 15 '14 at 19:28
  • you need to use isset for first call cause first time is not posting or you can check is $_SERVER['REQUEST_METHOD']=='POST' then go for the $_POST – Fisherman Apr 15 '14 at 19:30

1 Answers1

4

$_POST[] won't contain anything until after the form is POSTed, use an isset() conditional.

if(isset($_POST['name'], $_POST['email'])) {
    echo $_POST['name']; echo "<br>";
    echo $_POST['email'];
}

As a side note, I wouldn't show any HTML if you are processing the form (unless you have an error to display with the form). In this case you would continue the conditional like this:

} else {
     // Display HTML and form here
}
Sam
  • 20,096
  • 2
  • 45
  • 71
  • any idea why it works if i use 2 files one for the html code and one for the php code and instead post get is needed but in that way isset is not needed? – Alban Kaperi Apr 15 '14 at 19:42
  • @user3527794 the reason one file is always breaking is because you are always running `echo $_POST['name']`, etc (and `$_POST['name']` isn't included until after form submission). If you split it into two files, your HTML file never looks for the `POST`ed variables. Technically if you called your PHP file directly through the browser (without using the form), you would still get the error because `$_POST['name']` still isn't defined. – Sam Apr 15 '14 at 19:45
  • i understand, any idea how can i get the input from an input form and paste it to a variable for example in that example if someone would write 2 number how could i get em and paste em to 2 variables? – Alban Kaperi Apr 15 '14 at 19:49
  • Not sure if I understand what you mean. If you are saying they can write two values in a field (like `test@testing.com, abc@123.com` for email), then you can use [`explode(',', $_POST['email']);`](http://us1.php.net/explode). If you want to make new variables out of the `POST`ed data, you can always do `$name = $_POST['name'];`. – Sam Apr 15 '14 at 19:51
  • no i was thinking for example if i could get 2 number or 2 strings from those input forms, and than if possible to pass those number or strings, to 2 other varialbes for example$x and $y. – Alban Kaperi Apr 15 '14 at 20:00
  • Why can't you say `$x = $_POST['fieldA']; $y = $_POST['fieldB'];`? Sorry if I still don't understand your question – Sam Apr 15 '14 at 20:01
  • sory i just saw that what u porposed $name = $_POST['name']; it worked – Alban Kaperi Apr 15 '14 at 20:02
  • 1
    thank you very much you really clarified my mind with those concepts :) – Alban Kaperi Apr 15 '14 at 20:03