简体   繁体   中英

“Message send” confirmation popup with PHP and JS

I am just started with PHP and have a rather simple problem I can't seem to figure out. I have set up a basic PHP script with will send me the content from my sites contact page. The script itself is fine - and so is the validator. Now what I am trying to achieve is getting a simple popup (similar to an alert function in javascript). Here is my try:

if ($valid) {
        //*isUTF8($subject);
        //*isUTF8($formcontent);
        sendMail();
        $body = $successMarkup . $backMarkup;
        $title = "Form sent";
        @header("location:formsent.php");
    } else {
        $body = $errorMarkup . $errorMarkupEnd . $backMarkup;
        $title = "Form errors";
    }

The file formsent.php I am refering to here only includes basic html markup as well as an javascript alert which is executed as soon as you open the page:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Contact Success</title>
</head>

<body>
<script language="Javascript">
<!--
alert ("Thank you for your message! I will come back to you as soon as possible!")
//-->
</script>
</body>
</html>

Here my question: After I send the filled out contactsheet via the send button I get a popup with the message shown above - BUT to do so it leaves the actual page I am on and shows me only a white screen.

How can I get that popup message implemented without leaving the page I am on?

Try using this one: http://jquery.malsup.com/form/ with jQuery to make an Ajax form submit simple. The examples, shown on that page are quite enough to implement your type of a story. just put:

<script>
$(document).ready(function(){ 
  $('form#form_id').ajaxForm(
     function(data){ 
       alert(data); 
     }
    )
})
</script>

And so everything you need to do in your sendmail script is to echo the needed message, no redirecting required.

header("Location: <url>") results in a proper redirect. You need to use Ajax here to send the data (when the user submits it), receive the contents of the popup box and then display it.

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