简体   繁体   中英

Submitting form and success message on same page?

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.com";
    $from_add="form@***********.net";

    $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.

alert 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.com";
$from_add = "form@***********.net";

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM