SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation duplicating data entry when i refreshed browser?

    Hi, basically i am creating a form in php.
    I have a simple php form for various input boxes.
    I then post the data to another page with $_POST['abc']
    and did an INSERT into ABC into mysql.

    But how do i stop the script from inserting the data into mysql again if the user were to click on the refresh button? Becuase currently if the user would to click the refresh button again, another entry of data would be INSERTED into my MYSQL.

    Urgent, please help. Thanks

  2. #2
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On your action page
    PHP Code:
    session_start();
    if (
    $_SESSION['check'] != 'yes')
    {
    if (isset(
    $_POST['submit']))
    {
    $_SESSION['check'] = 'yes';
    // do your stuff

    There is probably a cleaner way to do it, though.

  3. #3
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks... this sounds cool. I'll go try it.

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The golden rule is RAP -- Redirect After Post. You should always send "Location" header after accepting POST data.

  5. #5
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  6. #6
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so what the difference in Dylannn's script and RAP rules?
    Dylannn's script should work fine and it's a tweak around right?
    Which is more of a professional way of doing things right?

  7. #7
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RAP is cleaner.

  8. #8
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chrischia
    so what the difference in Dylannn's script and RAP rules?
    Dylannn's script should work fine and it's a tweak around right?
    Which is more of a professional way of doing things right?
    His method fails on a site with multiple submit forms, if care is not taken. Also, it uses sessions, which is not always the way to go, since it uses cookies which can be denied by clients.

  9. #9
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Any guide for RAP?

    Then how should I implement the Redirect after POST RAP?
    Any guide to lead me through?

    I understand abit from the RAP links above but i do not know how to implement it.

  10. #10
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=chrischia]Then how should I implement the Redirect after POST RAP?
    Any guide to lead me through? [QUOTE]

    After inserting all data into MySQL, but this
    PHP Code:
    header("Location: /secondpage.html); 
    And also, in your first post, you didn't mention anything about validating your input, are you?

  11. #11
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so do i put it inside the header in php tag right after insert?
    <?php
    $query = "INSERT into ABC values ...";
    header("Location: /secondpage.html");?>

    i have used javascript to valid so shouldn't be of any problem?


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
  •