kevin seda kevin seda - 5 months ago 7
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

reset();
but it didn't work.

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

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


$to = "flash1996mph@hotmail.com";
$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";
?>


and

$('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;
});

$.ajax({
url: url,
type:method,
data: data,
success: function(response) {
$('#success-message').html(response).show();
setTimeout(function() {
$('#success-message').fadeOut('slow');
}, 2000);
}
});
return false;
});

Answer

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

$(':input','form.subscribe')
 .not(':button, :submit, :reset, :hidden')
 .val('') .removeAttr('checked')
 .removeAttr('selected');
Comments