简体   繁体   中英

PHP Contact Form Modal Box Don't Close on Error

I have a modal box that has a PHP form on it. When the form is submitted it closes the modal box and redirects back to the page. I want the modal box to stay open if there is an error.

It is ok if modal box closes when it is a successful submission because it redirects to my success url. This is only an issue if there is an error on the form. I am using php to check to see if there is a blank field and using the built in email validator.

Also worth mentioning if there is an error on the form and it closes the modal box if you open it back up you see the errors since it wasn't successfully submitted.

I am using this for my form action

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">

I can post more of the code if necessary.

Is there anything that strikes anyone that could be a simple fix for this?

You can do Client Side (JavaScript/Jquery) validation. Which can be done by using onSubmit='return validateMyForm()' on the form element.

But, its good to have server side validation, so use AJAX for that.

Don't submit your form, use <input type="button"> instead of <input type="submit">

Submit your form using $.post or $.get or $.ajax, get PHP errors as return and show errors on the Modal Box

To do this you can use ajax...

Tutorial

I am using php to check to see if there is a blank field and using the built in email validator.

While it's good to perform validation on the server, you should deal with validation on the client side. Use javascript to validate so you don't hit the server and your modal screen won't close.

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