0

After 2 days of trying to fix my problem myself i gave up :( So will try to get help here. My problem is that xampp gives me this error :

Notice: Undefined index: name in C:\xampp\htdocs\portfolio\index.php on line 78 Notice: Undefined index: email in C:\xampp\htdocs\portfolio\index.php on line 79 Notice: Undefined index: message in C:\xampp\htdocs\portfolio\index.php on line 80 Notice: Undefined index: human in C:\xampp\htdocs\portfolio\index.php on line 84 Notice: Undefined index: submit in C:\xampp\htdocs\portfolio\index.php on line 88

I managed to find that i need define variables before using them, or check for existence,But my knowledge of PHP is in its very basics. Could someone help me with that?

<section id="contact">
<h3>Contact me :</h3>
<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Example'; 
    $to = '[email protected]'; 
    $subject = 'Hello';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_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="index.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>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">

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

</form>


</section>
2
  • You should probably lookup the isset() function. Commented Oct 24, 2013 at 13:08
  • Since there is no POST request, the variables such as $_POST['name'] is not defined, so XAMP gives that error. Commented Oct 24, 2013 at 13:09

2 Answers 2

0

Since there is no POST request, the variables such as $_POST['name'] is not available, so PHP gives that error. What you need to do is check if a $_POST request has been made, then define the variables :

<?php
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Example'; 
    $to = '[email protected]'; 
    $subject = 'Hello';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_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>';
    }
}
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for a quick response,changed the code, but error remains the same. Error shows up when accessing php page in Dreamweaver or when accessing page in localhost through xampp. When uploaded on web server, error disappears. Maybe problem is elsewhere?
Works like a charm! Thank you.Need to spend more time reading books because i don't understand what you did :( Thanks again!
0

Currently, your page is showing these messages because you are showing all errors and notices.

If you include the following at the top of your page, then you will not see messages for notices:

<?php error_reporting (E_ALL ^ E_NOTICE);  ?>

Please note, however, that this only hides the messages. It does not resolve the underlying problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.