Help with PHP code radio buttons not working in email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
erinlee
Forum Newbie
Posts: 2
Joined: Fri Jul 12, 2013 11:20 am

Help with PHP code radio buttons not working in email

Post by erinlee »

First a disclaimer; I am so not a web developer or any sort of computer guru. I am the Human Resources/payroll person for the small company that I work for, and somehow have gotten stuck the last few years doing their websites despite all of my kicking and screaming. That said, please excuse my ignorance on these matters, I have tried to teach myself what I can, but I know I am far from being "good" and I'm sure I've made alot of errors along the way that would probably make most of you laugh me out of the room.

The latest project that has been thrust upon me is to try to get an online form working that can be submitted with the results via email. I have managed to get most of the form elements working, but some of the radio buttons are giving me fits. I've been working on this for 2 days and just can't figure out where I'm going wrong, so I'm begging for some help from those more knowledgeable than myself. Hopefully I get the code posted correctly below:

Here is my code on my webpage for the form:

[text]


<form id="form1" name="form1" action="send-email-volunteer-activity-form.php" method="post">





<div align="left"><a name="TemplateInfo"></a>
</div>
<h1 align="left">Volunteer Activity Form</h1>
<p align="left">Current Desert Oasis Hospice Volunteers can now submit your Volunteer Activity Forms online.<br />


<br />

Volunteer Name: <input name="volunteer_name" id="volunteer_name" type="text" value="" size="50" maxlength="50"><br>
Patient's Initials: <input name="patients_initials" id="patients_initials" type="text" size="50" maxlength="50"><br>

Visit Date: <input name="visit_date" id="visit_date" type="text" size="50" maxlength="50"><br><br>

<strong>REMEMBER:</strong><br>
Documentation of your activity should be turned in within 48 hours of your visit to the Volunteer Coordinator.<br><br>

<strong>Type of Service:</strong><br>
<input type="radio" name="type_of_service" id="type_of_service" value="visit">Visit<br>
<input type="radio" name="type_of_service" id="type_of_service" value="refused_visit">Refused Visit<br>
<input type="radio" name="type_of_service" id="type_of_service" value="administrative_support">Administrative Support<br>
<input type="radio" name="type_of_service" id="type_of_service" value="bereavement">Bereavement<br>
<input type="radio" name="type_of_service" id="type_of_service" value="treasure_chest_volunteer">Treasure Chest Volunteer<br>
<input type="radio" name="type_of_service" id="type_of_service" value="volunteer_training">Volunteer Training<br>
<input type="radio" name="type_of_service" id="type_of_service" value="inservice_meeting">In-Service/meeting<br>
Other: <input type="text" name="type_of_service" id="type_of_service" value="" size="100" maxlength="150"><br />
<br />
<strong>Services Provided:</strong><br>
<input type="radio" name="services_provided" id="services_provided" value="patient_support_companionship">Patient Support/Companionship<br>
<input type="radio" name="services_provided" id="services_provided" value="caregiver_support_companionship">Caregiver support/companionship<br>
<input type="radio" name="services_provided" id="services_provided" value="provided_music">Provided Music<br>
<input type="radio" name="services_provided" id="services_provided" value="crafts">Crafts<br>
Other: <input type="text" name="services_provided" id="services_provided" value="" size="100" maxlength="150"><br />
<br>
<strong>Hours Worked: </strong><br>
Start Time:<input name="hours_worked_start_time" id="hours_worked_start_time" type="text" value="" size="10" maxlength="10"> <input type="radio" name="start_time" id="start_time" value="AM" />AM <input type="radio" name="start_time" id="start_time" value="PM" />PM
<br />
End Time: <input name="hours_worked_end_time" id="hours_worked_end_time" type="text" value="" size="10" maxlength="10"> <input type="radio" name="end_time" id="end_time" value="AM" />AM <input type="radio" name="end_time" id="end_time" value="PM" />PM<br>
<br><br>

<strong>Total Time Including Travel Time:</strong> (round to quarter hour)
<input name="total_time" id="total_time" type="text" value="" size="20" maxlength="20"><br>

<br><br>
<strong>Observations/Concerns/Comments: </strong>(for urgent issues please call 602-424-4204 immediately)<br>
<textarea name="observations_concerns_comments" id="observations_concerns_comments" cols="100" rows="30" wrap="hard"></textarea><br />


<br />

<strong> Volunteer's Signature:</strong> <font size="-2">(By typing your full name here, this acts as your signature in an electronic version)</font>
<input name="volunteer_signature" id="volunteer_signature" type="text" value="" size="50" maxlength="50">

Date: <input name="date_signed" id="date_signed" type="text" value="" size="20" maxlength="20"><br><br />
<br />
<br />

<input type="submit" name="submit" id="sumbit" value="Send" />

</form>
[/text]

And here is my PHP send mail code:

Code: Select all

<?php
// loop through every form field
/* 
while( list( $field, $value ) = each( $_POST )) {
   // display values
   if( is_array( $value )) {
      // if checkbox (or other multiple value fields)
      while( list( $arrayField, $arrayValue ) = each( $value ) ){
         echo "<p>' . $arrayValue . '</p>\n";
      }
   } else {
      echo "<p>' . $value . '</p>\n";
   }
}
*/


$email_message = "" ;



$email_message .= '<h2>Volunteer Activity Form </h2>';
$email_message .= '<table width="600" border="0" cellspacing="0" cellpadding="0">';

$email_message .= '<tr>';
$email_message .= '<td width="168" align="left" valign="top">&nbsp;</td>';
$email_message .= '<td width="46" align="left" valign="top">&nbsp;</td>';
$email_message .= '<td width="386" align="left" valign="top">&nbsp;</td>';
$email_message .= '</tr>';
  
$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">Volunteer Name&nbsp; </td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">' . $_POST['volunteer_name'] . ' </td>';

$email_message .= '</tr>';
  
$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">Patient Initials&nbsp;&nbsp; </td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">' . $_POST['patients_initials'] . '</td>';
$email_message .= '</tr>';
  
$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">Visit Date&nbsp; </td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">' . $_POST['visit_date'] . ' </td>';
$email_message .= '</tr>';
  
$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '</tr>';


$email_message .= '<tr>';
$email_message .= '<td colspan="3" align="left" valign="top"><h3>Services:</h3></td>';
$email_message .= '</tr>';


$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">Type of Service</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">';
$email_message .=  $_POST['type_of_service'] ;

$email_message .= '</td>';
$email_message .= '</tr>';

    
$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">Services Provided</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">' . $_POST['services_provided'] . ' </td>';

$email_message .= '</tr>';

$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '</tr>';

$email_message .= '<tr>';
$email_message .= '<td colspan="3" align="left" valign="top"><h3>Hours Worked:</h3></td>';
$email_message .= '</tr>';

$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">Start Time</td>';
$email_message .= '<td align="left" valign="top">' . $_POST['hours_worked_start_time'] . '</td>';
$email_message .= '<td align="left" valign="top">' . $_POST['start_time'] . '</td>';
$email_message .= '</tr>';

$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">End Time</td>';
$email_message .= '<td align="left" valign="top">' . $_POST['hours_worked_end_time'] . '</td>';
$email_message .= '<td align="left" valign="top">' . $_POST['end_time'] . '</td>';
$email_message .= '</tr>';

$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">Total time including travel time</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">' . $_POST['total_time'] . ' </td>';
$email_message .= '</tr>';


$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '</tr>';

$email_message .= '<tr>';
$email_message .= '<td colspan="3" align="left" valign="top"><h3>Observations/Concerns/Comments:</h3></td>';
$email_message .= '</tr>';

$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">Comments:</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">' . $_POST['observations_concerns_comments'] . ' </td>';
$email_message .= '</tr>';

$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '</tr>';



$email_message .= '<tr>';
$email_message .= '<td colspan="3" align="left" valign="top"><h3>Volunteer Signature:</h3></td>';
$email_message .= '</tr>';



$email_message .= '<tr>';
$email_message .= '<td colspan="2" align="left" valign="top">Volunteers Signature: (By typing your full name here, this acts as your signature in an electronic version)</td>';
$email_message .= '<td align="left" valign="top">' . $_POST['volunteer_signature'] . ' </td>';
$email_message .= '</tr>';


$email_message .= '<tr>';
$email_message .= '<td align="left" valign="top">Date Signed</td>';
$email_message .= '<td align="left" valign="top">&nbsp;</td>';
$email_message .= '<td align="left" valign="top">' . $_POST['date_signed'] . ' </td>';
$email_message .= '</tr>';


$email_message .= '</table>';


// EDIT THE 2 LINES BELOW AS REQUIRED
// $email_to = "ejaehnig@geriatricsolutionsllc.com";
$email_to = "test@desertoasishospice.com";
$email_subject = "Volunteer Activity Form";
$email_from = "test@desertoasishospice.com";

 

// create email headers
$headers = 'From: '.$email_from."\r\n".
$headers .= "Content-type: text/html\r\n"; 
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 





// success. sends you to the thank you pge 
header( 'Location: /volunteeractivityformsubmission.htm' ) ;
?>
 
<!-- include your own success html here -->
 
Thank you for submitting your Volunteer Activity Form.  We greatly appreciate your continued service to Desert Oasis Hospice patients and their families!
 
And here is what my emails look like:

[text]Volunteer Activity Form

Volunteer Name Jane Smith
Patient Initials WB
Visit Date 7/5/13

Services:
Type of Service
Services Provided

Hours Worked:
Start Time 4:00 PM
End Time 4:30 PM
Total time including travel time 0.5

Observations/Concerns/Comments:
Comments: none

Volunteer Signature:
Volunteers Signature: (By typing your full name here, this acts as your signature in an electronic version) Jane Smith
Date Signed 7/12/13
[/text]

So you can see my radio buttons for the AM/PM time are working in the email, but the radio buttons for "type of service" and "services provided" are not.

HELP! What am I doing wrong?

Thank you greatly in advance!!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with PHP code radio buttons not working in email

Post by requinix »

Code: Select all

<input type="radio" name="type_of_service" id="type_of_service" value="visit">Visit<br>
<input type="radio" name="type_of_service" id="type_of_service" value="refused_visit">Refused Visit<br>
<input type="radio" name="type_of_service" id="type_of_service" value="administrative_support">Administrative Support<br>
<input type="radio" name="type_of_service" id="type_of_service" value="bereavement">Bereavement<br>
<input type="radio" name="type_of_service" id="type_of_service" value="treasure_chest_volunteer">Treasure Chest Volunteer<br>
<input type="radio" name="type_of_service" id="type_of_service" value="volunteer_training">Volunteer Training<br>
<input type="radio" name="type_of_service" id="type_of_service" value="inservice_meeting">In-Service/meeting<br>
Other: <input type="text" name="type_of_service" id="type_of_service"  value="" size="100" maxlength="150"><br />
If I were to select "Visit" then PHP would receive raw form data that looks like

Code: Select all

type_of_service=visit&type_of_service=
The first value is the radio button I chose and the second is what came from the textbox. Since PHP doesn't do multiple values for a single field the textbox's (empty) value will overwrite the radio button's value in $_POST.

Give the textbox a different name from the radio buttons and adjust your PHP so that it knows when to use the radio button and when to use the textbox. Typically that's done by using an "Other" radio button:

Code: Select all

<input type="radio" name="type_of_service" value="other">Other: <input type="text" name="type_of_service_other" value="" size="100" maxlength="150"><br />

Code: Select all

if ($_POST["type_of_service"] == "other") {
    $type_of_service = "Other: " . $_POST["type_of_service_other"];
} else {
    $type_of_service = $_POST["type_of_service"];
}
Couple other comments to consider:
- Did you see that I didn't include an ID on the "Other" radio button? You cannot use the same ID in more than one place in HTML. You would have to give them all different IDs such as "type_of_service_visit", "type_of_service_refused_visit", and so on, but you don't have anything in the HTML that actually needs those IDs to be present (note that in forms only the field name matters) so I'd suggest you just remove all of them. They're unnecessary.
- Ever noticed that some forms on sites let you click the text for something and automatically get the corresponding checkbox or radio button to select? Have you noticed that your form doesn't do that and you have to click the actual radio button itself? It's very easy to add that in: if your radio button and text are right next to each other then wrap them in a <label>.

Code: Select all

<label><input type="radio" name="type_of_service" id="type_of_service" value="visit">Visit</label><br>
For the "Other", put the textbox in the <label> too so that trying to use the textbox will automatically select the radio button.

Code: Select all

<label><input type="radio" name="type_of_service" value="other">Other: <input type="text" name="type_of_service_other" value="" size="100" maxlength="150"></label>
erinlee
Forum Newbie
Posts: 2
Joined: Fri Jul 12, 2013 11:20 am

Re: Help with PHP code radio buttons not working in email

Post by erinlee »

OMG! Thank you a million times over! I did everything you suggested and it works flawlessly!!!!!

I really appreciate it! I can stop beating my head on my desk now!! :)
Post Reply