2

I am new to php and I made a contact form but i get a

Notice: Undefined index: name in C:\xampp\htdocs\portfolio\portfolio\html\contact.php

for the lines $_POST - Name, email, message and human.

How can I get take the error away??

                                    $name = $_POST['name'];
                                    $email = $_POST['email'];
                                    $message = $_POST['message'];
                                    $from = 'From:'; 
                                    $to = '86376@ict-idcollege.nl'; 
                                    $subject = 'Hello';
                                    $human = $_POST['human'];

                                    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
                                        if (isset($_POST['submit'])) {
                                            if ($name != '' && $email != '') {
                                                if ($human == '4') {                 
                                                    if (mail ($to, $subject, $body, $from)) { 
                                                    echo '<p>Your message has been sent!</p>';
                                                } else { 
                                                    echo '<p>Something went wrong, go back and try again!</p>'; 
                                                } 
                                            } else if ($_POST['submit'] && $human != '4') {
                                                echo '<p>You answered the anti-spam question incorrectly!</p>';
                                            }
                                            } else {
                                                echo '<p>You need to fill in all required fields!!</p>';
                                            }
                                        }
                                ?>
                                <form method="post" action="contact.php">

                                    <label>Name</label>
                                    <input name="name" placeholder="Type Here">

                                    <label>Email</label>
                                    <input name="email" type="email" placeholder="Type Here">

                                    <label>Message</label>
                                    <textarea name="message" placeholder="Type Here"></textarea>

                                    <label>*Wat is 2+2? (Anti-spam)</label>
                                    <input name="human" placeholder="Type Here">

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

                                </form>
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126

3 Answers3

3

By using isset or !empty:

<?php
    $name    = (isset($_POST['name'])    ? $_POST['name']    : '');
    $email   = (isset($_POST['email'])   ? $_POST['email']   : '');
    $message = (isset($_POST['message']) ? $_POST['message'] : '');
?>
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
1

Just Make Changes this in your code :

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

$from = 'From:';
$to = '86376@ict-idcollege.nl';
$subject = 'Hello';

if (isset ($_POST['human'])) {
  $human = $_POST['human'];
}
Bora
  • 10,529
  • 5
  • 43
  • 73
Harshal
  • 3,562
  • 9
  • 36
  • 65
0

place the code you have above inside a test block as follows

 if (isset($_POST['submit'])) {
   $name = $_POST['name'];
   $email = $_POST['email'];
  ...
  until the end of ?>

this is because when ever the page loads without the form submission. (because you have the control logic and display logic in the same file) it executes the code on the top.

DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • I realised what I did and as you said i had to move the if (isset($_POST['submit'])) { –  Aug 20 '13 at 10:00