[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

How do I set a cookie with a select list?

I've got a select list similar to the following select list. I want to pass the results into a cookie that then displays on the cookielanding.php page. What is the best way to do this using PHP?

<form id="cookietest" name="cookietest" method="post" action="cookielanding.php">
 
  <select name="select">
        <option value="">Select a Value</option>
      <option value="1">Value One</option>
        <option value="2">Value Two</option>
        <option value="3">Value Three</option>
  </select>
 
  <input name="SubmitTest" type="submit" />

</form>
0
befidled
Asked:
befidled
  • 2
1 Solution
 
dr_dedoCommented:
in cookielanding.php
<?
if (isset($_POST['select'])){
   setcookie("select_value",$_POST['select'],time()+60*60*24*30);
}
?>

to retrieve it

echo $_COOKIE['select_value'];
0
 
befidledAuthor Commented:
ok, this works, but as I'm testing it, it keeps displays the previous value. So for instance, I select "Value 3", but on the landing page it displays "1" if the cookie had been set to 1 before I chose "Value 3". Then if I go back and select "Value 2", it will now display "3". What do I need to do to force it to refresh the cookie value?

thanks.
brian
0
 
befidledAuthor Commented:
Ok, I've got it working, but it takes 3 separate php pages, is there anyway to condense it to two? Essentially, "cookietest.php" allows me to choose from my list and posts the results to "cookieglue.php". "cookieglue.php" then sets the cookie and then redirects to "cookielanding.php" which displays the results. I'd like to eliminate the script "cookieglue.php" if possible.

Here are the 3 pages:

cookietest.php
____________

<!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>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>

<body>

<form id="org_choice" name="org_choice" method="post" action="cookieglue.php">
  <select name="organizationchoice">
    <option value="">Select a Value</option>
      <option value="1">Value One</option>
    <option value="2">Value Two</option>
    <option value="3">Value Three</option>
  </select>
  <input name="SubmitTest" type="submit" />
</form>

</body>
</html>


cookieglue.php
____________

<?php

if (isset($_POST['organizationchoice'])){
   setcookie("cookieTest", $_POST['organizationchoice']);
   header("Location: cookielanding.php");
}

?>


cookielanding.php
______________

<!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>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="../WA_Cookies/Cookies.js"></script>
</head>

<body>
<div>Welcome to cookie outpost. You've just landed here.</div>
<div>The PHP cookie is: <?php echo $_COOKIE['cookieTest']; ?></div>
</body>
</html>

0
 
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now