Calver Posted October 8, 2009 Share Posted October 8, 2009 I think this is more of a PHP question than an Ajax one, as I suspect I will have to dump the latter I've written a small custom calculator which involves selecting options from four dropdowns. When any of the options are changed, the result is retrieved via Ajax and displayed on the page - no page refreshes - brilliant! Then I wanted to remember the latest selections for when the visitor next returns to the page - Cookies! After reading some tutorials on Cookies, it soon became apparent that they need to be set at the top of the page, before any html is ouput. As I'm not refreshing the page after the selections are made, this didn't seem possible. So, do I have to dump the Ajax, add a Submit button, and refresh the page in the normal way to set the Cookies? Quote Link to comment https://forums.phpfreaks.com/topic/176993-solved-catch-22/ Share on other sites More sharing options...
mrMarcus Posted October 8, 2009 Share Posted October 8, 2009 have your AJAX run a script that sets the $_COOKIE upon the user's selection. then, just have the system read the cookie each time the user comes to the page/site. no problemos. Quote Link to comment https://forums.phpfreaks.com/topic/176993-solved-catch-22/#findComment-933181 Share on other sites More sharing options...
PFMaBiSmAd Posted October 8, 2009 Share Posted October 8, 2009 If the server is what is setting the cookie (you could set a cookie using javascipt in the bowser), the only requirement is that the header must be sent in a response before any content is sent in a response. The response in question is the one the server sends due to the AJAX http request. The request and response that caused the whole page to be originally displayed is not part of the situation, that request/response completed long ago. You can set a cookie or start/resume a session in the .php code that is the target of your AJAX requests. You can in fact set a cookie/start/resume a session on any request/response, even for something like the request that fetches an image or other media on a page. Quote Link to comment https://forums.phpfreaks.com/topic/176993-solved-catch-22/#findComment-933184 Share on other sites More sharing options...
Calver Posted October 8, 2009 Author Share Posted October 8, 2009 Thank you both very much for the quick replies, which have given me hope Here's my calculator (hyperfocal.php) ... <?php $f = $_GET['focalLength']; $N = $_GET['aperture']; $c = $_GET['sensor']; $unit = $_GET['unit']; // Set the Cookie here ? $H = hyperfocal_distance($f, $N, $c, $unit); $unitstr = "Meters"; if (strtoupper($unit) == "F") $unitstr="Feet"; // Send the result back echo $H . " " . $unitstr ; function hyperfocal_distance($f, $N, $c, $unit) { $div = 1000; if (strtoupper($unit) == "F") $div = 304.8; return round(((pow($f, 2) / ($N * $c)) + $f)/$div, 1, PHP_ROUND_HALF_UP); } ?> Are you saying that I can set the Cookie as soon as I've retrieved the options from the querystring? Quote Link to comment https://forums.phpfreaks.com/topic/176993-solved-catch-22/#findComment-933198 Share on other sites More sharing options...
PFMaBiSmAd Posted October 8, 2009 Share Posted October 8, 2009 Yes, and one thing I was going to add to the above, all the cookies that match the URL being requested via AJAX will be sent to the server on each request, so the existing cookies would be available in the posted code. Quote Link to comment https://forums.phpfreaks.com/topic/176993-solved-catch-22/#findComment-933200 Share on other sites More sharing options...
Calver Posted October 8, 2009 Author Share Posted October 8, 2009 Yes, and one thing I was going to add to the above, all the cookies that match the URL being requested via AJAX will be sent to the server on each request, so the existing cookies would be available in the posted code. OK, I'll have a go at that and report back. I may be some time Quote Link to comment https://forums.phpfreaks.com/topic/176993-solved-catch-22/#findComment-933205 Share on other sites More sharing options...
Calver Posted October 8, 2009 Author Share Posted October 8, 2009 Fantastic! It's working! I'm very happy because I spent all weekend working out how to do the Ajax part Thank you very much for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/176993-solved-catch-22/#findComment-933285 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.