Data to session or cookies

jorgve 30 Sep, 2016
Hi,

I'd like to know a little bit more about the "session to data" function, I've been reading the last paragraph of this FAQ which describes what I'm looking for, but I still can't figure it out:
https://www.chronoengine.com/faqs/63-cfv4/cfv4-working-with-form-data/2697-how-can-i-use-the-form-data.html

Basically I´ve got 50+ forms, all of these forms have a hidden field called "source". I use this field to identify the source of the lead, facebook, google, etc. and it will be passed on to our CRM.

All the hidden "source" fields are empty, so at the moment I populate them as following:
www.mydomain.com/landingpage-google/?source=google

And this works fine, when the landing page doesn't have any links, when it does.. I mark them with "?source=google", but when they reach to the next page.. this parameter will be lost obviously.

So I'd like to use the "Session to data" option to, when a certain form is loaded, it will store a value of "source" for a certain amount of time which will populate the hidden field of all other forms..

Am I looking in the right direction by using the "Data to session"?

Thanks in advance.
GreyHead 30 Sep, 2016
Hi jorgve,

Joomla! has a User session that is kept open fro as long as the user is active on the site. Actually for a time after that set in your Global Configuration - it defaults to 15 minutes.

You can save data to the User session using the Data to Session actions - though they are designed to save all of the form data. It might be better to use a Custom Code action in the On Load event of your forms to save just the source value. This would need to be before the HTML (Render form) action.

Here's one way to code this - it gives priority to a new value of source over a saved one.
<?php
$jsession = \JFactory::getSession();
$source = $jsession->get('source', '');
if ( !empty($form->data['source']) ) {
  $source = $form->data['source'];
}
$jsession->set('source', $source);
$form->data['source'] = $source;
?>

Bob
jorgve 04 Oct, 2016
Hi GreyHead,

Thanks for your reply, do you also know if there is a similar solution for Wordpress?
I see that maybe I posted this in the wrong topic, I'm sorry for that.

All the best,
Jorg
GreyHead 04 Oct, 2016
1 Likes
Hi Jorg,

Not exactly the same - I'm not very familiar with CF in WordPress, but you could use code like this example I found.

Bob
jorgve 05 Oct, 2016
Thanks for your fast reply!

I've saw that example, but I can't really get it to work, since I don't have a lot of php knowledge.

I´ve put in the functions.php:

add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');

function myStartSession() {
    if(!session_id()) {
        session_start();
    }
}

function myEndSession() {
    session_destroy ();
}


And in a Custom Code action in the On Load event:

$_SESSION['source'] = "source_test";


But it isn't working..

Not really sure where and how to get the data using the code:
if(isset($_SESSION['source'])) {
    $value = $_SESSION['source'];
} else {
    $value = '';
}
GreyHead 05 Oct, 2016
Hi jorgve,

Please try
<?php
if(isset($_SESSION['source'])) {
    $form->data['value_name'] = $_SESSION['source'];
} else {
    $form->data['value_name'] = '';
}
?>
in a Custom Code action where you want to access the saved data.

Bob
jorgve 05 Oct, 2016
Many thanks for the code GreyHead, but unfortunately it isn't working..

I didn't change de code in the functions.php (altough I did some variations which I found on other websites), and I've put in a Custom Code action in the On Load event:

<?php

$_SESSION['source'] = "source_test";

if(isset($_SESSION['source'])) {
    $form->data['value_name'] = $_SESSION['source'];
} else {
    $form->data['value_name'] = '';
}

?>


When I enable the debugger, it just shows the field "source" empty, it doesn't give any error.
I don't know if I'm doing something wrong, or if you have any suggestion?
GreyHead 05 Oct, 2016
Hi jorgve,

You may need to check the PHP manual. Does this version work in the If clause?
if( !empty($_SESSION['source']) ) {

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