PHP form control radio controls

I have a basic form that I submit using some basic PHP. I have a form view that works fine, except that I have a switch (for the preferred contact method), and I'm not sure how to add this to PHP so that it sends via email. Both switch parameters have the same name, so it does not work as a value. My code is below.

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" ); } ?> 

HTML for the form 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> 

Thank you for help!

+8
html php submit forms form-submit
source share
4 answers
 <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 attribute value .

And PHP:

 $contact = $_POST['contact'] //Will return either "email" or "phone". 
+15
source share

You need radio stations:

  <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> 
+2
source share

Just give your radio inputs a value attribute. This is what will be submitted via POST. Then you can access it through $ _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> 
+2
source share

Easy! Just add value to your switches.

 <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> 
+1
source share

All Articles