使用 PHP 表单处理的单选按钮

发布于 2024-10-18 01:43:18 字数 2501 浏览 2 评论 0原文

我有一个使用一些基本 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("[email protected]", "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>

非常感谢您的帮助!

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("[email protected]", "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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萧瑟寒风 2024-10-25 01:43: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>

请注意添加的 value 属性。

还有 PHP:

$contact = $_POST['contact']
//Will return either "email" or "phone".
<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".
内心激荡 2024-10-25 01:43:18

您的收音机需要值:

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

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>
煮酒 2024-10-25 01:43:18

只需给您的无线电输入一个值属性即可。这是将通过 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>

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>
塔塔猫 2024-10-25 01:43:18

简单的!只需向单选按钮添加一个值即可。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文