0

I have a form which I am submitting to my database, the form includes Jquery Validate plugin and ajax to submit.

The issue I am having is that after I click submit the form does not clear, it updates to database etc but I am just trying to clear the form and css highlight on the input field so someone could add a new record. Any help please?

CODE:

$(document).ready(function () {
    $.validator.addMethod("time", function (value, element) {
        return this.optional(element) || /^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/i.test(value);
    }, "Please enter a valid time.");

    $("#newform").validate({

        //validation
        debug: false,
        rules: {
            Name: {
                required: true,
                minlength: 3,
            },

            Surname: {
                required: true,
            },

        },

        submitHandler: function (form) {
            $.post('process.php', $("#newform").serialize(), function (data) {
                $('#results').html(data);
                $('#newform').reset();
            });
        }
    });
});

HTML:

<form name="newform" id="newform" action="" method="POST">  



    <p>Name:</p>
        <input type="text" id="pName" name="sName" /><br />

    <p>Surname:</p>
        <input type="date" id="pSName" name="pSName" /><br />


    <input type="submit" name="submit" value="Submit"> 
</form>

<div id="results"><div>

3 Answers3

2

You can use the following code to clear your form:

$('#newform').reset();

To focus on a specific <input> then you can use this after the reset() call:

$('input[name="sName"]').focus();
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
0

Try the following:

submitHandler: function (form) {
    var jqxhr = $.post('process.php', $("#newform").serialize(), function (data) {
        $('#results').html(data);
    });

    jqxhr.done(function() {
        $('#newform')[0].reset();
    });
}
lshettyl
  • 8,166
  • 4
  • 25
  • 31
-2
$('#newform').reset();

The above code will do. But reading your comments I see that you will have to make the ajax call synchronous. Because if its asynchronous, you will clear the form before the submit request is actually processed on server side. That's the reason you see a clear form before process.php

In the ajax call pass async also in the object parameter-

{url:'xyz.com',async:false}
halkujabra
  • 2,844
  • 3
  • 25
  • 35
  • How would I go about doing that? Thanks. – user3406032 Mar 11 '14 at 12:40
  • Can I not clear the form after the request is processed on server side? – user3406032 Mar 11 '14 at 12:41
  • Where exactly would I place that? Sorry I'm really new to this so have no idea, i'd be grateful if you can write it in so I can paste it and try it – user3406032 Mar 11 '14 at 12:44
  • Downvoted because: Setting `async:false` is the worst User-Experience decision possible. If a user is on a slow 3G connection then their browser will be locked up for the duration of the AJAX call. Most latency issues stem from making the initial HTTP connection, the data transfer is relatively quick depending on the amount of data returned by process.php. The connection is what **kills** the user-experience so **NEVER** force the user's browser to lock up for no reason. The A in AJAX = **Asynchronous** or else we would use SJAX – MonkeyZeus Mar 11 '14 at 12:52
  • Hmm, What I can see from all your comments everywhere is that your process.php has some error. Because the only reason for alert not working in success function(when your html seems correct) is that the call was not successful. Try pressing F12 in browser and see if it says 'Internal Server Error' in console. – halkujabra Mar 11 '14 at 12:55