简体   繁体   中英

Echoing a variable in php gives one number and setting the variable to a cookie and evaluating the cookie gives another?

I have a variable in a post array called childnumber, which was a javascript variable sent to the server via an ajax post. In the php script it is here:

$newValue = $_POST["childnumber"]+1;

I have added 1 to it. Echoing $newvalue in the php script and sending it back to the client via the ajax response gives the correct number on the clientside html. But if I set the cookie

setcookie($cookieName,$newValue);

in the php instead of echoing it back to the client and check the cookies clientside the cookiename is correct but the new value comes out as just '1' everytime. So how do I get the cookie value to be the value which is echoed back to the client?

Thanks

As I understand your process, you send an asynchronous request, process it by setting a cookie and... what do you send back?

I might be wrong (and be glad for any clarification from others) but does not the cookies are set along the http headers and therefore are accessible after you "reload" or redirect to new/same page?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM