Forums

"Saving" entered Data in TextFields w Cookies

mh.schreiber 11 Mar, 2009
Hi,
before I start trying my luck on this projekt, I'd like to figure out if there isn't an easier or more logic way.

Project:
I'm using Chronoforms for a quite simple contactform, but I'd like to allow non-registered users to keep there data -once entered- in a cookie, so they don't need to fill in their name and stuff again and again
Plan:
Using PHP or JS to let ChronoForms set a cookie.
Worries:
- I read PHP must set cookies even before the header, which would be quite hard to realize with the Joomla, right?!
- I'm using more than one form - can they all read&write the same cookie?
- Is there a good howto on cookies in general and on chronoforms&cookies in detail?

many thanks in advance,
mh schreiber
GreyHead 11 Mar, 2009
Hi mh schreiber,

I don't think that there is any great problem with this. I have some multi-part forms that use cookies to remember users between the parts and they work pretty smoothly.

Here's a snippet that both reads and writes a cookie
<?php
// Check uidp cookie, if it's not found then create one
// uidp = Unique IDentifier Pxxx
$uidp = "";
if ( JRequest::getVar('uidp', '', 'cookie', 'string') ) {
    $uidp = JRequest::getString('uidp', '', 'cookie');
} else {
    srand( ( double ) microtime() * 10000 );
    $uidp = "I" . substr( base64_encode( md5( rand() ) ), 0, 16 );
    setcookie('uidp', $uidp);
}
?>

Bob
mh.schreiber 12 Mar, 2009
Hi Bob,
thank you very much for the fast anwer! Your snippet works fine, but I might need another word of explanation.

What your Snippet does is set an unique id, on which a users could be identified. Now how do I do the next step to adress the textfields and save the entered data in the same cookie?

regards,

mh schreiber
GreyHead 12 Mar, 2009
Hi mh.schreiber,

Use the same syntax but add more setcookie('name', 'value'); entries

Bob
mh.schreiber 12 Mar, 2009
Hi Bob,
thanks again for the fast anwer!

Setting Cookies works fine (thanks to your help :-) ), but "reading" them seems to fail. The data stored in the cookie(s) won't show up in the form after a reload.

Might there just be a mistake in my syntax?

On Submit code - before sending email:

<?php
$uidp = "";
if ( JRequest::getVar('uidp', '', 'cookie', 'string') ) {
    $uidp = JRequest::getString('uidp', '', 'cookie');
} else {
    srand( ( double ) microtime() * 10000 );
    $uidp = "I" . substr( base64_encode( md5( rand() ) ), 0, 16 );
    setcookie('uidp', $uidp);
	 }	
if($_REQUEST['form']['vorname']) {
setcookie("vorname", $_REQUEST['form']['vorname']);
}
?>


Within the form-HTML:
<?php

if($_REQUEST['form']['vorname']) {
setcookie("form[vorname]", $_REQUEST['form']['vorname']);
}

if($_COOKIE['form'])
$form = $_COOKIE['form'];

?>


And the input-field

<input type="text" name="form[vorname]" value="<? echo $form['vorname']; ?>" />


Am I blind or is there a serious mistake?
Thanks!
Max_admin 12 Mar, 2009
Hi,

maybe the cookie name cant have [ brackets ?

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
mh.schreiber 13 Mar, 2009
Hi Max,

wouldn't that mean the cookie wouldn't even be set? Because that's the only working part so farπŸ™‚

mh schreiber
Max_admin 13 Mar, 2009
ok, try to debug it line by line, try to cut your code into pieces and print the variables data and compare..etc, it looks a wasting of time but if you can't get through it ?πŸ™‚

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 13 Mar, 2009
Hi mh.schreiber,

Here's a slightly simplified version that works OK. Form HTML
<?php
if( isset($_REQUEST['vorname'])) {
  setcookie("vorname", $_REQUEST['vorname']);
}
if( isset($_COOKIE['vorname']) ){
  $vorname = $_COOKIE['vorname'];
} else {
  $vorname = "Nicht gefunden";
}
?>
<input type="text" name="vorname" value="<?php echo $vorname; ?>" />
<br />
<input type="submit" name="submit" value="Submit" />
On Submit
<?php
if ($_REQUEST['vorname'] ) {
  setcookie("vorname", $_REQUEST['vorname']);
}
?>

I removed the arrays - they don't seem to work in cookies and they created some PHP Warnings in ChronoForms. If you need to do a bunch of these you can do them using a for each loop, possibly packing the results array into a single cookie if you need to.

Bob
nml375 13 Mar, 2009
Been a while since I played around with cookies and php, but as I recall, the only implication of having [] in the name, is that php will try and parse it as an array. As such, your code would seem proper.

Now, looking at your code, it appears to me, that you are setting the cookie 'vorname', but attempt to read the cookie 'form' (doesn't exist), and index 'vorname' of that.
Also, I'm wondering whether you really should be setting the cookie when the form loads, as there should be no input from the user there.

Solution: set and read the same cookie.

Edit: See you beat me to it again, Bob :wink:
Edit2: Have you put any thought of using sessions (JSession) with custom sid's?
Max_admin 13 Mar, 2009

Have you put any thought of using sessions (JSession) with custom sid's?



I prefer sessions for doing this, but sometimes cookies are needed, if cookies are not necessary here then sessions are definitely better!πŸ™‚

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 14 Mar, 2009
Hi,

I thought the idea here was to give the cookie quite a long life so that if the user returned (a few weeks later maybe) their data would be available. I don't know much about sessions but don't they have a pretty short life?

I've handled this in the past by saving the data in the database but putting a unique id into a cookie, and cross-checking the email in case they use another computer. That's simpler than saving all the data in the cookie.

Bob
nml375 14 Mar, 2009
With php-sessions, the lifetime of the session is entirely up to the coder, and the sid is usually stored within a cookie (unless transparent sid is enabled).
I'm not sure how joomla would respond to creating custom JSession objects rather than retrieving the existing one from JFactory::getSession(), as the expire time setting may only be defined when the session object is created. So I guess this would be a dead-end for long-time session data.

I suppose one could create a psuedo-session class for storage, mimicking the behaviour of joomla/php sessions.
Max_admin 14 Mar, 2009
Hi,

yes, I think for long time and non sensitive apps, cookies are preferred!

I think there is a problem here with the 2 dimensional cookie/request variables, it needs some debugging!

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
tonny 20 Jun, 2009
I've been playing with cookies to refill textboxes. This works fine. How can I do the same with textareas?
GreyHead 20 Jun, 2009
Hi tonny,

I imagine in the same way as an input field except that the value goes between the tags
<textarea . . . >Some value here</textarea>


Bob
tonny 22 Jun, 2009
Hi Greyhead,

Thanks, this was what I needed. At first I used a value, but this did not work.

Tonny
This topic is locked and no more replies can be posted.