Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP Form with Bootstrap Validation

  • 30-10-2017 07:35PM
    #1
    Registered Users, Registered Users 2 Posts: 60 ✭✭


    I have a php bootstrap validation form that when submitted sends the details to a database. The form works nicely. I have the validation and the success working, I have all the details going to the database but my issue is with clearing the form. When I press submit, it all works well except for it doesn't clear the form. This is my code....CAN ANYONE PLEASE HELP!!!

    .on('success.form.bv', function(e) {
    $('#success_message').slideDown({ opacity: "show" }, "slow") // Do something ...
    $('#contact_form').data('bootstrapValidator').resetForm();

    // Prevent form submission
    e.preventDefault();

    // Get the form instance
    var $form = $(e.target);

    // Get the BootstrapValidator instance
    var bv = $form.data('bootstrapValidator');

    // Use Ajax to submit form data
    $.post($form.attr('action'), $form.serialize(), function(result) {
    console.log(result);
    }, 'json');
    });


Comments

  • Registered Users, Registered Users 2 Posts: 6,271 ✭✭✭Buford T Justice


    $(this).closest('form').find("input[type=text], textarea").val("");
    

    Or
    window.location.reload()
    


  • Registered Users, Registered Users 2 Posts: 7,133 ✭✭✭Talisman


    jQuery doesn't have a form reset method, you can use a native DOM method:
    document.getElementById("contact_form").reset();
    

    Or if you want to stay in jQuery land:
    $("contact_form")[0].reset();
    


  • Registered Users, Registered Users 2 Posts: 60 ✭✭Nolic


    Talisman wrote: »
    jQuery doesn't have a form reset method, you can use a native DOM method:
    document.getElementById("contact_form").reset();
    

    Or if you want to stay in jQuery land:
    $("contact_form")[0].reset();
    

    My JavaScript would be very limited. Do I add one of these to the bottom of the code already provided by Bootstrap Validator?


  • Registered Users, Registered Users 2 Posts: 60 ✭✭Nolic


    $(this).closest('form').find("input[type=text], textarea").val("");
    

    Or
    window.location.reload()
    

    My JavaScript would be very limited. Do I add one of these to the bottom of the code already provided by Bootstrap Validator?


  • Registered Users, Registered Users 2 Posts: 6,271 ✭✭✭Buford T Justice


    You'd add it to the post request for submitting the form. Once the submission has completed correctly, that's the point where you'd want to clear the form i'd imagine


  • Advertisement
Advertisement