gpt4 book ai didi

php - 带有 PHP 表单处理的单选按钮

转载 作者:太空狗 更新时间:2023-10-29 13:06:32 31 4
gpt4 key购买 nike

我有一个要使用一些基本 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>

非常感谢您的帮助!

最佳答案

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

关于php - 带有 PHP 表单处理的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5031768/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com