Jump to content

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/176993-solved-catch-22/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/176993-solved-catch-22/#findComment-933184
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/176993-solved-catch-22/#findComment-933198
Share on other sites

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 :)

 

Link to comment
https://forums.phpfreaks.com/topic/176993-solved-catch-22/#findComment-933205
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.