0

I'm using a contact form on my website and I'm trying to validate the user input using PHP. The problem with it is that although it will validate the input alright, if you then enter correct details and hit send, it doesn't do anything. I think this is because it's not doing the second part of the if statement after an error has been entered, but I'm not sure how to fix it. Below is the code I'm using so far.

    <?php
error_reporting(E_ALL ^ E_NOTICE);

function fix_string($var)
{
    if(get_magic_quotes_gpc()) $var = stripslashes($var);
    $var = strip_tags($var);
    return $var;
}

{
    $details = array('name' => fix_string($_POST['name']),
            'email' => fix_string($_POST['email']),
            'number' => fix_string($_POST['number']),
            'message' => fix_string($_POST['message']));
}

$send =  $_POST['send'];
$message = "";
$email = $details['email'];

 foreach ($details as $field => $detail)
    $message .= ucfirst($field) . ": " . $detail . "\r\n";

$to = "[email protected]";
$subject = "Website contact form";
$message = wordwrap($message, 70, "\r\n");
$headers = 'From: ' .$email . "\r\n" .
    'Reply-To: ' .$email . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

function trim_value(&$value)
{
    $value = trim($value);
}

array_walk($details, 'trim_value');

if ($send)
{
    foreach ($details as $field => $detail)
    {
        if (empty($detail) && $field!='number')
            echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";
    }
    if (!is_numeric($details['number']))
        echo "<p class='error'>Please enter a valid telephone number</p>";
}
    else
    {
        mail($to, $subject, $message, $headers);
        echo "<p class='success'>Thank you for your message, you will receive a response shortly.</p>";
    }

?>

<div id="contactform">
<form action="" method="post">
<fieldset>

<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" required />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" required />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" required></textarea>

<p class="small"><span class="star">*</span>&nbsp; Denotes a required field </p>

<input type="submit" id="send" name="send" value="Send" />

</fieldset>
</form>

I tried changing it to a while loop and just doing the validation until it is valid and then sending the email, however my attempts at this didn't work either.

1 Answer 1

1

Could you try change your if ($send) {} block to the following and test?

if ($send) {
    $bHasError = false;
    foreach ($details as $field => $detail) {
        if (empty($detail) && $field!='number') {
            echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";
            $bHasError = true;
        }
    }
    if (!is_numeric($details['number'])) {
        echo "<p class='error'>Please enter a valid telephone number</p>";
        $bHasError = true;
    }

    if (!$bHasError) {
        mail($to, $subject, $message, $headers);
        echo "<p class='success'>Thank you for your message, you will receive a response shortly.</p>";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it worked. I tried something similar, but I set $bhaserror outside of the if statement and used elseif and it didn't work. Such a simple solution, thanks!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.