2

I have used this code to save the RSForm data in a cookie and if the user tries to fill in the same form again, the previously entered data is loaded as default values:

Script called on form display:

// COOKIES
$inputCookie  = JFactory::getApplication()->input->cookie;
$bdayd = $inputCookie->get('rsformdatabdayd', '', 'string');
$bdaym = $inputCookie->get('rsformdatabdaym', '', 'string');
$bdayy = $inputCookie->get('rsformdatabdayy', '', 'string');
echo '<script>
        document.getElementById("bdayd").value = "'. $bdayd .'";
        document.getElementById("bdaym").value = "'. $bdaym .'";
        document.getElementById("bdayy").value = "'. $bdayy .'";
</script>';

Script called on form process:

// COOKIES
$inputCookie  = JFactory::getApplication()->input->cookie;
$inputCookie->set('rsformdata', $_POST['form']);
$inputCookie->set('rsformdatacountry', $_POST['form']['country']);
$inputCookie->set('rsformdataaddress-level1', $_POST['form']['address-level1']);
$inputCookie->set('rsformdatastudies', $_POST['form']['studies']);
$inputCookie->set('rsformdataemployment', $_POST['form']['employment']);
$inputCookie->set('rsformdataage', $_POST['form']['age']);
$inputCookie->set('rsformdatabdayd', $_POST['form']['bday']['d']);
$inputCookie->set('rsformdatabdaym', $_POST['form']['bday']['m']);
$inputCookie->set('rsformdatabdayy', $_POST['form']['bday']['y']);
$inputCookie->set('rsformdatasuscribe', $_POST['form']['suscribe']);

In the fields, I have these values (I'll only post two as example):

Textbox "name" Default Value:

//<code>
$inputCookie  = JFactory::getApplication()->input->cookie;
$value = $inputCookie->get('rsformdata', '', 'array');
return $value['name'];
//</code>

Dropdown "country" Default value:

//<code>
$options = array('|País de residencia:',
'ES|España',
'MX|México',
'CO|Colombia',
'ZM|Zambia',
'ZW|Zimbabue');

$inputCookie  = JFactory::getApplication()->input->cookie;
$value = $inputCookie->get('rsformdatacountry', '', 'array');

$items = ""; 
foreach ($options as $options_item) {
 $items .= $options_item;
 $active = explode('|', $options_item);
 if (in_array($active[0], $value)) {
  $items .= "[c]\n";
 } else {
  $items .= "\n";
 }
}

return trim($items);
//</code>

The code successfully updates the form fields with the preserved values. If someone has already filled in the form, the values are loaded, but I get these php warnings and notices:

[28-Aug-2019 17:08:31 Europe/Berlin] PHP Warning:  Illegal string offset 'address-level2' in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 4
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Notice:  Uninitialized string offset: 0 in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 4
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Warning:  Illegal string offset 'street-address' in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 4
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Notice:  Uninitialized string offset: 0 in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 4
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Warning:  Illegal string offset 'postal-code' in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 4
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Notice:  Uninitialized string offset: 0 in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 4 
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Warning:  in_array() expects parameter 2 to be array, string given in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 24
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Warning:  in_array() expects parameter 2 to be array, string given in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 24
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Warning:  in_array() expects parameter 2 to be array, string given in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 24
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Warning:  in_array() expects parameter 2 to be array, string given in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 24
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Warning:  in_array() expects parameter 2 to be array, string given in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 24
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Warning:  in_array() expects parameter 2 to be array, string given in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 99
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Warning:  in_array() expects parameter 2 to be array, string given in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 99 
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Warning:  in_array() expects parameter 2 to be array, string given in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 13
[28-Aug-2019 17:08:31 Europe/Berlin] PHP Warning:  in_array() expects parameter 2 to be array, string given in /home/aacom/public_html/test/administrator/components/com_rsform/helpers/field.php(67) : eval()'d code on line 13

Any idea how to avoid these notices and warnings?

1

For one, you are declaring:

$inputCookie->set('rsformdatacountry', $_POST['form']['country']);

This means that the rsformdatacountry cookie value is a string (not an array). This is appropriate assuming that your form is not allowing multiple selections.

When you call it, use:

$activeCountry = $inputCookie->get('rsformdatacountry', '', 'string');

Then when making your comparisons in the loop, use a different check:

foreach ($options as $i => $options_item) {
    if (strpos($options_item, $activeCountry . '|') === 0) {
        $options[$i] .= "[c]";  // concatenate the selected marker
    }
}
return implode("\n", $options);

To explain: if (strpos($options_item, $active . '|') === 0) { it will check the short value entirely. it appends a pipe to ensure partial matches do not occur. the === 0 means the match must occur at the very start of the string. For these reasons, it will be stable and reliable.

As for any other warnings, use var_export($variable_name) to get a clearer understanding of what data you are processing.

  • I am not sure how to use your code. I have tried but doesnt works. Maybe my programmer level is too low. I think we will need to hire a freelancer o similar. thanks anyway – Sukinoz Aug 29 at 16:09
  • I am confident that we can work this out. I am just confused about which "working" script is actually updating the form with the cookie data. You see, you are employing two different techniques. You have the first snippet updating the form with javascript (this is client-side scripting). Your 3rd and 4th snippets are trying to do the same thing with php (this is server-side scripting). My current answer assumes that your server-side technique is working, but now I am thinking that only the javascript is working and that the php is just dead weight. Remove the php handling and let me know. – mickmackusa Aug 29 at 20:39
  • If removing the php form updating scripts eliminates the errors and the javascript updates the form, let me know and I'll adjust my answer. – mickmackusa Aug 29 at 20:40
  • Two different freelancers have try to solve it, and both have cancelled the project without solving it. – Sukinoz Sep 3 at 18:56
1

You have posted snippets that are using two separate means of updating the form.

The javascript-leveraging snippet is updating the form AFTER the form elements have already been generated. This means that the standard form for all users is adjusted at the last moment to apply the cookie values to the fields for that specific user -- seems like the right idea.

The pure php snippet is amending the stored rsform form data (the piped syntax) BEFORE it is rendered as html. The form structure/options are shared by all users, so amending it for each user may be possible, it is probably not ideal.

If your form is correctly being updated with the cookie data AND the server-side technique is generating errors (not working), then you should simply remove the server-side scripting.

To help streamline/stabilise your code, and avoid declaring your many cookie data variables individually, I recommend getting and setting all of the cookie data as a multidimensional array like this: (untested)

JFactory::getApplication()->input->cookie->set('rsformdata', $_POST['form']);

and

$cookieData = JFactory::getApplication()->input->cookie->get('rsformdata', [], 'array');
$cookieJson = json_encode($cookieData);

echo "
<script>
let cookieObj = $cookieJson;
if (cookieObj.country !== undefined) {
    document.getElementById('country').value = cookieObj.country;
}
if (cookieObj.bday !== undefined) {
    if (cookieObj.bday.d !== undefined) {
        document.getElementById('bdayd').value = cookieObj.bday.d;
    }
    if (cookieObj.bday.m !== undefined) {
        document.getElementById('bdaym').value = cookieObj.bday.m;
    }
    if (cookieObj.bday.y !== undefined) {
        document.getElementById('bdayy').value = cookieObj.bday.y;
    }
}
</script>
";

I didn't type out all of your form fields -- just enough to show you how to check and access the multi-dimensional data. This technique is going to serve you well because it will not break when you apostrophes/quotes in your preserved values.

0

The error messages appears to be for fields that you have not provided a value for in your "Script called on form process:" such as adress-level2 so I expect that com_rsforms/helper/field.php is expecting a value in an array, like the other fields you do provide, but you are giving it an empty or no value at all.

0

this is a solution tested and running ok.

(PD: The solution seems to be the use (or abuse) of the @ to avoid the warnings, and I am not sure if this can be considered a real solution or not)

Script called on form display:

//COOKIES
$inputCookie = JFactory::getApplication()->input->cookie;
$bdayd = $inputCookie->get('rsformdatabdayd', '');
$bdaym = $inputCookie->get('rsformdatabdaym', '');
$bdayy = $inputCookie->get('rsformdatabdayy', '');

echo '<script>
        jQuery( document ).ready( function() {
            document.getElementById("bdayd").value = "'. $bdayd .'";
            document.getElementById("bdaym").value = "'. $bdaym .'";
            document.getElementById("bdayy").value = "'. $bdayy .'";
        } );

</script>';

Script called on form process:

// COOKIES
$inputCookie = JFactory::getApplication()->input->cookie;
@$inputCookie->set('rsformdata', $_POST['form']);
@$inputCookie->set('rsformdatacountry', $_POST['form']['country']);
@$inputCookie->set('rsformdataaddress-level1', $_POST['form']['address-level1']);
@$inputCookie->set('rsformdatastudies', $_POST['form']['studies']);
@$inputCookie->set('rsformdataemployment', $_POST['form']['employment']);
@$inputCookie->set('rsformdataage', $_POST['form']['age']);
@$inputCookie->set('rsformdatabdayd', $_POST['form']['bday']['d']);
@$inputCookie->set('rsformdatabdaym', $_POST['form']['bday']['m']);
@$inputCookie->set('rsformdatabdayy', $_POST['form']['bday']['y']);
@$inputCookie->set('rsformdatasuscribe', $_POST['form']['suscribe']);

Textbox "name" Default Value:

//<code>
$inputCookie  = JFactory::getApplication()->input->cookie;
$value = $inputCookie->get('rsformdata', '', 'array');

if(isset($value['name'])){
       return $value['name'];
}else{
       return '';
}

//</code>

Dropdown "country" Default value:

//<code>
$options = array('|País de residencia:',
'ES|España',
'MX|México',
'YE|Yemen',
'DJ|Yibuti',
'ZM|Zambia',
'ZW|Zimbabue');

$inputCookie  = JFactory::getApplication()->input->cookie;
$value = $inputCookie->get('rsformdatacountry', '', 'array');

$items = ""; 
foreach ($options as $options_item) {
 $items .= $options_item;
 $active = explode('|', $options_item);
 if (@in_array($active[0], $value)) {
  $items .= "[c]\n";
 } else {
  $items .= "\n";
 }
}

return trim($items);

//</code>
  • 1
    The use of the "stfu operator" is definitely not good coding practice. Was my second posted answer not effective? We don't merely want to help you succeed with quality techniques, we also want future researchers to succeed with quality techniques. – mickmackusa Sep 12 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.