选择选项,电子邮件表单

时间:2016-02-14 05:30:57

标签: php forms

我试图选择'选择选项'发布到我的电子邮件,其他输入字段 发帖但不是选择它让我疯了。请指教。还有 关于sql注入预防方法的其他建议将不胜感激。

HTML

 <form action="contactform.php" method="post" >
<input type="text" name="name" placeholder="*Full Name">
<input type="text" name="email" placeholder="*Email">
<input type="tel" name="telephone"placeholder="*Telephone">
<input type="text" name="comments"class="feedback-input"id="comments"placeholder="*How can I help?">
 <select name="selectoption">
    <option value="first">First</option>
    <option value="second">Second</option>
    <option value="third">Third</option>
  </select>
<input type="text" name="code" placeholder="1+2 =" />
<input type="submit"value="Send"class="button">
</form>



PHP
<?php

if(isset($_POST['email'])) {


if (strtolower($_POST['code']) != '3') {die('Wrong access code');}


    $email_to = "";

    $email_subject = "contact form submission";

    $name = $_POST['name']; // required

    $email_from = $_POST['email']; // required

    $telephone = $_POST['telephone']; // not required


    $comments = $_POST['comments']; // required

    $selectoption = $_POST['selectoption']; // required



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }

    $email_message .= "Name: ".clean_string($name)."\n";

    $email_message .= "Email: ".clean_string($email_from)."\n";

    $email_message .= "Telephone: ".clean_string($telephone)."\n";


    $email_message .= "Comments: ".clean_string($comments)."\n";

    $email_message .= "Selectoption: ".clean_string($selectoption)."\n";




// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>






Thank you for contacting us. We will be in touch with you very soon.
<a href="#">return to website</a>


<?php

}

?>



thanks very much
Dan

1 个答案:

答案 0 :(得分:0)

<select name="selectoption">
    <option value="first">First</option>
    <option value="second">Second</option>
    <option value="third">Third</option>
 </select>

您可以通过以下代码获得选定的值: -

$selectOption = $_POST['selectoption'];

使用isset检查值是否存在。

$name = isset($_POST['name']) ? $_POST['name'] : '';  

$email_from = isset($_POST['email']) ? $_POST['email'] : ''; 

$telephone = isset($_POST['telephone']) ? $_POST['telephone'] : '';    

$comments = isset($_POST['comments']) ? $_POST['comments'] : ''; 

$selectoption = isset($_POST['selectoption']) ? $_POST['selectoption'] : ''; 

对于SQL注入预防技术,

请参阅此link

希望它会对你有所帮助:)。