JSHINER Posted September 21, 2006 Share Posted September 21, 2006 Does anyone have a sample code I can look at that sets a cookie upon checking a box. The form will have a text box asking for zip, and an option to remember zip. If it is checked, next time a visitor comes to the site they will go to: www.site.com/index.php?action=search&zip=$zip.I also need the form to send them to that specified search page, which it already does, but I also need it to set the cookie.Any help / pointers would be greatly appreciated. I have never worked with cookies before.Thank you! Link to comment https://forums.phpfreaks.com/topic/21554-cookie-question/ Share on other sites More sharing options...
Daniel0 Posted September 21, 2006 Share Posted September 21, 2006 I haven't tested it, but it should work: [code]<?phpob_start();if(!empty($_POST['zip'])){ echo "Your zip is {$_POST['zip']}"; if($_POST['remember_zip']) { echo "<br />We are remembering your zip code."; setcookie("zip", $_POST['zip'], time()+3600*24*365); // store it for one year }}else { echo <<<EOF<form action='{$_SERVER['REQUEST_URI']}' method='post'><label>Zip: <input type='text' name='zip' /></label><br /><label><input type='checkbox' name='remember_zip' value='1' /> Remember zip code</label><br /><br /><button type='submit'>Submit</button></form>EOF;}ob_end_flush();?>[/code]Note: The reason I can send headers (set a cookie in this example) is that I use output control. Link to comment https://forums.phpfreaks.com/topic/21554-cookie-question/#findComment-96184 Share on other sites More sharing options...
JSHINER Posted September 21, 2006 Author Share Posted September 21, 2006 I need that form to perform an action too... Can that be done? Link to comment https://forums.phpfreaks.com/topic/21554-cookie-question/#findComment-96187 Share on other sites More sharing options...
Daniel0 Posted September 21, 2006 Share Posted September 21, 2006 Sure... what kind of action should be performed? Link to comment https://forums.phpfreaks.com/topic/21554-cookie-question/#findComment-96200 Share on other sites More sharing options...
JSHINER Posted September 21, 2006 Author Share Posted September 21, 2006 The current form:<form action="search2.php?action=search" method="post">Also, in action=search the zip code is validated, so could the cookie only be set if the else is true? Link to comment https://forums.phpfreaks.com/topic/21554-cookie-question/#findComment-96216 Share on other sites More sharing options...
Daniel0 Posted September 21, 2006 Share Posted September 21, 2006 I'm not sure what you mean, but just change [code]if($_POST['remember_zip'])[/code] to [code]if($_POST['remember_zip'] && *do validation stuff here*)[/code](You obviously need to do some validation where it says *do validation stuff here*) Link to comment https://forums.phpfreaks.com/topic/21554-cookie-question/#findComment-96225 Share on other sites More sharing options...
JSHINER Posted September 21, 2006 Author Share Posted September 21, 2006 I need the form to post to the action=search. The validation is there. Link to comment https://forums.phpfreaks.com/topic/21554-cookie-question/#findComment-96234 Share on other sites More sharing options...
Daniel0 Posted September 21, 2006 Share Posted September 21, 2006 Just change the url in the action attribute on this line: [code]<form action='{$_SERVER['REQUEST_URI']}' method='post'>[/code] Link to comment https://forums.phpfreaks.com/topic/21554-cookie-question/#findComment-96237 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.