Jump to content

[SOLVED] Cookies problem


iarp

Recommended Posts

I thought this was going to be a simple form to change a cookies value, but i can't figure out wtf is wrong. I'm in need of assistance please.

<?php
require_once('MDS_includes/header.php');

if(isset($_POST['submitted'])) {

		$temp = $_POST['links'];
		$t = $temp;
		$expire = time()+60*60*24*30;
		setcookie("mds_viewtype", $t, $expire);
}
?>

<form action="prefs.php" method="post">

<label>How would you like product links to open?</label><br />
	<input type="radio" name="links" value="1" id="501" <?php if ($_COOKIE['mds_viewtype'] == '1') echo checked; ?> /><label for="501">Not in a popup maner</label><br />
	<input type="radio" name="links" value="2" id="502" <?php if ($_COOKIE['mds_viewtype'] == '2') echo checked; ?> /><label for="502">With javascript popups(default)</label><br />
<input type="submit" name="submit" value="Submit" />
<input type="hidden" value="submitted" name="submitted"/>
</form>


<?php
require_once('MDS_includes/footer.php');

?>

Link to comment
https://forums.phpfreaks.com/topic/133745-solved-cookies-problem/
Share on other sites

close...but a couple things..

 

#1 When you use setcookie, the value in $_COOKIE isn't available until the NEXT page load. To get around this, after you set the cookie, send the page back to itself with header(). It won't run recursively, cus when you send it back to itself, it will loose the POST

 

#2 echo checked is missing quotes around 'checked'. and, to be picky, it should be checked=true

 

<?php
   require_once('MDS_includes/header.php');

   if(isset($_POST['submitted'])) {

         $temp = $_POST['links'];
         $t = $temp;
         $expire = time()+60*60*24*30;
         setcookie("mds_viewtype", $t, $expire);
         header('Location: '.$_SERVER['PHP_SELF']);
         exit;
   }
?>

<form action="" method="post">

   <label>How would you like product links to open?</label><br />
      <input type="radio" name="links" value="1" id="501" <?php if ($_COOKIE['mds_viewtype'] == '1') echo 'checked="true"'; ?> /><label for="501">Not in a popup maner</label><br />
      <input type="radio" name="links" value="2" id="502" <?php if ($_COOKIE['mds_viewtype'] == '2') echo 'checked="true"'; ?> /><label for="502">With javascript popups(default)</label><br />
   <input type="submit" name="submit" value="Submit" />
   <input type="hidden" value="submitted" name="submitted"/>
</form>

<?php
   require_once('MDS_includes/footer.php');

?>

Archived

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.