Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Javascript Form

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #32491
    ChrisBull
    Member

    I have a javascript form and I only want the user to be able to submit it if they have javascript installed.
    At the moment i have a form with two input fields and then an ‘a tag’ that javascript adds to the page and has a href value of javascript: submitForm(). This all works fine. I would like to know if with this system is there anyway that someone without javascript enabled could submit the form?

    Many Thanks
    Chris

    #49018

    Hey Chris – I’ve actually run into a similar scenario. I handled it by setting the form action using JavaScript or jQuery. This way if someone without JavaScript enabled tries to submit the form, the page just posts to itself and doesn’t go anywhere.

    Here’s a jQuery solution:

    $('#contactform').attr('action', 'post.php');

    Or just the plain JavaScript one:

    document.getElementById('contactform').action = 'post.php';
    #48915
    ChrisBull
    Member

    Hi, At the moment I have

    $("#formSubmit").click(function() {

    but the page still gets reloaded when the button is clicked, will your way stop it reloading?

    #48923

    You could set the button as disabled and then use jQuery to enable it.

    $('#submit').attr('disabled', false);

    You HTML for the button would then look something like:

    #48926
    ChrisBull
    Member

    Lol, sorry I didn’t realise I hadn’t explained it, I can’t have the page refresh, even for javascript users. I will check the details through ajax and a server php file, so at the moments the page will still refresh when the user click submit.

    #48961
    Masayoshi
    Member

    Chriss i think you want to prevent the default behaviour of that submit button.
    If so you can use return false; at the end of your .click function.
    So you will have something like:


    $("#formSubmit").click(function() {
    // ajax stuff here
    return false;
    }
Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.