PHP表单处理的单选按钮

时间:2011-02-17 16:37:02

标签: php html forms submit form-submit

我有一个基本的表单,我使用一些基本的PHP提交。我的表单提交工作很好,除了我有一个单选按钮(首选的联系方式),我不知道如何在PHP中添加,以便发送电子邮件。两个单选按钮选项具有相同的名称,因此不能用作值。我的代码如下。

PHP如下:

<?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如下:

  <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>

非常感谢你的帮助!

4 个答案:

答案 0 :(得分:17)

<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>

请注意添加的value属性。

PHP:

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

答案 1 :(得分:2)

您的无线电需要值:

  <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 :(得分:2)

只需为您的无线电输入提供值属性即可。这是通过POST提交的内容。然后,您可以通过$ _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>

答案 3 :(得分:1)

轻松!只需在单选按钮上添加一个值即可。

<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>