Reputation: 120
This is a php file called sendmail.php that validates and sends an email from a contact form (which is included below)
The trouble I'm having is when the alert is clicked it redirects the page to sendemail.php because, I'm assuming, of form action=sendmail.php
How do I resolve this so it stays on the contact page and the user can pick up where they left off? I have tried using header like for the thank you page but to no avail!
if (isset($_POST["submit"])) {
$validate = validateInput($_POST["name"], $_POST["message"], $_POST["subject"]);
if ($validate==FALSE) {
$error_msg = "Please fill out all information";
echo '<script type="text/javascript">
alert("'.$error_msg.'");
</script>';
} else {
$mailcheck = spamcheck($_POST["email"]);
if ($mailcheck==FALSE) {
$error_msg = "Invalid email address";
echo '<script type="text/javascript">
alert("'.$error_msg.'");
</script>';
} else {
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$subject = $_REQUEST['subject'] ;
mail( "[email protected]", $subject, $message, "From: $email" );
header( "Location: http://www.thankyou.html" );
}
}
}
<form method="post" action="sendmail.php">
<input type="text" name="name" maxlength="50" placeholder="Name" class="contact-standard" /></br></br>
<input type="email" name="email" placeholder="Email" class="contact-standard"></input></br></br>
<input type="text" name="subject" placeholder="Subject" class="contact-standard"></input></br></br>
<textarea name="message" placeholder="Message" class="contact-message"></textarea></br></br>
<input type="submit" name="submit" value="Send Message" class="contact-submit"></input></br></br>
</form>
Upvotes: 1
Views: 9273
Reputation: 12505
Validate form with jQuery BEFORE user submits form, make the "Thank you" happen on same page on successful submit. This way you just use PHP to process email send.
<?php
if(isset($_POST["submit"])) {
$email = $_POST['email'] ;
$message = strip_tags($_POST['message']);
$subject = strip_tags($_POST['subject']);
if(mail("[email protected]", $subject, $message, "From: $email")) { ?>
<h3>Thank you, come again!</h3>
<?php }
else { ?>
<h3>An error occurred. My bad!</h3>
<?php }
} ?>
<form method="post" action="contact.php" id="emailer">
<input type="text" name="name" maxlength="50" placeholder="Name" class="contact-standard" /></br></br>
<input type="email" name="email" placeholder="Email" class="contact-standard"></input></br></br>
<input type="text" name="subject" placeholder="Subject" class="contact-standard"></input></br></br>
<textarea name="message" placeholder="Message" class="contact-message"></textarea></br></br>
<input type="submit" name="submit" value="Send Message" class="contact-submit"></input></br></br>
</form>
<style>
.error { color: red; }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script>
$(document).ready(function() {
// On submit, validate below fields.
$("#emailer").validate({
rules: {
name: "required",
email: {
required: true,
email: true
},
subject: "required",
message: "required"
},
messages: {
name: "required",
email: {
required: "Required",
email: "must be valid email"
},
subject: "required",
message: "required"
}
});
});
</script>
Upvotes: 1
Reputation: 2509
Try this and Read the comment. Comment: Add this line each time the validation failed window.location= "Your-Form-File-Name.php";
if (isset($_POST["submit"])) {
$validate = validateInput($_POST["name"], $_POST["message"], $_POST["subject"]);
if ($validate==FALSE) {
$error_msg = "Please fill out all information";
echo '<script type="text/javascript">
alert("'.$error_msg.'");
window.location= "Your-Form-File-Name.php"; **//This line added by me**
****
</script>';
} else {
$mailcheck = spamcheck($_POST["email"]);
if ($mailcheck==FALSE) {
$error_msg = "Invalid email address";
echo '<script type="text/javascript">
alert("'.$error_msg.'");
window.location= "Your-Form-File-Name.php"; ////This line added by me
**//Added above line**
</script>';
} else {
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$subject = $_REQUEST['subject'] ;
mail( "[email protected]", $subject, $message, "From: $email" );
header( "Location: http://www.thankyou.html" );
}
}
}
Upvotes: 1
Reputation: 464
This is sendmail.php, right? IF you want to redirect to contact.php just header() to there. You set form action to sendmail.php that means you set to this file too, it will do the same if you delete action prop from form tag. Just change your header value to contact.php or
print 'location.href="'.$url_to_contact_php.'"';
Upvotes: 0
Reputation: 14624
I don't have experience with php but i think you need to returen false when form is not validated.
if ($validate==FALSE) {
$error_msg = "Please fill out all information";
echo '<script type="text/javascript">
alert("'.$error_msg.'");
</script>';
return false;//Form is not validated stop from being submitted.
}
Upvotes: 1