updating a multi value list field from an external form

12 posts by 2 authors in: Forums > CMS Builder
Last Post: December 29, 2015   (RSS)

Hi All,

After doing some forum searching (mostly in post # 2233624)  I thought that I’d found the answer to updating a multi value list field in the accounts table from an external form, but I can’t seem to get the newly checked or unchecked values to update in the database.

To display the existing values I’m using:

And this seems to be working fine.

To update the levels_of_care database I’m using this in the colsToValues array:

$colsToValues['levels_of_care'] = "\t" . implode("\t", $_REQUEST['levels_of_care']) . "\t";

That doesn’t seem to work correctly.

Other fields are updated, but the all multi value list entry check boxes  are cleared and the following error is displayed along with The “Thanks, we've updated your profile!” message:

Warning: implode(): Invalid arguments passed in /home7/ellescho/public_html/dbtct/profile_test2.php on line 55

Any idea what I might be missing?

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By ross - December 28, 2015

Hi Jerry.

Here's an example of what I've used for this in the past

$checkboxString = "";
foreach ($_REQUEST[$field['name']] as $value) {
     $checkboxString .= $value ."\t";
}

$colsToValue[ FIELDNAME ] = $checkboxString;

Let me know if that helps.

Thanks!

$checkboxString = "";

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By ross - December 28, 2015

Hi Jerry.

Here's an example of what I've used for this in the past

$checkboxString = "";
foreach ($_REQUEST[$field['name']] as $value) {
     $checkboxString .= $value ."\t";
}

$colsToValue[ FIELDNAME ] = $checkboxString;

Let me know if that helps.

Thanks!

$checkboxString = "";

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By gkornbluth - December 29, 2015 - edited: December 29, 2015

Hi Ross,

Hope you'll have a moment to look at this again.

In my haste I seen to have left out the form part of the code I'm trying to use.

In the form, for a list field called ‘levels_of_care’ that gets it’s values from the num field in another table and it’s labels from the title field in that same table, I’m using:

<tr>
              <td valign="top">Levels of Care</td>
              <td><?php $fieldname = 'levels_of_care'; ?>
                <?php
if(is_array(@$_REQUEST[$fieldname])){
$fieldValues = $_REQUEST[$fieldname];
}
else{
$fieldValues = explode("\t",trim(@$_REQUEST[$fieldname],"\t"));
}
?>
                <?php $idCounter = 0; ?>
                <?php foreach (getListOptions('accounts', $fieldname) as $value => $label): ?>
                <?php $id = "$fieldname." . ++$idCounter; ?>
                <input type="checkbox" name="<?php echo $fieldname ?>" id="<?php echo $id ?>"
value="<?php echo htmlspecialchars($value) ?>" <?php if(in_array($value,$fieldValues)):?> checked="checked" <?php endif ?>/>
                <label for="<?php echo $id ?>"><?php echo htmlspecialchars($label) ?></label>
                <br/>
                <?php endforeach ?></td>
            </tr>


When I used:

$colsToValues['levels_of_care'] = "\t" . implode("\t", $_REQUEST['levels_of_care']) . "\t";

To update the levels_of_care database in the colsToValues array, it doesn’t seem to work correctly.

Other fields are updated, but the all multi value list entry check boxes  are cleared and the following error is displayed along with The “Thanks, we've updated your profile!” message:

Warning: implode(): Invalid arguments passed in /home7/ellescho/public_html/dbtct/profile_test3.php on line 55


Using what I think you meant in your suggestion, I replaced the code in the colToValues array with:

 $checkboxString = "";
foreach ($_REQUEST['levels_of_care'] as $value) {
     $checkboxString .= $value ."\t";
}
$colsToValue['levels_of_care'] = $checkboxString;


but that throws the error:

Warning: Invalid argument supplied for foreach() in /home7/ellescho/public_html/dbtct/profile_test4.php on line 56


Sorry for the confusion.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By ross - December 29, 2015

Hi Jerry

I think what might be happening is this variable isn't correct: $_REQUEST['levels_of_care']

What I recommend is using "showme($_REQUEST)" to get a full list of variables available to $_REQUIEST and see what the field name is you need to work with.

Let me know how you make out.

Thanks!

$_REQUEST['levels_of_care']

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By ross - December 29, 2015

Hi Jerry

I think what might be happening is this variable isn't correct: $_REQUEST['levels_of_care']

What I recommend is using "showme($_REQUEST)" to get a full list of variables available to $_REQUIEST and see what the field name is you need to work with.

Let me know how you make out.

Thanks!

$_REQUEST['levels_of_care']

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Hi Ross,

Thanks for revisiting this.

Here are the results of the showme

Array ( [save] => 1 [fullname] => My full name
[email] => &#x73;&#x65;&#116;&#x68;&#x2e;&#97;&#120;&#x65;&#108;&#x72;&#111;&#100;&#64;&#121;&#97;&#108;&#101;&#46;&#101;&#100;&#x75;
[username] => saxelrod
[practice_name] => Mental Health Pride
[practice_street_address] => 423 W14th St
[practice_city] => New York [practice_state] => NY
[practice_zip] => 10019
[levels_of_care] => 2
[submit] => Update profile >> )

Interesting thing is that the number after the ['levels of care'] always reflect the last item number checked in the list, but the actual field in the user account record is cleared of all check marks.

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By ross - December 29, 2015

Hi Jerry.

There are likely a couple things happening here. First, the field in your form needs to have square brackets in it like this:

"name=levels_of_care[]"

Next up, are you saying you weren't expecting numbers to be the value? Or was that part ok?

Let me know how you make out.

Thanks!

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By gkornbluth - December 29, 2015 - edited: December 29, 2015

Thanks Ross,

The [] (in red) seems to have done the trick. ( do the brackets turn the values into an array?)

For anyone else following this thread, the final code in the form is:

<tr>
              <td valign="top">Levels of Care</td>
              <td><?php $fieldname = 'levels_of_care'; ?>
               <?php
if(is_array(@$_REQUEST[$fieldname])){
$fieldValues = $_REQUEST[$fieldname];
}
else{
$fieldValues = explode("\t",trim(@$_REQUEST[$fieldname],"\t"));
}
?>
                <?php $idCounter = 0; ?>
                <?php foreach (getListOptions('accounts', $fieldname) as $value => $label): ?>
                <?php $id = "$fieldname." . ++$idCounter; ?>
                <input type="checkbox" name="<?php echo $fieldname ?>[]" id="<?php echo $id ?>"
value="<?php echo htmlspecialchars($value) ?>" <?php if(in_array($value,$fieldValues)):?> checked="checked" <?php endif ?>/>
                <label for="<?php echo $id ?>"><?php echo htmlspecialchars($label) ?></label>
                
                <br/>
                <?php endforeach ?></td>
            </tr>

And in the colsToValues array:

  $colsToValues['levels_of_care'] = "\t" . implode("\t", $_REQUEST['levels_of_care']) . "\t";

Note that in this case I'm pulling the list of possible option values (num) and option labels (text) for the levels_of_care field from another table

Best,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php