Forum Moderators: coopster

Message Too Old, No Replies

PHP Forms & Cookies

         

stidj

10:58 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



I'm having a strange problem with PHP

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

dcrombie

10:41 am on Aug 2, 2005 (gmt 0)



After the cookie is set (using JavaScript or PHP), the server (where your PHP is running) still doesn't know about it. It's only on the _next_ request to that domain that the cookie values are sent in the request headers.

mcibor

11:20 am on Aug 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have sth similar:

<?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

stidj

2:20 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



Hi guys,

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

mcibor

8:48 am on Aug 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is a way of making a cookie to store form info, but it recquires javascript.

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

stidj

3:22 pm on Aug 3, 2005 (gmt 0)

10+ Year Member



Please post the code, that sounds interesting.

Do you think my idea to use _POST variables still is ok? The idea is that later on the user may have to return to the beginning of the 2 page form and that is when it is important to read the cookies so they don't have to retype everything.

Thanks

mcibor

8:45 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



for working with cookies:
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);
}

mcibor

9:06 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have multiple selects, but you can just simply modify the function:
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];}}


Simply <body onLoad="js_loadTxt()"> and <button onclick="javascript: js_saveTxt();return false;">Save</button>

Best regards
Michal Cibor