thirdphase Posted May 18, 2011 Share Posted May 18, 2011 I am trying to pass a variable as a cookie from one page to another, but can't seem to get it working. The first page has a form with a submit button. The forms action goes to a second page called updated.php. In the updated.php page I have the following code (with the cookie right at the top before anything else): <?php setcookie("get_orderID", $get_orderID, time()+5); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> etc... Later in this page I have the code to send the original form data to an sql database and then retireve an auto increment value back from the database as follows: <?php if($edit_orderID == 0) { $submit_order_sql = "insert into my_orders (date, name, clientID) values ( \"$date\", \"$name\", \"$clientID\" )"; $submit_order_rs = mysql_query( $submit_order_sql, $conn ); $get_order_sql="select orderID from my_orders where job_no = $job_no"; $get_order_rs = mysql_query( $get_order_sql,$conn ); $get_order_row = mysql_fetch_array( $get_order_rs ); $get_orderID = $get_order_row["orderID"]; ?> Finally I need to send the variable I have found back to the form page (using a cookie). But it seems that as the variable is being created at the bottom of the page, the cookie at the top is not seeing the variable. Can anyone help please?? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/236733-cookie-not-working/ Share on other sites More sharing options...
Zurev Posted May 18, 2011 Share Posted May 18, 2011 PHP is interpreted line by line, I don't believe you can declare something below it and have it work. Also, you're setting your cookie to exist for 5 seconds, might want to check on PHP's setcookie. http://www.php.net/manual/en/function.setcookie.php Quote Link to comment https://forums.phpfreaks.com/topic/236733-cookie-not-working/#findComment-1216937 Share on other sites More sharing options...
thirdphase Posted May 18, 2011 Author Share Posted May 18, 2011 Thanks for your reply. I thought that was the case but I'm not sure how else I can do this, as I can't put all of the code to send the data to the database above the setcookie as this doesn't work either! The 5 seconds was set so that the cookie would only exist long enough to redirect back to the form page and populate the fields again. Would there be another way of sending this variable?? Quote Link to comment https://forums.phpfreaks.com/topic/236733-cookie-not-working/#findComment-1216940 Share on other sites More sharing options...
PFMaBiSmAd Posted May 18, 2011 Share Posted May 18, 2011 Php produced web pages should be laid out as follows - <?php // php code that determines what the page is going to do, if you are going to stay on the page or redirect, and what content is going to be on the page (php produced content is simply built in php variables) ?> <!DOCTYPE .... <body> html.... <?php echo $some_php_produced_content here...; ?> html.... <?php echo $some_other_php_produced_content_here...; ?> html... </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/236733-cookie-not-working/#findComment-1216942 Share on other sites More sharing options...
thirdphase Posted May 18, 2011 Author Share Posted May 18, 2011 I have now moved all of the php code above the html and it works fine! Obvious when you know how! Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/236733-cookie-not-working/#findComment-1217234 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.