SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot malluwood's Avatar
    Join Date
    Nov 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to avoid multiple submit button click

    hi all

    i have one form on which the data is stored in database and suppose the
    user clicks the back button of browser and again click on submit button
    then the data will be saved in database again.

    how can i avoid this? please help me

    Thanks

  2. #2
    SitePoint Zealot glowdot's Avatar
    Join Date
    Jun 2007
    Location
    Santa Monica, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. disable the back button with javascript.
    2. check to see if the data is already in the database before inserting it.

    Those are just two options off the top of my head.

    Also, depending on whether it is for your sake or the user's sake that you don't want data to be submitted twice, you should have an intermediate page that says "are you sure?" or similar before submitting. i.e. display form->display confirmation page->submit data.
    Advertise on Glowfoto
    banners as low as $25/month, text ads $10/month
    Share 10 million impressions per month!

  3. #3
    SitePoint Zealot malluwood's Avatar
    Join Date
    Nov 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there any other way?

    can we use cookies or sessions

  4. #4
    SitePoint Zealot glowdot's Avatar
    Join Date
    Jun 2007
    Location
    Santa Monica, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Again off the top of my head, you could set a cookie when you submit the data, and then refuse to submit anything again until the cookie expires. You could do the same thing with a session var if you set it to a timestamp.

    That only prevents resubmitting within a certain amount of time though.

    What exactly are you trying to prevent here? If it is just multiple, accidental submissions you are trying to avoid, that's going to be easily handled by one of the above. If it is abusive users, you might want to go as far as querying the database for the data before you submit it.
    Advertise on Glowfoto
    banners as low as $25/month, text ads $10/month
    Share 10 million impressions per month!

  5. #5
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    809
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The most easy way is going to a separate process file, which submits data to the database, and redirects the user to another page using header.

    Something like this:

    Code php:
    // form.php
     
    <?php if (!isset($_GET['success'])){ ?>
    <form method="post" action="process.php">
    <input type="hidden" value="banana" name="foo">
    <input type="submit">
    </form>
     
    <?php } else { ?>
     
    <p>Banana was successfully stored in the database!</p>
     
    <?php } ?>
     
    // process.php
     
    $sql = "INSERT INTO fruit SET fruitname = '".$_POST['foo']."'";
    /* database logic goes here */
     
    if ( /* successfull insert */ ) {
      header ('Location: form.php?success');
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •