Forum Moderators: coopster
Basically I have a form that calls itself in PHP
if ( $form_submit == true && $errors == true )
{
echo $the_form_again;
}
I have a function that upon submission of the forms gathers all of the $_POST variables and sets them as cookies. I think I'm missing something about how this works but if I submit a value "5" the cookie shown is null until I refresh or resubmit again.
Then after submitting again the cookie is set to the value 5
I hope that is clear enough, can anyone explain why it seems the cookie values aren't what I expect after submitting the form and immediately storing them into cookies?
Thanks
<?php
if($_POST["submit"] == "Submit")
{
foreach($_POST as $key=>$value)
{setcookie($key, $value, time()+3600);}
echo "Thank you for submitting the values";
}
else
{?>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="POST">
<input type="text" name="name"><br><input type="submit" name="submit" value="Submit"></form>
<?php }?>
This should work as expected - generate cookie on the first submit.
Michal Cibor
Thank you for your reply.
My code is similar to what you mentioned. Basically if the form is submitted it will always create the cookies based on the $_POST array.
The problem unfortunately is that it seems dcrombie is correct and that although the cookie is generated after the submit, the script doesn't seem to know about it until the next refresh which means if I am not mistaken, it is not possible to create and read cookies in one go.
In other words I am correct in thinking that I can't use cookies to fill in form values immediately just like most people use the $_POST variables?
Let's try to learn from this :)
So if you need the values on the first go, don't try to get them from cookies you just set (since they won't appear until the next submit/refresh/reload).
Instead I guess I could use $_POST variables to fill in form values and not take the data from the cookies until I need it on a different form/page?
If anyone has a better idea or knows of a way to use cookies on the same form then let me know if I'm missing something here :)
Thanks
On submit button (I have a <button, not <input type="submit") I call javascript function, that stores data into a cookie.
Then on page load I have a function that reads the cookie and fills the form.
If you wish I can write here the coding
Best regards
Michal Cibor
function setCookie(name, value, expires, path, domain, secure) {
var varCookie = name + "=" + escape(value) +
((expires)? "; expires=" + expires.toGMTString() : "") +
((path)? "; path=" + path : "") +
((domain)? "; domain=" + domain : "") +
((secure)? "; secure" : "");
document.cookie = varCookie;
}function getCookie(name) {
var varCookie = document.cookie;
var prefix = name + "=";
var begin = varCookie.indexOf(prefix);
if (begin == -1)
return null;
var end = varCookie.indexOf(";", begin);
if (end == -1)
end = varCookie.length;
return unescape(varCookie.substring(begin + prefix.length, end));
}function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path)? "; path=" + path : "") +
((domain)? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}function fixDate(date) {
var base = new Date(0);
var delay = base.getTime();
if (delay > 0)
date.setTime(date.getTime() - delay);
}
var frm = document.getElementsByTagName("form");var sel = document.getElementsByTagName("select");
function js_saveTxt(){
var f = sel[0].value + "%2" +
sel[1].value + "%2";
var now = new Date();
fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
setCookie("rapTxt", f, now);
frm[0].submit();}function js_loadTxt(){
var begin = 0;
var end = 0;
var f = new Array(2);
if(document.cookie.indexOf("rapTxt=")!= -1){
var str = getCookie("rapTxt");
for(i = 0; i <= 1; i++){
end = str.indexOf("%2", begin);
f[i] = str.substring(begin, end);
begin = end + 2;}
sel[0].value = f[1];
sel[1].value = f[2];}}
Best regards
Michal Cibor