添加单选按钮到邮件表单

时间:2017-02-22 20:51:39

标签: php html email

我有一个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>
&#13;
&#13;
&#13;

它链接到一个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" );
}

是否可以将单选按钮选项添加到表单中,如果单击该选项也可以发送到我的电子邮件中?

2 个答案:

答案 0 :(得分:0)

是的,您可以轻松地向表单添加单选按钮。我将演示一个如何操作的快速示例,然后您可以使用它来更新您的。

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

答案 1 :(得分:0)

<强> HTML

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

所有无线电输入的名称应相同,因为只会发送一个值。

获取php中的值

<强> PHP

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