January 26, 20179 yr Hi, I'm using materialize.css (http://materializecss.com/modals.html) to display a modal/pop-up Form on page load. I have the modal displaying fine and submitting data via PHP. However, I want the pop-up to just close after submit instead of the user being redirected to another page. I've tried having it redirect back to the current page but this re-opens the pop-up again. Below is my PHP: <?php $email_to = "mail@domain.co.uk"; $name = $_POST["name"]; $email_from = $_POST["email"]; $tel = $_POST["tel"]; $message = $_POST["message"]; $email_subject = "Feedback from website"; $headers = "From: " . $email_from . "\n"; $headers .= "Reply-To: " . $email_from . "\n"; $message = "Name: ". $name . "\r\nTel: " . $tel . "\r\nMessage: " . $message; ini_set("sendmail_from", $email_from); $sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from); if ($sent) { header("Location: http://domain.co.uk/test.html"); } else { echo "There has been an error sending your comments. Please try later."; } ?> Any help greatly appreciated. Wasn't sure whether to post in this section or JS so please move if in the wrong place. Edited January 26, 20179 yr by AndyDesigns
January 27, 20179 yr I think you are redirecting your user on that page where your popup is located. instead of redirecting to popup page try to redirecting to the main page where you have simple page content. i mean to say try to separate your popup page and redirection page when you want to redirect your user. Hope it helps...
January 27, 20179 yr Author Thats my point though, I don't want to redirect the user at all. I just want them to be able to press submit and then stay on the current page.
January 27, 20179 yr You will need to validate and submit the form using Ajax and then fire a callback that closes the modal on success.Something like this, but you can use vanilla JS if you don't want to include jQuery, you'll have to write that yourself, though. The callback for the modal is: $('#modal1').modal('close');
January 27, 20179 yr Author You will need to validate and submit the form using Ajax and then fire a callback that closes the modal on success. Something like this, but you can use vanilla JS if you don't want to include jQuery, you'll have to write that yourself, though. The callback for the modal is: $('#modal1').modal('close'); Thanks I thought Ajax may be the only way. I'll look into it although I'm not every familiar with it.
January 27, 20179 yr Author Unfortunately that tutorial doesnt include actually sending the form data to a recipient mail in the PHP. I've tried finding other AJAX Form tutorials but they either don't show you how to process the form data via PHP or dont include a modal. If anyone can point me in the right direction it would be appreciated.
January 29, 20179 yr Unfortunately that tutorial doesnt include actually sending the form data to a recipient mail in the PHP. I've tried finding other AJAX Form tutorials but they either don't show you how to process the form data via PHP or dont include a modal. If anyone can point me in the right direction it would be appreciated. In the process.php file, there's a section where you can place the logic if the form validates. You don't have to use this tutorial but the same principles apply.
Create an account or sign in to comment