Undefined Index Issue, email template placeholder using check box

3 posts by 2 authors in: Forums > CMS Builder
Last Post: November 1, 2018   (RSS)

By gkornbluth - October 30, 2018 - edited: January 2, 2019

Hi All,

PROBLEM SOLVED

I needed to update a form to pass the values of 2 single value check box fields (whether checked or unchecked) to email templates using placeholders and was working on it for most of the day and there’s no joy in Mudville, I’ve still got one issue. thanks to Daniel it's all working now.

If the boxes are checked, they are passed to the email template on submission with no issue, but if they are not checked, they throw ‘undefined index’ errors and the values are not passed.

The checkbox fields are called ‘more_than_one_location_for_practice’ and ‘more_than_one_practitioner_in_practice’

Thanks for looking,

Jerry Kornbluth

REVISED CODE PER DANIEL'S SUGGESTIONS

First I define a concatenated link value for a variable called $addlink with:

// Added 11/1/18 per Daniel at Interactive Tools
$more_than_one_location_for_practice = 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;}

$addLink = "http://dbtproviders.com/cmsAdmin/admin.php?menu=accounts&action=add";
$addLink .= "&username=" .urlencode(@$_REQUEST['username']);
$addLink .= "&email=" .urlencode(@$_REQUEST['email']);
$addLink .= "&contact_first_name=" .urlencode(@$_REQUEST['first_name']);
$addLink .= "&contact_last_name=" .urlencode(@$_REQUEST['last_name']);
$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);

Then lower down on the page I populate the placeholders for the 2 email templates:

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']),
'more_than_one_location_for_practice' => ($more_than_one_location_for_practice),
'more_than_one_practitioner_in_practice' => ($more_than_one_practitioner_in_practice),
'addLink' => $addLink,
)));
$mailErrors = sendMessage($emailHeaders);
if ($mailErrors) { alert("Mail Error: $mailErrors"); }


And

// send email to Applicant
$emailHeaders = emailTemplate_loadFromDB(array(
'template_id' => 'DIRECTORY-LISTING-REQUEST-APPLICANT',

'placeholders' => array(
'contact.firstName' => ($_REQUEST['first_name']),
'contact.lastName' => ($_REQUEST['last_name']),
'more_than_one_location_for_practice' => ($more_than_one_location_for_practice),
'more_than_one_practitioner_in_practice' => ($more_than_one_practitioner_in_practice),

)));
$mailErrors = sendMessage($emailHeaders);
if ($mailErrors) { alert("Mail Error: $mailErrors"); }

Then, in the form I’ve been using:

<form method="post" action="">
<input type="hidden" name="save" value="1" />
<table border="0" cellspacing="0" cellpadding="2">

<tr>
<td width="40%" align="left" valign="middle" class="text_font"><b>What's Your First Name</b></td>
<td style="text-align:left" align="left" valign="middle"><input class="text" type="text" name="first_name" id="first_name" value="<?php echo htmlencode(@$_REQUEST['first_name']); ?>" /></td>
</tr>
<tr>
<td width="40%" align="left" valign="middle" class="text_font"><b>Your Last Name</b></td>
<td style="text-align:left" align="left" valign="middle"><input class="text" type="text" name="last_name" id="last_name" value="<?php echo htmlencode(@$_REQUEST['last_name']); ?>" /></td>
</tr>
<tr>
<td width="40%" align="left" valign="middle" class="text_font"><b>Is There More Than One DBT Trained Practitioner In This Practice? (Check If Yes)</b></td>
<td style="text-align:left" align="left" valign="middle"> <input type="checkbox" name="more_than_one_practitioner_in_practice" value="1" <?php checkedIf('1', @$_REQUEST['more_than_one_practitioner_in_practice']); ?> />
</td>
</tr>
<tr>
<td width="40%" align="left" valign="middle" class="text_font"><b>Is There More Than One Location For This Practice? (Check If Yes)<br />
</b></td>
<td style="text-align:left" align="left" valign="middle">
<input type="checkbox" name="more_than_one_location_for_practice" value="1" <?php checkedIf('1', @$_REQUEST['more_than_one_location_for_practice']); ?> />
</td>
</tr>

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 daniel - October 30, 2018

Hi Jerry,

Checkboxes are not "passed through" form submissions if they are not checked, so an additional step will need to be taken to handle them on the processing end. There are two main options:

First, you can silence errors by prepending the values with @ (e.g. @$_REQUEST['more_than_one_location_for_practice']) like they are in the $addLink definition. The result will be the same (passing through empty values) but it will prevent the errors.

If you want a bit more control over how these values are handled, you can check for the existence of the checkbox and create a new variable with the desired values. Something like this:

$moreThanOneLocation = 0;
if (isset( $_REQUEST['more_than_one_location_for_practice'] )) {
  $moreThanOneLocation = 1;
}

$moreThanOnePractitioner = 0;
if (isset( $_REQUEST['more_than_one_practitioner_in_practice'] )) {
  $moreThanOneLocation = 1;
}

You can replace 0/1 if you'd prefer something else, and then update the placeholder arrays to use the new variables.

An additional thing I noticed, there appear to be spaces before the "=" sign in the last two lines of the $addLink definition. These spaces are likely to cause issues when trying to access these values on the page being linked to, and should probably be removed:

$addLink .= "&more_than_one_location_for_practice=" .urlencode(@$_REQUEST['more_than_one_location_for_practice']); 
$addLink .= "&more_than_one_practitioner_in_practice=" .urlencode(@$_REQUEST['more_than_one_practitioner_in_practice']);

Let me know if you have any more questions!

Thanks,

Daniel
Technical Lead
interactivetools.com