简体   繁体   English

将单选按钮添加到邮件表单

[英]adding radio button to mail form

I have a HTML form : 我有一个HTML表单:

 <form name="sentMessage" id="contactForm" novalidate> <div class="row control-group"> <div class="form-group col-xs-12 floating-label-form-group controls"> <label>Name</label> <input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name."> <p class="help-block text-danger"></p> </div> </div> <div class="row control-group"> <div class="form-group col-xs-12 floating-label-form-group controls"> <label>Email Address</label> <input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address."> <p class="help-block text-danger"></p> </div> </div> <div class="row control-group"> <div class="form-group col-xs-12 floating-label-form-group controls"> <label>Address</label> <input type="text" class="form-control" placeholder="Address" id="address" required data-validation-required-message="Please enter your street address."> <p class="help-block text-danger"></p> </div> </div> <div class="row control-group"> <div class="form-group col-xs-12 floating-label-form-group controls"> <label>Postcode</label> <input type="text" class="form-control" placeholder="Postcode" id="postcode" required data-validation-required-message="Please enter your postcode."> <p class="help-block text-danger"></p> </div> </div> <div class="row control-group"> <div class="form-group col-xs-12 floating-label-form-group controls"> <label>Cocktails</label> <textarea rows="5" class="form-control" placeholder="Please enter which cocktails you would like to order" id="message" required data-validation-required-message="Please enter which cocktails you would like to order."></textarea> <p class="help-block text-danger"></p> </div> </div> <br> <div id="success"></div> <div class="row"> <div class="form-group col-xs-12"> <button type="submit" class="btn btn-success btn-lg">Send</button> </div> </div> </form> 

It is linked to a PHP file that will send the details to my email address 它链接到一个PHP文件,该文件会将详细信息发送到我的电子邮件地址

<?php

if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['address'])         ||
    empty($_POST['postcode'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    die( "No arguments Provided!" ); //any text other than 'success' will be considered fail.
   }

$name = $_POST['name'];
$email_address = $_POST['email'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$message = $_POST['message'];

// Create the email and send the message
$to = ''; 
$email_subject = "subject:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nAddress: $address\n\nPostcode: $postcode\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; 
$headers .= "Reply-To: $email_address"; 

/*** check if the mail() function was successful ***/
if( mail($to,$email_subject,$email_body,$headers) ){
    die( "success" ); //the ajax code will use 'success' as the trigger to confirm email was sent
}else{
    die( "email failed" );
}

Is there a way that a radio button option can be added to the form and if it has been clicked be sent to my email as well? 有没有一种方法可以将单选按钮选项添加到表单中,并且如果单击它也可以发送到我的电子邮件中?

Yes, you can easily add radio buttons to the form. 是的,您可以轻松地将单选按钮添加到表单。 I will demonstrate a quick example of how to do so and then you can use it to update yours. 我将演示一个有关如何执行此操作的快速示例,然后您可以使用它来更新您的示例。

<form>
  <input type="radio" name="gender" value="male" checked> Male<br> <!-- make sure atleast one is checked to ensure that atleast some data will be sent to your email -->
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="radio" name="gender" value="other"> Other  
</form> 

HTML HTML

<input type="radio" name="radio" value="option1" checked="checked" /> Option1<br/>
<input type="radio" name="radio" value="option2" /> Option2

name should be same for all radio inputs as only one value will be sent. 所有无线电输入的名称应相同,因为将仅发送一个值。

to get the value in php 获得价值的PHP

PHP PHP

$radiovalue = $_POST['radio']; //be careful with name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM