Jump to content

help with setcookie function


knowram

Recommended Posts

I have three pages. The first has a form on it with a text field. That gets submitted to the second page where I would like to set that submitted text as a cookie so that I can access that info on the third and following pages.

If I do this where I set the variable on the same page as i use setcookie everything works fine
[code]
//Page 1
<?php
$drname = "jacob";
setcookie("TestCookie", $drname);
?>
//Page 2
<?php
echo $_COOKIE["TestCookie"];
?>
[/code]

but if i do this it will not work
[code]
//Page 1

<form>
<input type="text" name="drname" size="24" />
<input type="submit" name="Submit" value="Submit" />
</form>

//Page 2
setcookie("drname", $drname);

//Page 3
echo $_COOKIE["drname"];
[/code]

What am I missing
Thanks for the help in advance.
Link to comment
Share on other sites

yah, this will work!


[quote author=fiddy link=topic=114493.msg465848#msg465848 date=1163137684]
I am not sure but try like this:

put action for form tag and nethos as post <form action="xxxxx" method="yyyyyy">

then try getting as :

if($_POST)
{
    $drname=$_POST['drname'];
    setcookie("TestCookie", $drname);
   
}
[/quote]
Link to comment
Share on other sites

Simply put, the setcookie function must be called before you do any rendering (i.e.: <form> <h> any html elements). This is because the cookie is passed in the header of a page instead of the body.

[code]
<?php

setcookie("cookie_name", $cookie_data);

?>

<p>First thing you see on the page</p>
[/code]
Link to comment
Share on other sites

Sorry guys once again all looks good but will not work here is the code once again.

[code]
//Page 1
<form action="diagnoses/choice.php" method="post">
<input type="text" name="drname" size="24" /> <input type="submit" name="Submit" value="Submit" />
</form>

//Page 2
<?php
if($_POST)
{
$drname = $_POST['drname'];
setcookie("drname", $drname);
}
?>

//Page 3
<?php
echo $_COOKIE["drname"];
?>
[/code]

???????
Link to comment
Share on other sites

The code you just posted is working for me

Page 1
[code]<?php

echo '<form action="2.php" method="post">
<input type="text" name="drname" size="24" /> <input type="submit" name="Submit" value="Submit" />
</form>';

?>[/code]

Page 2
[code]<?php

if($_POST)
{
$drname = $_POST['drname'];
setcookie("drname", $drname);
}

?>[/code]

Page 3
[code]<?php

echo "The set cookie is: ". $_COOKIE["drname"];

?>[/code]

It does set the cookie as shown here using the firefox web developer addon.

[img]http://www.carpfishinguk.net/cookie.jpg[/img]
Link to comment
Share on other sites

Okay now I am really confused. if I make 3 new pages with just that code you are right it works fine. But I still can't get it to work in the pages I need it to. Here is the entire code for the 3 pages. assume I typed jacob in the text box.

[code]
[color=yellow]//Page 1[/color]

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

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>EMR Beta 1</title>
</head>


<body background="../images/BG.jpg">
<div align="center">
<table width="550" border="0" cellspacing="0" cellpadding="0" style="background-image: url(../images/full.jpg);">
<tr>
<td valign="top">
<div align="center">
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<img src="../images/docin.gif" alt="" height="52" width="228" border="0" /><br />
<font size="-1" color="white">Use your full name as your would like it to appear on the final print<br />
And remember it is cap sensitive</font><font color="white"><br />
</font>
<br />
<form action="diagnoses/choice.php" method="post">
<input type="text" name="drname" size="24" /> <input type="submit" name="Submit" value="Submit" />
</form>
<br />
<br />
<br />
<font size="-1" color="white">Please do not include your appellation (Dr. NP. PA-C.).<br />
If this is your first time logging in this will be added on the following page.</font><br />
<br />
<br />
<br />
<br />
<br />
</div>
</td>
</tr>
</table>
</div>
</body>

</html>

[color=yellow]//Page 2[/color]

<?php
setcookie("drname", $drname);
echo $_COOKIE["drname"]; [color=yellow]//will print jacob[/color]
?>

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

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>EMR Beta 1</title>
</head>


<body background="../../images/BG.jpg" text="white" link="white" vlink="white" alink="white">
<div align="center">
<?php

//Conecting to database for buttons

mysql_connect("localhost", "envision_knowram", "test") or
die ("Could not connect to database");
mysql_select_db("envision_elcer") or
die ("Could not select database");

$result=mysql_query("select * from medications WHERE Doc = '$drname'");
$row1=mysql_fetch_array($result);

$result2=mysql_query("select * from instructions WHERE Doc = '$drname'");
$row2=mysql_fetch_array($result2);

//looking for doctor


if ($row1[0] != $drname and $row2[0] != $drname){


?>
<br />
<br />
<br />
<font size="+1" color="white">Could not find a anyone by that name.<br /></font>
<font color="white">If this is your first time loging in please select a Practitioners Designation.</font><br />

<form method=post action="newdoc.php">
<input type="submit" name="Pos" value="Dr.">
<input type="submit" name="Pos" value="PA-C">
<input type="submit" name="Pos" value="NP">
<input type="hidden" value="<?php echo $drname ?>" name="drname">
</form>

If you have created an account already please try to login again<br />
<br />
<font size="+2" color="white"><a href="../Login.html">Login</a></font>

<?php
}else{
  ?>
<table width="550" border="0" cellspacing="0" cellpadding="0" style="background-image: url(../../images/full.jpg);">
<tr>
<td align="center" valign="top">
<br />
<br />
<br />
<br />
<br />
<br />
<a href="../running1/Clear.php"><img src="../../images/start.gif" alt="" height="47" width="326" border="0" /></a><br /> [color=yellow]//link used to get to the next page[/color]
<br />
<img src="../../images/or.gif" alt="" height="42" width="70" border="0" /><br />
<br />
<a href="editchoice.php?drname=<?= $drname?>"><img src="../../images/edit.gif" alt="" height="49" width="326" border="0" /></a><br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</td>
</tr>
</table>
</div>
</body>
</html>
<?php
}
echo $_COOKIE["drname"]; [color=yellow]//will print jacob[/color]
?>

[color=yellow]//Page 3 ../running1/Clear.php[/color]

<?php
echo $_COOKIE["drname"]; [color=yellow]//will not print jacob or anything[/color]
?>
<!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">

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Clear</title>
</head>

<body background="../../images/BG.jpg">
<?php
mysql_connect("localhost", "envision_knowram", "test") or
die ("Could not connect to database");
mysql_select_db("envision_elcer") or
die ("Could not select database");

$query = "UPDATE `patients` SET `Diag1` = '' ,`Diag2`= '' ,`Meds1` = '' ,`Meds2`= '',`Ins1` = '' ,`Ins2`= '' WHERE `Ref` = '$drname'";


Mysql_query($query) or
  die (mysql_error());
 
  echo $_COOKIE["drname"];
?>
<p><META HTTP-EQUIV=Refresh CONTENT="1; URL=http:diagnoses.php"></p>
</body>

</html>
[/code]

sorry for all the code but from everything that I can see it should work something seems to be clearing the cookie some where.

thanks again for the help
Link to comment
Share on other sites

??? ???

Okay now I am having trouble with my simple test group of pages. Here is the code that I am using.

[code]
//Page 1
<!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">

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="Adobe GoLive" />
<title>Untitled Page</title>
</head>

<body>
<p>
<form  method="post" action="test2.php">
<input type="text" name="drname" size="24" /> <input type="submit" name="Submit" value="Submit" />
</form>
</p>
</body>

</html>

//Page 2
<?php
setcookie("drname", $drname);
?>
<META HTTP-EQUIV=Refresh CONTENT=".3; URL=http:test3.php">

//Page 3

<?php
echo $_COOKIE["drname"];
?>
[/code]

Everything works great the first time through. But if I go back to the first page and try entering something different it dose not change the cookie. Is there something I need to add to make that happen? Also if I ever get this working I am going to want to set the cookie so that it never dies. How do I do that?

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