mewebs
asked on
jquery or json form stay on page when submitted
I have a form that uses Jquery to show a message for
*field required error message
I am trying to get it to show a success message if the form is submitted on the same page.
I know json can be used, but do not know where to start with json.
The form submits as long as the req fields are filled in.
Does anyone know how I can modify this code to show the "success" div if all the "req" fields are filled out just like the ".req" shows if the fields are empty.
** notes
in the php, I currently redirect to thanks.php for valid and spam.php for honeypot filled in
My brain is fried today.
Thanks
form.js
process.php
form.html
*field required error message
I am trying to get it to show a success message if the form is submitted on the same page.
I know json can be used, but do not know where to start with json.
The form submits as long as the req fields are filled in.
Does anyone know how I can modify this code to show the "success" div if all the "req" fields are filled out just like the ".req" shows if the fields are empty.
** notes
in the php, I currently redirect to thanks.php for valid and spam.php for honeypot filled in
My brain is fried today.
Thanks
form.js
$(function() {
function validateform() {
var valid = true;
$(".req").css("border","1px solid #ccc");
$(".req").each(function() {
if ( $(this).val() == "" || $(this).val().replace(/\s/g, '').length == 0 ) {
$(this).css("border","1px solided");$(".required").css("display","block");
valid = false;
}
});
return valid;
}
$("#submit").click( function() {
$('#myform').submit( validateform );
$('$name').submit();
});
});
process.php
<?php
// Clean up the input values
foreach ( $_POST as $key => $value ) {
if ( ini_get('magic_quotes_gpc') )
$_POST[$key] = stripslashes( $_POST[$key] );
$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}
// Honeypot don't send
if ( !empty($_POST["confirm_email"] ) ) {
header("location:spam.php");exit;
}
// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$confirm = $_POST["confirm_email"];
$phone = $_POST["phone"];
$message = $_POST["message"];
$mls = $_POST["mls"];
// Send the email *****************************
$to = "email@mywebsite.com";
$subject = "Website message: $name";
$message = "MLS Listing:\n$mls\n\nFrom:\n$name\n\nEmail:\n$email\n\nPhone:\n$phone\n\nMessage:\n$message";
$headers = "From: $email";
mail($to, $subject, $message, $headers);
echo("<script>location.href = 'thanks.php';</script>");
?>
form.html
<!--form-->
<form class="myform" id="myform" name="myform" action="process.php" method="post">
<div class="required">Red Highlights are required</div>
<div class="success">This is where the Succcess message is!</div>
<label>Name</label><input id="name" name="name" class="req" type="text"/>
<label>Email</label><input id="email" name="email" class="req" type="email" />
<label class="confirm_email">Confirm Email</label><input id="confirm_email" name="confirm_email" class="confirm_email" type="email" class="req2"/>
<label>Phone</label><input id="phone" name="phone" type="tel" />
<label>Message</label><textarea id="message" name="message" type="text" class="req"></textarea>
<input id="mls" name="mls" class="mls" value="MLS1234567" />
<input type="submit" id="submit" value="Submit Message" />
</form>
<!--form-end-->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="form.js"></script>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks.
You should serialize the form (to json) and perform an Ajax POST with the information, handling the success or error callbacks and behave as you wish. In your case showing a success message and probably redirecting out of there after right?
So the code for the save button (normal button, not a submit) click should be something like:
Open in new window