How to get a popup after submitting a form?

[SOLVED] Thanks to everyone who answered.

I have a PHP script that runs after a user submits a contact form. This script shows a message in the upper left corner, saying if it worked correctly or if it ran into a problem.

Now my question is: how can I put these messages in a popup? I know that there is JS, but I hardly know about it.

(Example: http://www.dylanvanheugten.nl/contact.php )


Php

$name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $from = 'From: Site Contact'; $to = ' info@dylanvanheugten.nl '; $subject = $_POST['subject']; $human = $_POST['human']; $body = "From: $name\n E-Mail: $email\n Message:\n $message"; if ($_POST['submit']) { if ($name != '' && $email != '' && $subject != '' && $message != '') { if ($human == '12') { if (mail ($to, $subject, $body, $from)) { echo '<p>Uw bericht is verzonden.<br/> U krijgt binnen 3 werkdagen een bericht terug.</p>'; } else { echo '<p>Er is iets mis gegaan tijdens het versturen van uw bericht.<br/> Probeert u alstublieft nogmaals.</p>'; } } else if ($_POST['submit'] && $human != '12') { echo '<p>U heeft de spam preventie som foutief beantwoord.</p>'; } } else { echo '<p>Alstublieft alle velden invullen.</p>'; } } 

HTML

 <form method="post" action="contact.php"> <label>Naam</label> <input name="name" placeholder="Naam"> <label>Email</label> <input name="email" type="email" placeholder="Email"> <label>Onderwerp</label> <input name="subject" placeholder="Onderwerp"> <label>Bericht</label> <textarea name="message" placeholder="Laat hier ook uw telefoonnummer achter als u telefonisch contact wilt."></textarea> <label><strong>*Spam preventie*</strong><br/>Wat is 11+1?</label> <input name="human" placeholder="11 + 1 ="> <input id="submit" name="submit" type="submit" value="Verzend"> <label><u>Alle velden zijn verplicht.</u></label> </form> 
+4
source share
5 answers

Include echo tags with <script> tags like this.

 echo '<script>alert("Alstublieft alle velden invullen");<script>'; 
+3
source

Just as you can print HTML with PHP, you can "print" javascript. Here is an example with a warning field:

 print("<script>window.alert('This is a javascript alert from PHP');</script>"); 

You can also put your PHP variables inside the statement if you need to.

0
source

You can just alert msg like,

 $msg=''; if ($_POST['submit']) { if ($name != '' && $email != '' && $subject != '' && $message != '') { if ($human == '12') { if (mail ($to, $subject, $body, $from)) { $msg='Uw bericht is verzonden.<br/> U krijgt binnen 3 werkdagen een bericht terug'; } else { $msg='Er is iets mis gegaan tijdens het versturen van uw bericht.<br/> Probeert u alstublieft nogmaals.'; } } else if ($_POST['submit'] && $human != '12') { $msg='U heeft de spam preventie som foutief beantwoord.'; } } else { $msg='Alstublieft alle velden invullen.'; } echo '<script> alert("'.$msg.'"); </script>'; // you can replace the above javscript code to any plugin like jquery ui modal box } 
0
source

Add some javascript to the top of your html markup, where you define the display method of the popup. Something like that:

 <script type="text/javascript"> function displayPopup() { alert("Form submitted!"); } </script> 

Then in your PHP, when the form has been submitted, just call the displayPopup () method in the script tag.

You can optimize the approach above, but it should give you the opportunity to start.

0
source

Save the error message in a variable and just echo in the desired div.

 if ($_POST['submit']) { if ($name != '' && $email != '' && $subject != '' && $message != '') { if ($human == '12') { if (mail ($to, $subject, $body, $from)) { $message = '<p>Uw bericht is verzonden.<br/> U krijgt binnen 3 werkdagen een bericht terug.</p>'; } else { $message = '<p>Er is iets mis gegaan tijdens het versturen van uw bericht.<br/> Probeert u alstublieft nogmaals.</p>'; } } else if ($_POST['submit'] && $human != '12') { $message = '<p>U heeft de spam preventie som foutief beantwoord.</p>'; } } else { $message = '<p>Alstublieft alle velden invullen.</p>'; } } 

And in your HTML you can create a and display an error inside it:

 <div id="errorMessage"> <?php echo $message; ?> </div> 
0
source

Source: https://habr.com/ru/post/1495950/


All Articles