Question regarding Cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Question regarding Cookies

Post by jefffan24 »

I would like to add selected elements from a form containing radio buttons to a cookie.

So like if I have 2 options: Vertical, Horizontal and the user selects Horizontal i want horizontal to become the value of the cookie.

Right now this is what I have:

Above the html tag:

Code: Select all

 
<?php 
$attendance_yes = 'unchecked';
$attendance_no = 'unchecked';
 
if (isset($_POST['submit'])) {
$selected_radiobtn = $_POST['guest_attendance']; 
 
(setcookie("TestCookie",$selected_radiobtn,time()+3600)){
 
 
}
?>
In the body:

Code: Select all

 
<?php
echo "<br /><br /><br /><br /><center>";
echo "<h3>Click the buttons on one of the options below and an example will show up below me!</h3>";
echo "<iframe src=\"menus.php\" name=\"test\" width=\"700px;\" height=\"280px\" frameborder=\"0\" scrolling=\"no\">" ;
echo "</iframe>";
 
echo "<form name=\"menus\" id=\"menus\" method=\"post\" action=" . $PHP_SELF . "\">";
echo "<br /><br /><input type=\"radio\" name=\"Menu\" value=\"Vertical\" onclick=\"test.location.href='menus/vertical/cssmenu.html'\" />Vertical";
echo "<input type=\"radio\" name=\"Menu\" value=\"horizontal\" title=\"Horizontal\" onclick=\"test.location.href='menus/horizontal/cssmenu.html'\">Horizontal<br />";
echo "<input type=\"submit\" name=\"submit\" value=\"next\" />";
echo "</form></center>";
?>  
How would I do this?
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: Question regarding Cookies

Post by jefffan24 »

Never mind I got it this is what i did:

Code: Select all

<?php 
$menu = $_POST["Menu"];
if (!isset($_POST['submit'])) {
 
echo "<br /><br /><br /><br /><center> 
<h3>Click the buttons on one of the options below and an example will show up below me!</h3>
<iframe src='menus.php' name='test' width='700px;' height='280px' frameborder='0' scrolling='no'></iframe>
<form name=\"menus\" id=\"menus\" method=\"post\">
<br /><br /><input type=\"radio\" name=\"Menu\" value=\"Vertical\" onclick=\"test.location.href='menus/vertical/cssmenu.html'\" />Vertical  &nbsp;
<input type=\"radio\" name=\"Menu\" value=\"horizontal\" title=\"Horizontal\" onclick=\"test.location.href='menus/horizontal/cssmenu.html'\">Horizontal<br />
<input type=\"submit\" name=\"submit\" value=\"next\" />
</form></center>";
} else {
    setcookie("Menu", $menu, time()+6000);
    header ("location: cookie.php");
}
?>
all before the html tag and it worked.
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: Question regarding Cookies

Post by jefffan24 »

Well now I got another question. How can i make it so whatever value is set in the cookie is auto selected when the user loads the page.

Like lets say I have 3 radio button options. And the user selects the 2nd one and then they leave the page, when they leave the page I set a cookie with the value from button 2. How can make it so that when that user comes back it pulls that value from the cookie and autoselects option 2?
Post Reply