So right now I have a basic contact.html with a php script set to email me if someone on my website contacts me. When they click submit, it takes them to contact_sent.php, which is a blank page. I want it so that when they click the submit, it keeps them on the same page, as well as display a success message from SweetAlert, and then clear the form so they can't spam the "Send Message" button.
<?php
if ($_POST['submit']) {
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['msg'];
$to_add="***********@gmail";
$from_add="form@***********";
$email_message="<br /><br /><b>Name:</b><br />$name <br />";
$email_message.="<br /><br /><b>Reply Email:</b><br />$email <br />";
$email_message.="<br /><br /><b>Regarding:</b><br />$subject <br />";
$email_message.="<br /><br /><b>Message:</b><br />$message <br />";
$headers="From: $from_add \r\n";
$headers.="Reply-To: $from_add \r\n";
$headers.="Return-Path: $from_add\r\n";
$headers.="X-Mailer: PHP \r\n";
$headers.="MIME-Version: 1.0\r\n";
$headers.="Content-Type: text/html;\r\n";
mail($to_add,$subject,$email_message,$headers);
}
alert("Sent!");
?>
Edit: The alert at the end doesn't work.
So right now I have a basic contact.html with a php script set to email me if someone on my website contacts me. When they click submit, it takes them to contact_sent.php, which is a blank page. I want it so that when they click the submit, it keeps them on the same page, as well as display a success message from SweetAlert, and then clear the form so they can't spam the "Send Message" button.
<?php
if ($_POST['submit']) {
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['msg'];
$to_add="***********@gmail.";
$from_add="form@***********";
$email_message="<br /><br /><b>Name:</b><br />$name <br />";
$email_message.="<br /><br /><b>Reply Email:</b><br />$email <br />";
$email_message.="<br /><br /><b>Regarding:</b><br />$subject <br />";
$email_message.="<br /><br /><b>Message:</b><br />$message <br />";
$headers="From: $from_add \r\n";
$headers.="Reply-To: $from_add \r\n";
$headers.="Return-Path: $from_add\r\n";
$headers.="X-Mailer: PHP \r\n";
$headers.="MIME-Version: 1.0\r\n";
$headers.="Content-Type: text/html;\r\n";
mail($to_add,$subject,$email_message,$headers);
}
alert("Sent!");
?>
Edit: The alert at the end doesn't work.
Share Improve this question edited May 5, 2015 at 10:02 Mystic4TW asked May 3, 2015 at 21:13 Mystic4TWMystic4TW 171 silver badge5 bronze badges 3- 1 alert is a js function not a php one – user557846 Commented May 3, 2015 at 21:20
- @Dagon Disregard that. – Mystic4TW Commented May 3, 2015 at 21:24
- submit form to same page, separate form from response. – user557846 Commented May 3, 2015 at 21:38
3 Answers
Reset to default 3alert is javascript code.So you need your php script to produce that javascipt. Hope it will help you
<?php
if($_POST['submit']) {
//your other codes here
mail($to_add,$subject,$email_message,$headers);
//becareful keep the whole javascript code on mail function's success.
//I just added how to do It.It will alert too if your mail function return false.
?>
<script type="text/javascript">
alert("Sent!");
</script>
<?php
}
?>
<?
if ($_POST['submit'])
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['msg'];
$to_add = "***********@gmail.";
$from_add = "form@***********";
$email_message = "<br /><br /><b>Name:</b><br />$name <br />";
$email_message .= "<br /><br /><b>Reply Email:</b><br />$email <br />";
$email_message .= "<br /><br /><b>Regarding:</b><br />$subject <br />";
$email_message .= "<br /><br /><b>Message:</b><br />$message <br />";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;\r\n";
mail($to_add,$subject,$email_message,$headers);
//echo js alert
}else{ // form not submitted yet
//display form code
}
?>
What you're describing is essentially what AJAX is. If you are using jquery you can use its $.ajax function on the button press and send the data via its data property. Based on your current PHP code it might look something like this:
$.ajax({ url: '/route/to/php/function',
data: {name = nameVal,
email = emailVal,
subject = subjectVal,
message = messageVal},
type: 'post',
success: function(output) {
//do whatever after the POST is successful
}
});
You'd put the form clearing code and SweetAlert code in the same event listener for the submit button, either before or after the $.ajax call.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745365406a4624553.html
评论列表(0条)