简体   繁体   中英

select option, email form

I'm trying to get the 'select option' to post to my email, the other input fields do post but not the select it's driving me nuts. Please advice. Also any additional advice on sql injection prevention methods would be much appreciated.

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
<select name="selectoption">
    <option value="first">First</option>
    <option value="second">Second</option>
    <option value="third">Third</option>
 </select>

You can get selected valueby below code:-

$selectOption = $_POST['selectoption'];

Use isset to check value is exist or not.

$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'] : ''; 

For SQL Injection Prevention Techniques,

Refer this link .

Hope it will help you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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