1

I would like to get my php form to display which radio boxes have been checked. In other words when someone contacts me via email through my website, i would like it to display which radio box they ticked. I've tried everything but cannot get it to work!

HTML CODE SO FAR -

<table width="308" border="0">
      <label>
        <input type="radio" name="servicetype[]" value="checkbox" id="Technical Support" />
        Technical Support</label>

      <label>
        <input type="radio" name="servicetype[]" value="checkbox" id="Information Services" />
        Information Services</label>
  <tr>
    <td align="left">
   <input name="Submit" type="submit" id="Submit" class="contact-class2" value="Send"/>
    </td>
  </tr>
</table>
    </form>

PHP SO FAR

<?php
 $field_name = $_POST['cf_name'];
 $field_email = $_POST['cf_email'];
 $field_message = $_POST['cf_message'];

 $mail_to = 'www.example.com';
 $subject = 'hello';

 $servicetype = join(", ", $_REQUEST["servicetype"]);
 if (isset($_POST['servicetype'])) {
    foreach ($_POST['servicetype'] as $key => $val) {
        // do what you need
    }
 }


 $body_message = ''.$field_message."\n"."\n";
 $body_message .= 'From: '.$field_name;

 $headers = 'From: '.$field_email."\r\n";
 $headers .= 'Reply-To: '.$field_email."\r\n";

 $mail_status = mail($mail_to, $subject, $body_message, $headers  );



 if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        window.location = 'Contact form.html';
    </script>
 <?php
 }
 else { ?>
    <script language="javascript" type="text/javascript">
        window.location = 'Contact form.html';
    </script>
 <?php
 }
 ?>
2
  • $_POST will get the value attribute of your radio buttons, not the id. Also, if they're radio buttons, do you need to send an array? Because you can only pick a single one. Commented May 14, 2013 at 17:26
  • At the moment when i send my form it only displays the field names and headers etc but doesn't display any of the check boxes. That whole field is completely blank. I'm super novice at PHP so i need help desperately :( Commented May 14, 2013 at 17:57

1 Answer 1

2

You've built your checkbox inputs incorrectly. The value attribute should be the displayed "name" of the checkbox, e.g.

    <input type="checkbox" name="servicetype[]" value="Technical Support" id="Technical Support" />
                 ^^^^^^^^                              ^^^^^^^^^^^^^^^^^

It's that value that gets submitted along with the form. As it stands now, you'd simply be submitting the word "checkbox" and would have no way of knowing WHICH checkbox(es) were selected.

Your email would simply be:

$body_message = "blah blah blah. Requested services: " . implode(',' $_POST['servicetype']);
Sign up to request clarification or add additional context in comments.

2 Comments

What exactly does "implode" mean? What do you think would be the best way to show which checkbox was selected in an email? I don't think the PHP coding i used to display which checkbox was selected is correct at all
join() is an alias for implode().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.