kevin seda kevin seda - 1 year ago 64
PHP Question

Resetting a form after submit

I have a form which if the email is wrong gives me an error message while i stay on the same page, this is possible because i used ajax. Now I'm trying to achieve when i submit the form and the return is an error that the forms reset.

I tried

but it didn't work.

header("Refresh:7; url=contact.php");
$email = $_POST['subscribefield'];

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Dit emailadres klopt niet";

$to = "";
$subject = "Abonee voor de nieuwsbrief";
$body = "$email \n Heeft zich aangemeld voor de nieuwsbrief";

mail($to, $subject, $body);
echo "U heeft zich zojuist aangemeld voor de vandenberg nieuwsbrief";


$('form.subscribe').on('submit', function() {
var that = $(this),
url = that.attr('action'),
method = that.attr('method'),
data = {};

that.find('[name]').each(function(index, value) {
var that = $(this),
name = that.attr('name'),
value = that.val();

data[name] = value;

url: url,
data: data,
success: function(response) {
setTimeout(function() {
}, 2000);
return false;

Answer Source

If what you really want to do is to empty the form values try:

 .not(':button, :submit, :reset, :hidden')
 .val('') .removeAttr('checked')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download