weixin_33726313 2016-06-28 07:43 采纳率: 0%
浏览 57

提交后重置表格

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 
 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;
});
  • 写回答

3条回答 默认 最新

  • weixin_33676492 2016-06-28 07:54
    关注

    Put this button inside your <form>:

    <input type="reset" value="Reset Form" id="resetButton" >
    

    and make a click on this button whenever you find error in return like this,

    $("#resetButton").click();
    
    评论

报告相同问题?