POST data & Refreshing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ninuhadida
    New Member
    • Jul 2007
    • 4

    POST data & Refreshing

    hi there! This is actually my first post over here.. I thought i'd register since most answers to my common problems that i google up turn up to be in this site.. however I have a problem myself now.

    This is the source code: (PHP)

    [php]if(mysql_error( )) { // was there an error ?
    //yes, issue warning
    echo "Sorry, your comment was <b>not</b> added due to a database error!";
    }
    else { // no, print success and link
    echo "<meta http-equiv='refresh' content='0'>";
    }[/php]


    what am i trying to do over here? basically i'm working on a site where visitors can post comments. Now since i'm using POST data, we all know that if a user refreshes the page it gets reposted. I know that your browser gives you an error, but let's face it -- the common user doesn't even know what POST data is, moreover what the error generated by the browser means.

    now onto my problem. the code runs the mysql query with tha data that has been submitted in the comment form (after being validated) As shown in the source code, if there was a mysql error, it will display an error to the user. else it is supposed to refresh by echo'ing a meta tag. Just a couple of weeks ago, just after coding this thing, it worked flawlessly. But now it doesn't! I checked that it is actually refreshing by setting the number of seconds to 5 and checked that it is being refreshed.. but still the POST data is still there, because when I hit the refresh button, it displays the POST data error.

    If you have any idea why this is happening please let me know.. cos it's driving me nuts, especially when I know that a couple of weeks ago it worked flawlessly :/

    thanks guys,

    ninuhadida.
  • ninuhadida
    New Member
    • Jul 2007
    • 4

    #2
    sorry guys, I just found out I posted this thing in the articles section! & I can't find a way to delete my post. If an admin can move this thing to the questions section / delete it, i would greatly appreciate it.

    ..sorry for being a nuisance!

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, ninuhadida. Welcome to TSDN!

      Originally posted by ninuhadida
      sorry guys, I just found out I posted this thing in the articles section! & I can't find a way to delete my post. If an admin can move this thing to the questions section / delete it, i would greatly appreciate it.

      ..sorry for being a nuisance!
      No problem. Happens all the time.

      Unfortunately.

      But it's no biggie. I'll move your thread over to the correct spot so we can get some Experts right on it.

      Comment

      • adamalton
        New Member
        • Feb 2007
        • 93

        #4
        Is the comment bit in a frame or something? i.e. does the whole page not change anyway when they submit the comment? Or is it a facebook-style thing where it submits the post but the page itself doesn't change? If so, if the meta tag isn't working could you use javascript?

        [HTML]<script type="text/javascript">
        window.location ="url_of_the_pa ge.php"; //I think that's right!
        </script>[/HTML]

        I'm not hot on the intricacies of html, but doesn't a meta tag have to go in the <head> section?

        Comment

        • ninuhadida
          New Member
          • Jul 2007
          • 4

          #5
          Originally posted by adamalton
          Is the comment bit in a frame or something? i.e. does the whole page not change anyway when they submit the comment? Or is it a facebook-style thing where it submits the post but the page itself doesn't change? If so, if the meta tag isn't working could you use javascript?

          [HTML]<script type="text/javascript">
          window.location ="url_of_the_pa ge.php"; //I think that's right!
          </script>[/HTML]

          I'm not hot on the intricacies of html, but doesn't a meta tag have to go in the <head> section?
          first off, thanks pbmods for moving my question. Sorry again for the unneeded trouble!

          thanks adamalton for your reply. The page doesn't have any frames at all. the meta tag is printed in a <div> element. I know that it ain't valid xhtml since it's not in the <head> section. but i have a problem since the head section is already printed out before the the script goes into action and decides whether to refresh or not. What pisses me off here is that just 2 weeks ago it worked flawlessly & now that it isn't working, the page is still being refreshed = meaning that the meta tag is working, but the browser still keeps POST data.

          about your little javascript snippet, I try to keep js code to the minimum as possible, since a simple turn off javascript for the browser options means a breakdown of how the website works.. and you mentioned that facebook (i don't use that site.. never even browsed it.. :/) adds your comments without the page being refresh at all. I would love to have something like that but I guess that means you need to have some kind of AJAX code, and I never written js/xml code myself. I certainly want to learn that but for now php/xhtml/css/mysql is already too much to take in at once!

          thanks again,

          regards

          ninuhadida.

          Comment

          • adamalton
            New Member
            • Feb 2007
            • 93

            #6
            Yeah, I have no idea how facebook manages to post things without refreshing the page, and so far I've never needed to know....so I don't!

            Don't get me started on w3c web standards and 'valid' html. Some of it is such pointless crap that just means your pages are full of useless code! (Like images having to have alt="".) GRRRRRRR!!!!!!! !

            But anyway...if your meta tag is as you have it at the moment then you are telling your browser to refresh the page, i.e. reload it, and therefore send the POST data as well. But if you change it to this;
            <meta http-equiv='refresh' content='0; url=http://www.yoursite.co m/samepage.php'>"
            then it will 'redirect' your browser, i.e. make it think it's going somewhere else, and so it shouldn't send the POST data again, even though you are just redirecting it to the page it's already on!

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, ninuhadida.

              The way I always tackle this problem is to do my backend stuff on a different page than my frontend stuff.

              In other words, when the User submits the form on somepage.php, it goes to dbi/somepage.php. If the DBI ('DataBase Interaction') script hits an error, it redirects back to the form so that the User can fix his errors. On success, it redirects to the success page.

              Comment

              • ninuhadida
                New Member
                • Jul 2007
                • 4

                #8
                hey pbmods & adamalton,

                thanks for your suggestions, i will try your suggestions tommorrow, since i had a long day today and i'm tired. hope i'll have time to code something tommorrow!

                BTW, the meta tag that redirectes, i already used it in another section (the contact us) and it worked perfectly. so i hope it does the same with the comment thing.

                will let you know about the outcome soon :)

                thanks again,

                regards,

                ninu.

                Comment

                • henryrhenryr
                  New Member
                  • Jun 2007
                  • 103

                  #9
                  Hello ninuhadida

                  Quick note about the alt="" concern - if your visitor is blind and can't see your picture then their screen-reader should be able to read out the alt attribute - ie at least get a better idea of your content. I think it helps for SEO too.

                  For the refresh problem - I think two methods already mentioned - handle forms on a seperate page (ie don't use :
                  Code:
                  action="<?php echo $_SERVER['PHP_SELF']?>"
                  ). And you can keep track of form submissions in your database by having it write a unique number to a table and check that number on every submit of the form.

                  My method is to use sessions (also works with cookies). You have the page check the form, run DB stuff then save the data, messages, whatever in the $_SESSION array. Then you use
                  [CODE=php]<?php header('Locatio n: www.mysite.com/page.php'); exit(); ?>[/CODE] to reload the page to itself a second time (make sure you use absolute link in header() and put exit() to prevent the rest of the script running).

                  When this page reloads for the second time, the $_POST data is gone so a browser refresh won't re-submit it!

                  Comment

                  Working...