10

I have a basic form that I am submitting using some basic PHP. I have the form submission working great, except that I have a radio button (for preferred method of contact) and I am not sure how to add that in the PHP so that sends in the email. Both radio button options have the same name, so that isn't working as the value. My code is below.

The PHP is as follows:

<?php
  $name    = stripslashes($_POST['name']);
  $email   = stripslashes($_POST['email']);
  $phone   = stripslashes($_POST['phone']);
  $contact = stripslashes($_POST['contact']);
  $message = stripslashes($_POST['message']);
  $form_message = "Name: $name \nEmail: $email \nPhone: $phone \nPreferred Method of Contact: $contact \nMessage: $message";

// Exit process if field "human" is filled (because this means it is spam)
if ( $_POST['human'] ) {
  echo 'Tastes Like Spam!'; exit; }
// if it is not filled, submit form
else {
  header( "Location: http://www.newurl.com");

  mail("myemail@gmail.com", "Email Subject", $form_message, "From: $email" );
}
?>

The HTML for the form is below:

  <form method="post" id="form" action="handle_form.php">
    <div class="field">
      <input type="text" name="human" id="human" class="txt" />
    </div>
    <div class="field form-inline">
      <label class="contact-info" for="txtName">Name*</label>
      <input type="text" name="name" id="name" class="txt" value=""/>
    </div>
    <div class="field form-inline">
      <label class="contact-info" for="txtEmail">Email*</label>
      <input type="text" name="email" id="email" class="txt" value=""/>
    </div>
    <div class="field form-inline">
      <label class="contact-info" for="txtPhone">Phone</label>
      <input type="text" name="phone" id="phone" class="txt" value=""/>
    </div>
    <div class="field form-inline radio">
      <label class="radio" for="txtContact">Preferred Method of Contact</label>
      <input class="radio" type="radio" name="contact" checked /> <span>Email</span>
      <input class="radio" type="radio" name="contact" /> <span>Phone</span>
    </div>
    <div class="field form-inline">
      <textarea rows="10" cols="20" name="message" id="message" class="txt" value=""></textarea>
    </div>
    <div class="submit">
      <input class="submit" type="submit" name="submit" value="Submit Form">
    </div>
  </form>

Thanks so much for the help!

Andrew
  • 1,247
  • 6
  • 15
  • 22
  • you should also link your labels and inputfields by providing the input id in the for attribute of the label – Sam Vloeberghs Jul 17 '13 at 10:29
  • A simpler way to link labels with radio and checkboxes is to wrap both the text and the input in a ` – Mar Feb 15 '14 at 00:06

4 Answers4

18
<div class="field form-inline radio">
  <label class="radio" for="txtContact">Preferred Method of Contact</label>
  <input class="radio" type="radio" name="contact" value="email" checked /> <span>Email</span>
  <input class="radio" type="radio" name="contact" value="phone" /> <span>Phone</span>
</div>

Note the added value attribute.

And the PHP:

$contact = $_POST['contact']
//Will return either "email" or "phone".
Chris Sobolewski
  • 12,819
  • 12
  • 63
  • 96
  • Wow, thanks everyone for the quick response. I thought I had tried that and it didn't work, but I just tried it again and it works perfectly! Thanks! – Andrew Feb 17 '11 at 17:01
2

You radios need values:

  <input class="radio" type="radio" value="email" name="contact" checked /> <span>Email</span>
  <input class="radio" type="radio" value="phone" name="contact" /> <span>Phone</span>
Capsule
  • 6,118
  • 1
  • 20
  • 27
2

Just give your radio inputs a value-attribute. This is what will get submitted via POST. You can then access it via $_POST['nameofradio']

  <input class="radio" type="radio" name="contact" value="Email" checked /> <span>Email</span>
  <input class="radio" type="radio" name="contact" value="Phone" /> <span>Phone</span>
Tim
  • 5,893
  • 3
  • 35
  • 64
1

Easy! Just add a value to your radio buttons.

<input class="radio" type="radio" name="contact" value="Email" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" value="Phone" /> <span>Phone</span>
Stephen S.
  • 837
  • 7
  • 16
  • 29