0

I've been working my way up on validating a form and got the JS and PHP validation to work but I am still having a hard time adding ajax(with or without JQUERY) to submit to the php file and return a success message.

My form with CSS and JS validation:

<html>
<head>
<style type="text/css">
#nameerror {
color:red;
}
#emailerror{
color:red;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">

function Validate()  {

var email = document.forms['form']['email'].value;
var atpos = email.indexOf('@');
var dotpos = email.lastIndexOf('.');
var name = document.forms['form']['name'].value;

if (name == null || name == ""){
document.getElementById('nameerror').innerHTML="Please enter your name";
return false;
} else if (atpos < 1 || dotpos < atpos+2 || dotpos+2 >= email.length) {

document.getElementById('emailerror').innerHTML="Please enter a valid email";
return false;
} else {

}   
}

</script>


</head>
<body>
<div>Sign Up</div>
    <form name="form" action="form_validation.php" id="form" onsubmit="return Validate();" method = 'post'> 
<label>Name:</label><br/>
<input id='name' type="text" name='name' /><br/>
<span id="nameerror"></span><br/>
<label>Email:</label><br/>
<input type='text' name='email' id = 'email'/> <br/>
<span id= "emailerror"></span><br/>
<label>Comments:</label><br/>
<textarea name='comments' id ='comments'></textarea><br/>
<span id="comerror"></span>
<input type="submit" name="submit" value="Submit"/>  
<span id="success" style="display:none">Your message has been sent successfully.</span>

</form>  
</body>
</html>

And this is form_validation.php:

<?php

if(isset($_POST['submit'])){
if($_POST['name']){
    $name = $_POST['name'];
}
if(!preg_match('/^[a-zA-Z\s]+$/', $name)){
    echo "Name can only contain letters.";
    return false;
} else {
        echo "Name accepted";
}   

if($_POST['email']){
    $email = $_POST['email'];
}
$pattern = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
if(!preg_match($pattern, $email)){
    echo "Please enter a valid email address.";
    return false;
} else {
    echo "Email Valid";
}
if($_POST['comments']){
    $comments = $_POST['comments'];     
}
if (strlen($comments) > 100){
    echo "please enter less than 100 characters.";
    return false;
} 
}

?>

thanks for the help!

1
  • there's a big hole where you need code. Commented Feb 7, 2013 at 23:53

1 Answer 1

0
$('form').on('submit',function() {

$.ajax({
       url: 'form_validation.php',
       data: $(this).serialize(),
       type: 'POST'
            }).done(function(data){

                  // you can append a success message to your document here

            });


                                  });  
Sign up to request clarification or add additional context in comments.

1 Comment

I've added it to my code with a success message to display in a span tag but all it does is reroute to my form_validation.php. How can I just stay on the form page?

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.