Jump to content

Cookie not working


Recommended Posts

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):



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">




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:



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??



Link to comment
Share on other sites

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??

Link to comment
Share on other sites

Php produced web pages should be laid out as follows -


// 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 ....

<?php echo $some_php_produced_content here...; ?>
<?php echo $some_other_php_produced_content_here...; ?>

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.