Skip to content Skip to sidebar Skip to footer

Php/javascript Email Form And Validation

So I have the JavaScript code which validates the first name field, I am adding more validation once I have worked this out. So basically if the field is not filled in, return fals

Solution 1:

In your form id="email" is conflicting with input id="email"

You can use HTML5 attributes for form validation (in HTML5 supported browsers)

http://www.the-art-of-web.com/html/html5-form-validation/#.UnDpBHMW3eU

Code

<?phpif(isset($_POST['submit'])) {
    print_r($_POST);
    die;
    $email_to = "emailaddress";
    $email_subject = "Mint Makeup & Beauty Enquiry";        

    $fname = $_POST['fname']; // required$lname = $_POST['lname']; // required$message = $_POST['message']; // required$email_from = $_POST['email']; // required// create email content$email_content = "From:"." ".$fname." ".$lname."\n"."Email:"." ".$email_from."\n"."Message:"." ".$message; 
    mail($email_to, $email_subject, $email_content);
}
?><!DOCTYPE html><htmllang="en"><head><scripttype="text/javascript">functionvalidateForm() { 
    var error = false;
    var error_string = 'Please correct the following errors:\n\n';  
    if (document.getElementById('fname').value == ""){
        error_string += "--> First name must be filled out.\n";
        error = true;
    } 
    if (document.getElementById('lname').value == ""){
        error_string += "--> Last name must be filled out.\n";
        error = true;
    } 
    if (document.getElementById('email').value == ""){
        error_string += "--> Email must be filled out.\n";
        error = true;
    }    
    if(error){
        alert(error_string);
        returnfalse;
    }  else {
        returntrue;
    }
}
</script></head><body><formonsubmit="return validateForm(this)"action=""method="post" ><divid="form_fname"><labelfor="fname"><imgsrc="images/firstname.png"width="94"height="17"alt="first name" /></label><inputtype="text"name="fname"id="fname" /></div><divid="form_lname"><labelfor="lname"><imgsrc="images/lastname.png"width="89"height="17"alt="last name" /></label><inputtype="text"name="lname"id="lname" /></div><divid="form_email"><labelfor="email"><imgsrc="images/email.png"width="53"height="17"alt="email" /></label><inputtype="text"name="email"id="email" /></div><divid="form_message"><labelfor="Message"><imgsrc="images/message.png"width="77"height="17"alt="message" /></label><textareaname="message"id="message"cols="45"rows="5"></textarea></div><divid="form_submit"><inputtype="submit"name="submit"id="submit"value="Submit" /></div></form></body></html>

I just showed you how client side validation works and form is posting correctly.. You should always use server side validation..

Solution 2:

Try

document.forms["email"]["fname"].value

instead

document.forms['email'].fname.value

Also you can get field this way:

document.getElementById('fname').value

Post a Comment for "Php/javascript Email Form And Validation"