Hi, I created a guestbook that uses php + mysql. Anyways, how do i make it so that if someone hits submit twice or multiple times to sign my guestbook, that it wont have duplicate entries...

I know i can stop this with javascript... But a lot of people dont have javascript enabled so I want to do it server side...

THANKS! 🙂

    well, the easy but not failsafe way would be to just change the header to the new post upon posting but if you want the failsafe way you could just create a query that got the previous "id" and then match the post text to the text trying to be posted and make sure they dont match. did that make sense? i hope so, it is late - if not i can clarify again in the morning.

    good luck
    Ben

      nope didn't make much sense....🙁 I'm still pretty new to this stuff

        well you can also, just after you do your insert query, say
        header("location: sumwhere.php");

        and it will throw away all your post/get variables, and there will be nothing to insert when the person submits.

          Originally posted by terrabull
          well you can also, just after you do your insert query, say
          header("location: sumwhere.php");

          and it will throw away all your post/get variables, and there will be nothing to insert when the person submits.

          Tried it, it didn't work. If i click submit multiple times it still does it.

            heres a client side solution. this javascript thing will make a counter++ everytime the user hits the submit button, if the counter>1 it will not submit the form.

            its just a theory, and i havent tested it but this code should work.

            (as for them re-freshing the page that submits the data to enter it many times, use the other methods)

            <script>
            count=0;
            function valSub(){
            	count++;
            	return ((count>1)?false:true);
            }
            </script>
            <form action="<?=$GLOBALS['PHP_SELF']?>" method="post" onSubmit="return valSub();">
            <input type="submit"value="click me">
            </form>
            

            just another approach i s'pose
            -r

              In my opinion, the best way would be to use JavaScript, which would actually disable the Submit button once the form has processed. This way, you can't press it again, so you don't need to worry about ID's etc. That checking is good, if you're worried about users actually REFRESING the page.

              However, for your problem, I think this should do the trick:

              <script language="JavaScript">
                     function setSubmit() {
                             document.mainform.Submit.disabled=TRUE;
                     }
              </script>
              
              <form action="whatever.php" name="mainform" method="POST" onSubmit="setSubmit();">
              <input type="Text" etce tc whatever>
              <input type="Submit" name="Submit" value="Submit">
              </form>
              

              What this would do, as the user presses the Submit button, it'll grey it out, so they CANNOT click it again.

              This is quick code, so it's not very dynamic. For this to work, you have to have name="mainform" in your <FORM> line, and your submit button has to have the name of Submit... otherwise, change the variable names, it's simple enough 😉

              Hope this helps,
              Kevin

                Originally posted by kevinc
                In my opinion, the best way would be to use JavaScript, which would actually disable the Submit button once the form has processed. This way, you can't press it again, so you don't need to worry about ID's etc. That checking is good, if you're worried about users actually REFRESING the page.

                i had the right idea, but forgot alllll about the disable thing. time to make a note-to-self 🙂

                -r

                  8 days later

                  I did something even easier which works great - at least on my computer at work 🙂

                  On your submit button just add an onfocus blur like this:

                  <input type="Submit" name="Submit" value="Submit" onfocus="this.blur();">

                  This little neat feature is normally used to remove the at times pretty annoying dotted line around all text links and submit buttons ... but in this case it works pretty good with a submit button as I haven't (yet) been able to click the button twice before the script executes.

                  Anyway, it was just an idea 🙂

                  /// NetRoam

                    Probably the best solution, is to disallow dupe entries in the DB, and catch the error--That way, even if they have JS disabled, you still don't allow the dupes. And the way you control no dupes is through the use of nextval, and curr val.

                    do a search for preventing repeat

                    That should bring up a couple of threads where this was already discussed

                      Write a Reply...