chop Posted February 23, 2016 Share Posted February 23, 2016 (edited) As many times as the browser refresh button is pressed, form data will continually be sent. True the browser warns you about this but that doesn't solve the problem. The numbers are added to the previous numbers. What is the best way to prevent a user from refreshing while they are still on the same form page? I put warnings of my own near the submit but... I'm not very confident in people even reading it. "Your computer may catch fire if you hit your browser's refresh" Edited February 23, 2016 by chop Link to comment Share on other sites More sharing options...
Larry Posted February 24, 2016 Share Posted February 24, 2016 Really the best solution is to have the page do a redirect after handling the form submission, even if that redirect is to the same page. That would make the most recent request--the one being refreshed--a GET, not a POST. Link to comment Share on other sites More sharing options...
chop Posted March 1, 2016 Author Share Posted March 1, 2016 Thank you very much. Link to comment Share on other sites More sharing options...
chop Posted March 8, 2016 Author Share Posted March 8, 2016 I am using a redirect as you prescribed but can't get it to work properly on my localhost (it works okay on the remote server). I put it at the top of my page saved as edit_image.php <?php header( 'Location: http://localhost:8888/sites_in_progress/PNE_development/edit_image.php' ) ; require_once('includes/sessionInfo.php'); require ('includes/config.inc.php'); Perhaps the syntax is incorrect? I get the following error msg: The page isn't redirecting properlyFirefox has detected that the server is redirecting the request for this address in a way that will never complete.This problem can sometimes be caused by disabling or refusing to accept cookies. Cookies are not disabled thanks Link to comment Share on other sites More sharing options...
Larry Posted March 10, 2016 Share Posted March 10, 2016 Um...is that the right URL for local? Also, you almost always want to call exit() after redirecting with header() to stop the rest of the page from executing. Link to comment Share on other sites More sharing options...
chop Posted March 10, 2016 Author Share Posted March 10, 2016 But if I have the header(), then an exit() right after it at the top of the page, how will the page ever load the first time, before the edit is made and then submitted. Won't it always exit()? Link to comment Share on other sites More sharing options...
chop Posted March 10, 2016 Author Share Posted March 10, 2016 I was thinking that redirects always have to be at the top of the page. But maybe I should put it right after it updates the database? Another idea: I have 5 separate forms all on the same page and each with their own SUBMIT button. What if each of these forms were to go to a sort of "Thank You" page with a link to go back to the forms page if desired. Would that do it? Link to comment Share on other sites More sharing options...
Larry Posted March 10, 2016 Share Posted March 10, 2016 Oh, yeah, if you just have the header() right at the top of the page, that'd be wrong, because it won't handle a form submission. The header() should be called after the posted form data has been handled successfully. You can put a header() call anywhere, and commonly do, you just need to watch out for headers already sent issues. Link to comment Share on other sites More sharing options...
chop Posted March 12, 2016 Author Share Posted March 12, 2016 Great. Makes sense now. thanks Link to comment Share on other sites More sharing options...
chop Posted March 14, 2016 Author Share Posted March 14, 2016 Well, apparently it's more complicated. I am getting a "headers already sent "warning. I start a session near the top of my code and I also send headers early on. I tried using ob_on (and flush at the end of the code) but then I don't get some necessary $GET variables. First 4 lines of code: <?phpob_start();require_once('includes/sessionInfo.php');require ('includes/config.inc.php'); include ('includes/header.html'); ... put ob_flush here </body> I put the header() right after a successful db update. But, as I said, headers have already been sent. $q = "INSERT INTO ".OTHERSALES." (image_id, prints_amt, cards_amt, other_amt, prints_cost, cards_cost, other_cost, prints_income, cards_income, other_income, os_comment, os_date_sold, os_date_added) VALUES ('$image_id', '$prints_amt','$cards_amt','$other_amt','$prints_cost','$cards_cost','$other_cost','$prints_income','$cards_income', '$other_income', '$os_comment', '$os_date_sold', NOW() )"; $r = @mysqli_query ($dbc, $q); // Run the query. // ----- update the modification date in the users database update_user_table($dbc); $add_post_alert=true; // ----- END update header('Location: '.$_SERVER['PHP_SELF']); } else //----- Report the errors. ------- { echo '<h4 class="warning">The following need to be corrected:</h4> <p class="error">'; foreach ($errors as $msg) { // Print each error. echo " - $msg<br />\n"; Is it a matter of proper placement of ob_start / (flush), header(), $GET variables, session() to make it work? Previously, I had a user warning not to use the refresh button but it seems clunky and they don't always read anyway. Where this is a matter of an MYSQL INSERT, it is of special importance as it means a new row of stuff that is repeated, increasing all the totals. What to do? Link to comment Share on other sites More sharing options...
Larry Posted March 14, 2016 Share Posted March 14, 2016 What is the full and complete error message? Link to comment Share on other sites More sharing options...
chop Posted March 20, 2016 Author Share Posted March 20, 2016 (edited) An error occurred in script '/ApacheServer/ApacheDocRoot/sites_in_progress/PNE_development/edit_image.php' on line 791: Cannot modify header information - headers already sent by (output started at /ApacheServer/ApacheDocRoot/sites_in_progress/PNE_development/includes/header.html:6) Date/Time: 3-20-2016 12:32:00 Error message above line 791: header('Location: '.$_SERVER['PHP_SELF']); Line 791 comes right after the query update The following is at the very beginning of the page. You'll notice it has a session include (yours) and an include that loads the <head> information <?php require_once('includes/sessionInfo.php'); require ('includes/config.inc.php'); //require('includes/reports_functions.php'); // ALWAYS SEND THE IMAGE ID TO THIS PAGE ON SUBMIT $page_title = 'Edit'; // create state array $states_arr = array('SS'=>"Select State",'AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'DC'=>"D.O.Columbia",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa", 'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland", 'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'WV'=>"West Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming"); include ('includes/header.html'); This is the <head></head> information that gets loaded in the script above: <!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?php echo $page_title; ?></title> <?php require_once('includes/CMSassets.html'); ?> </head> I assume that the "headers already loaded" error has to do with one or both of the "includes" loaded at the beginning. I just don't know how to get around it. thanks Edited March 20, 2016 by chop Link to comment Share on other sites More sharing options...
Larry Posted March 21, 2016 Share Posted March 21, 2016 So the error message says that output is created by header.html. Are you using output buffering? Link to comment Share on other sites More sharing options...
chop Posted March 21, 2016 Author Share Posted March 21, 2016 I tried using output buffering but, if I remember correctly, I then got some undefined variables that I needed for the next page such as image id number and a couple other things. These had either been retrieved by GET I believe (might have been sending them in a header) or a POST Anyway, the ob didn't work when I put the ob at the beginning and then flushed it at the end of my code (because of what I described). My big mistake was to put multiple forms on the same page... everything got complex because of the amount of code. Good thing I use Dreamweaver at least, that let's me hide giant blocks. It might be a matter of rearranging my code. I'll probably take my own advice and go over the whole thing and clean it up. I should do that anyway about now. It's not really a life endangering glitch, but if you have any suggestions, I'll give it a try. thanks, chop Link to comment Share on other sites More sharing options...
Larry Posted March 22, 2016 Share Posted March 22, 2016 Well, the main thing is that if you might redirect somewhere deep into a script, then you have to use output buffering. Link to comment Share on other sites More sharing options...
chop Posted March 30, 2016 Author Share Posted March 30, 2016 (edited) I'm still working on this issue but closing in on a solution now. I think the only thing I need to know is the proper syntax for a redirect while sending a variable in the header. For instance, this works: header( 'Location: http://localhost:8888/sites_in_progress/PNE_development/edit_image.php?id=151' ) ; But the value 151 is contained in a variable $image_id I've tried several combinations of single and double quotes and it isn't working. How do I send the value of $image_id instead of "151" itself. My life will be complete if I can get that to work.... at least for the moment. thank you Edited March 30, 2016 by chop Link to comment Share on other sites More sharing options...
chop Posted March 30, 2016 Author Share Posted March 30, 2016 (edited) Okay, I think I have it: header( 'Location: http://localhost:8888/sites_in_progress/PNE_development/edit_image.php?id='.$image_id ) ; As a final end to this post, the redirect actually does work and I am not having any more problem with the browser refreshing and re-posting duplicate values. The main problem was that the redirect needed to be accompanied with a value that I needed carried over to the next page, which i finally did (above). I also discovered that a "headers have already been sent" error will be caused by things that can easily be overlooked, such as html that has been commented out (seemingly benign). Or by <?php header with a space after it. Unforgiving it is. Also went back to your book & re-read about output buffering which helped. thanks for you assistance.. onto other things Edited March 30, 2016 by chop Link to comment Share on other sites More sharing options...
chop Posted March 30, 2016 Author Share Posted March 30, 2016 Well, okay, not quite onto other things... just one more complication here. I need to add an anchor to the code below: header( 'Location: http://localhost:8888/sites_in_progress/PNE_development/edit_image.php?id='.$image_id ) ; so that it redirects to a specified location on the page (id="this_location") on the page. I can do it without the variable added on to the header() but not with it there. Link to comment Share on other sites More sharing options...
Larry Posted March 31, 2016 Share Posted March 31, 2016 Thanks for sharing what you've figured out so far! And kudos! I believe the syntax for variables plus anchors is edit_image.php#this_location?id=X Link to comment Share on other sites More sharing options...
chop Posted March 31, 2016 Author Share Posted March 31, 2016 okay, things seem to work. browser refresh is no longer a problem. thanks for help Link to comment Share on other sites More sharing options...
Larry Posted April 4, 2016 Share Posted April 4, 2016 Awesome. So glad to hear it! Link to comment Share on other sites More sharing options...
Recommended Posts