Niels
Niels

Reputation: 425

Php contact form errors

I'm building a php contact form that checks if some fields are filled it or not (no ajax). The form is not working for 100%. I will try to explain. When I submit my form, The user navigates to http://mydomain.com/send.php There is receive my error messages (if there are any). When I refresh the send.php page I receive the following errors: Notice: Undefined index: name in /Library/WebServer/Documents/~aledvertising/send.php

Here is my code

html form

<form method="post" action="send.php">
    <div id="form-top">

    </div>
    <div id="form-left">  
<label>Naam:<span class="star">*</span></label>
    <input name="name" placeholder="Uw naam">


    <label>Email:<span class="star">*</span></label>
    <input name="email" type="email" placeholder="Uw email">

    <label>Hoeveel is 2+2? (Anti-spam)<span class="star">*</span></label>
<input name="human" placeholder="Uw antwoord">

    </div>
    <div id="form-right">
    <label>Uw bericht:<span class="star">*</span></label>
    <textarea name="message" placeholder="Uw bericht"></textarea>
    </div>
        <input id="submit" name="submit" type="submit" value="Verzenden">
</form>

SEND.PHP

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: ' . $_POST['email']; 
    $to  = '[email protected]' . ', '; 
    $to .= $_POST['email'];
    $subject = 'Uw vraag op www.aledvertising.be';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\nMessage:\n $message";
 ?>
 <?php
if ($_POST['submit']) {
    if ($name != '' && $email != '' && $message != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
        } else { 
            echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
    }
    } else {
        echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
    }
}
    ?>

Can somebody please help me to get a full working form? Thank you

Upvotes: 0

Views: 1825

Answers (2)

user2195741
user2195741

Reputation:

You should see isset method from php.

<?php
if(isset($_POST["name"]))
{
//code here

}
?>

The problem is that when you reload the page, the server side code looks for the index name and it does not find it because name does not have any value yet. The value is received once you submit the page. So that's why the error is there.

update

The error is that when you submit the form, there are some fields that may be send empty, so once that the post values goes to send.php there are $_POST that will be expecting values. if some of them do not recive does values, it will tell you: "Hey, i do not have values, so i im null". That why you have to check that the fields are all filled or make a validation on server side.

<?php
if(isset($_POST["name"],$_POST["last_name"],etc,etc))
{
//if everything ok, ill go on.
}
else{
//if there are empty field, go back
}

?>

UPDATE 2

if (isset($_POST['name'],$_POST['email'],$_POST['message'])){

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: ' . $_POST['email']; 
    $to  = '[email protected]' . ', '; 
    $to .= $_POST['email'];
    $subject = 'Uw vraag op www.aledvertising.be';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\nMessage:\n $message";
    if ($name != '' && $email != '' && $message != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
        } else { 
            echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
    }
    } else {
        echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
    }
}

Upvotes: 3

user2119249
user2119249

Reputation:

<?php
if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: ' . $_POST['email']; 
    $to  = '[email protected]' . ', '; 
    $to .= $_POST['email'];
    $subject = 'Uw vraag op www.aledvertising.be';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\nMessage:\n $message";
    if ($name != '' && $email != '' && $message != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
        } else { 
            echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
    }
    } else {
        echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
    }
}
?>

setting the isset() function and then declaring the variables worked for me, tested it in my server, not giving any errors and form is submitted successfully...

please check it once...

Upvotes: 0

Related Questions