简体   繁体   English

在同一页面上提交表格和成功消息?

[英]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. 因此,现在我有了一个基本的contact.html,其中设置了php脚本,如果我的网站上的某人与我联系,则会通过电子邮件向我发送电子邮件。 When they click submit, it takes them to contact_sent.php, which is a blank page. 当他们单击提交时,会将他们带到contact_sent.php,这是一个空白页。 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. 我希望这样做,以便当他们单击提交时,将它们保留在同一页面上,并显示SweetAlert的成功消息,然后清除表单,以使他们不会向“发送消息”按钮发送垃圾邮件。

<?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. alert是javascript代码。因此,您需要使用php脚本来生成该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. 您所描述的实质上是AJAX。 If you are using jquery you can use its $.ajax function on the button press and send the data via its data property. 如果使用的是jquery,则可以在按下按钮时使用其$ .ajax函数,并通过其data属性发送数据。 Based on your current PHP code it might look something like this: 根据您当前的PHP代码,它可能看起来像这样:

 $.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. 您可以在$ .ajax调用之前或之后,将表单清除代码和SweetAlert代码放在同一事件监听器中,以提交按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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