用PHP表单处理的单选按钮 [英] Radio Buttons with PHP Form Handling

查看:145
本文介绍了用PHP表单处理的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

PHP如下:

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

表单的HTML如下:

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!

推荐答案

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

请注意添加的属性。

和PHP:

And the PHP:

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

这篇关于用PHP表单处理的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆