Problem passing an array of check boxes to a record using $addLink and an email template (Solved)
3 posts by 2 authors in: Forums > CMS Builder
Last Post: April 10, 2019 (RSS)
By gkornbluth - April 4, 2019 - edited: April 10, 2019
Hi All,
I’ve been using a series of $addLink entries together with an email template to develop a link that’s emailed to the admin which will create a record in my ‘accounts' database when clicked on, (see the old post at https://www.interactivetools.com/forum/forum-posts.php?postNum=2240565 ) and it’s working fine for text fields, single check boxes and dates.
I was having trouble creating the $addlink entry that will pass an array of check boxes (a list of check box fields created by a ‘get options from database’ query) to the database and what placeholder to use in the email template, and Daniel Loewe came to the rescue.
Thanks,
Jerry Kornbluth
Here’s the new entry in the form code that creates the check boxes to be checked (or not) and then passed to the admin through the email template:
<?php
$fieldname = 'dbt_training_support'; ?>
<?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 ?>
And here’s the updated $addlink and template code:
// add user
if (!$errorsAndAlerts) {
$more_than_one_location_for_practice = 0;
$more_than_one_practitioner_in_practice = 0;
$more_than_one_location_for_practicea = 0;
$more_than_one_practitioner_in_practicea = 0;
if (isset( $_REQUEST['more_than_one_location_for_practice'] )) {$more_than_one_location_for_practice = 1;}
$more_than_one_practitioner_in_practice = 0;
if (isset( $_REQUEST['more_than_one_practitioner_in_practice'] )) {$more_than_one_practitioner_in_practice = 1;}if ($more_than_one_location_for_practice === 0) {
$more_than_one_location_for_practicea = 'there is only one location for this practice.';
}
elseif ($more_than_one_location_for_practice === 1) {
$more_than_one_location_for_practicea = 'there are multiple locations for this practice.';
}
if ($more_than_one_practitioner_in_practice === 0) {
$more_than_one_practitioner_in_practicea = 'there is only one practitioner in this practice.';
}
elseif ($more_than_one_practitioner_in_practice === 1) {
$more_than_one_practitioner_in_practicea = 'there are multiple practitioner in this practice.';
}$addLink = "http://dbtproviders.com/cmsAdmin/admin.php?menu=accounts&action=add";
$addLink .= "&more_than_one_location_for_practice=" .urlencode(@$more_than_one_location_for_practice);
$addLink .= "&more_than_one_practitioner_in_practice=" .urlencode(@$more_than_one_practitioner_in_practice);
$addLink .= "&username=" .urlencode(@$_REQUEST['username']);
$addLink .= "&email=" .urlencode(@$_REQUEST['email']);
$addLink .= "&password=" .'2FE3Ur';
$addLink .= "&contact_first_name=" .urlencode(@$_REQUEST['first_name']);
$addLink .= "&contact_last_name=" .urlencode(@$_REQUEST['last_name']);
$addLink .= "&practice_name=" .urlencode(@$_REQUEST['practice_name']);
$addLink .= "&approved=".'1';
$addLink .= "&practice_street_address=" .urlencode(@$_REQUEST['practice_street_address']);
$addLink .= "&practice_room_or_floor=" .urlencode(@$_REQUEST['practice_room_or_floor']);
$addLink .= "&practice_city=" .urlencode(@$_REQUEST['practice_city']);
$addLink .= "&practice_state=" .urlencode(@$_REQUEST['practice_state']);
$addLink .= "&practice_zip=" .urlencode(@$_REQUEST['practice_zip']);
$addLink .= "&practice_phone=" .urlencode(@$_REQUEST['practice_phone']);
if (!empty( $_REQUEST['dbt_training_support'] )) {
foreach ($_REQUEST['dbt_training_support'] as $value) {
$addLink .= '&dbt_training_support[]=' . urlencode($value);
}
}
// send email to Admin
$emailHeaders = emailTemplate_loadFromDB(array(
'template_id' => 'DIRECTORY-LISTING-REQUEST',
'placeholders' => array(
'contact.firstName' => ($_REQUEST['first_name']),
'contact.lastName' => ($_REQUEST['last_name']),
'contact.email' => ($_REQUEST['email']),
'contact.username' => ($_REQUEST['username']),
'practice' => ($_REQUEST['practice_name']),
'street' => ($_REQUEST['practice_street_address']),
'room' => ($_REQUEST['practice_room_or_floor']),
'city' => ($_REQUEST['practice_city']),
'state' => ($_REQUEST['practice_state']),
'zip' => ($_REQUEST['practice_zip']),
'more_than_one_location_for_practicea' => ($more_than_one_location_for_practicea),
'more_than_one_practitioner_in_practicea' => ($more_than_one_practitioner_in_practicea),
'phone' => ($_REQUEST['practice_phone']),
'training' => ($_REQUEST['training_experience']),
'addLink' => $addLink,
)));
$mailErrors = sendMessage($emailHeaders);
if ($mailErrors) { alert("Mail Error: $mailErrors"); }
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Problem passing an array of check boxes to a record using $addLink and an email template
By daniel - April 10, 2019
Hi Jerry,
When using multiple checkboxes like that, it will return an array of selected results - rather than a string - so it takes a different method to process. Try adding this code after the block of $addLink assignments:
if (!empty( $_REQUEST['fieldname'] )) {
foreach ($_REQUEST['fieldname'] as $value) {
$addLink .= '&fieldname[]=' . urlencode($value);
}
}
You'll need to replace "fieldname" with the name of the checkbox field you're using.
Let me know if that does the trick!
Thanks,
Technical Lead
interactivetools.com
Problem passing an array of check boxes to a record using $addLink and an email template
By gkornbluth - April 10, 2019
Hi Daniel,
Worked like a charm.
Of course it's only easy when you know what you're doing.
Have a good evening.
I updated the code above to reflect the changes.
Thanks as always,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php