-2

I am trying to follow a PHP contact form tutorial from here! Seems pretty simple and I'm completely new to PHP. However, when running my project, I get so many "Notice" messages and errors!

I managed to fix many of them, by surrounding most variables in issets

if(isset($_POST['name'])){ $name = $_POST['name'];}
if(isset($_POST['email'])){$email = $_POST['email'];}
if(isset($_POST['phone'])){$phone = $_POST['phone'];}
if(isset($_POST['message'])){$message = $_POST['message'];}
if(isset($_POST['human'])){$human = intval($_POST['human']);}
if(isset($_POST['from'])){$from = 'Página Web';}
if(isset($_POST['to'])){$to = 'bla@bla.com';}
if(isset($_POST['name'])){$subject = 'Mensaje de Página Web';}
if(isset($_POST['name'])){$body = "De: $name\n Teléfono: $phone\n E-Mail:   $email\n Mensaje:\n $message";}

However, I still get these pesky notifications:

Screenshot

Line 16 is:

if ($_POST["submit"]) {

And the rest are for example found in lines like this:

<div class="col-sm-10">
        <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
        <?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>

even though these variables are declared on the top.

How can I fix the top notice and the "undeclared variable" errors? I've tried looking up other tutorials, but they seem to do the same, ignoring the issets (and making my Netbeans upset and full of warnings!) and using the same syntax.

Help is greatly appreciated.

Kotoriii
  • 9
  • 1
  • 6

1 Answers1

0

The first time you load the form, $_POST will not have anything populated. Try changing

if ($_POST["submit"]) {

to

if (isset($_POST["submit"])) {

to determine if the form was in fact submitted and continue accordingly.

Drakes
  • 23,254
  • 3
  • 51
  • 94
  • I'm not a smart man, I surrounded everything with issets, but not that specific if. Thank you so much, that did the trick. And thank you all the others for helping out too! I'll check your comment as answer as soon as the site allows me to! :) Thank you so much – Kotoriii Jun 14 '15 at 23:35
  • We've **all** been there. Trust me. Best wishes! – Drakes Jun 14 '15 at 23:35