Jump to content

Cookie not working


thirdphase

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

 

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

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.