1
votes

I have a contact form field that uses AJAX to submit the form, and PHP.

I have a small problem. I keep receiving an error that states,"the request was successful, but the form was not filled out correctly." This applies to the 'email' field. The form does not get submitted, and a user has to refill out the form.

Somehow, any email address entered is not valid.

Does anyone know why that could be?

/* Email Variables */
$emailSubject = 'You have received an inquiry from your website';
$webMaster = '[email protected]';

/* Data Variables */
$first = Trim(stripslashes($_POST['First']));
$last = Trim(stripslashes($_POST['Last']));
$email = Trim(stripslashes($_POST['E-Mail']));
$message = Trim(stripslashes($_POST['Message']));
$tempt = $_POST['tempt'];
$tempt2 = $_POST['tempt2'];

if ($tempt == 'http://' && empty($tempt2)) {

    $error_message = '';
    $reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9­-]+\.[a-zA-Z.]{2,5}$/";

    if(!preg_match($reg_exp, $email)) {

        $error_message .= "<p>A valid email address is required.</p>";
    }
    if (empty($first)) {
        $error_message .= "<p>Please provide your first name.</p>";
    }
    if (empty($last)) {
        $error_message .= "<p>Please provide your last name.</p>";
    }
    if (empty($message)) {
        $error_message .= "<p>Comments box cannot be left empty.</p>";
    }

    if (!empty($error_message)) {

        $return['error'] = true;
        $return['msg'] = "<h3> The request was successful, but the form is not filled out correctly.</h3>".$error_message;
        echo json_encode($return);
        exit();

    } else {

        $return['error'] = false;
        $return['msg'] = "<p>Thank you for contacting us. We definitely value your feedback," .$first .".</p>";
        echo json_encode($return);
    }

} else {

        $return['error'] = true;
        $return['msg'] = "<h3>There was a problem while sending this form. Try it again.</h3>";
        echo json_encode($return);
}

HTML

    <form class="contact-me" action="mail/mailform.php" method="post" name="contact-me">

<div id="response"><!-----------------CONTAINS FORM ERROR MESSAGE--------------></div>
<input name="First" class="required" title="Your First Name is Required" id="first" autofocus placeholder="First Name" type="text" maxlength="15"></input><br>
<input name="Last"  title="Your Last Name is Required" id="last" placeholder="Last Name" type="text" maxlength="15"></input><br>
<input name="E-mail"  title="E-Mail Address is Required" id="email" placeholder="Email Address" type="email" maxlength="50"></input><br>
<textarea name="Message"  title="Comments are Required" placeholder="Comments" rows="1" cols="50" id="message" onKeyDown="textCounter(this.form.comments,this.form.countDisplay);"onKeyUp="textCounter(this.form.message,this.form.countDisplay);"></textarea><br>
<input type="hidden" name="tempt" id="tempt" value="http://" />
<input type="hidden" name="tempt2" id="tempt2" class="clear" value="" />
<input name="countDisplay" type="text" id="countdisplay" value="2000" size="4" maxlength="4" readonly></input><br>
<button class="button" ontouchstart="('touchstart');">SEND</button>
</form>
1
Would you care to share the information about what framework is this supposed to be? Or what specific page gives you this 'error statement'?Smuuf
This error occurs on a contact page I created. www.3elementsreview.com/contactMarlon Fowler
Why are you so sure it is the email that is giving the trouble? Maybe also show the HTML form?putvande
<textarea name="Message" title="Comments are Required" placeholder="Comments" rows="1" cols="50" id="message" onKeyDown="textCounter(this.form.comments,this.form.countDisplay);"onKeyUp="textCounter(this.form.message,this.form.countDisplay);"></textarea><br> <input type="hidden" name="tempt" id="tempt" value="http://" /> <input type="hidden" name="tempt2" id="tempt2" class="clear" value="" /> <input name="countDisplay" type="text" id="countdisplay" value="2000" size="4" maxlength="4" readonly></input><br> <button class="button" ontouchstart="('touchstart');">SEND</button> </form>Marlon Fowler

1 Answers

0
votes

The problem is your email input name attribute.

Your input is:

<input name="E-mail" title="E-Mail Address is Required" id="email" placeholder="Email Address" type="email" maxlength="50">

and you're accessing it using $_POST['E-Mail'] (uppercase M). Note that array index are case sensitive.

Change that line to match your input's name as follows:

$email = Trim(stripslashes($_POST['E-mail']));