2016-09-29 35 views
-1

我正在尋找一種方法,可以設置我的下拉列表,以便在單擊名稱後向該特定人員發送電子郵件。基本上,php代碼emailto將更改爲在下拉列表中選擇的個人電子郵件地址。如何通過電子郵件聯繫表單?

我不知道該怎麼做,但這是一個項目,我認爲這將是很酷,如果我可以使用它,如果它甚至是可能的。

總而言之我問如果有人知道我可以用來完成這項任務的代碼。截至目前,下拉列表<li>設置爲<a href>。下面的代碼:

<form action="Test.php" method="post" class="popup-form"> 
       <input type="text" class="form-control form-white" maxlength="25" name="first_name" placeholder="Name"> 
       <input type="text" class="form-control form-white" maxlength="25" name="last_name" placeholder="Name"> 
       <input type="text" class="form-control form-white" name="email" placeholder="Email Address"> 
       <input type="text" class="form-control form-white" maxlength="11" name="telephone" placeholder="Name"> 
       <input type="text" class="form-control form-white" name="comments" placeholder="Message"> 

       <div class="dropdown"> 
        <button id="dLabel" class="form-control form-white dropdown" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 
         Contact 
        </button> 
        <ul class="dropdown-menu animated fadeIn" role="menu" aria-labelledby="dLabel"> 
         <li class="animated lightSpeedIn"><a href="#">Mr Ben Nguyen</a></li> 
         <li class="animated lightSpeedIn"><a href="#">Mr John Lee</a></li> 
         <li class="animated lightSpeedIn"><a href="#">Mr Robert Lamothe</a></li> 
         <li class="animated lightSpeedIn"><a href="#">Mr Bryan D.</a></li> 
         <li class="animated lightSpeedIn"><a href="#">Ms Shakira G.</a></li> 
        </ul> 
       </div> 
       <div class="checkbox-holder text-left"> 
        <div class="checkbox"> 
         <input type="checkbox" value="None" id="squaredOne" name="check" /> 
         <label for="squaredOne"><span>I Agree to the <strong>Terms &amp; Conditions</strong></span></label> 
        </div> 
       </div> 
       <button type="submit" name="submit" class="btn btn-submit">Submit</button> 
      </form> 

PHP的形式在這裏

<?php 
if(isset($_POST['email'])) { 

    // CHANGE THE TWO LINES BELOW 
    $email_to = "[email protected]"; 

    $email_subject = "Rhien Centennial Comment Form"; 


    function died($error) { 
     // your error code can go here 
     echo "We're sorry, but there's errors found with the form you submitted.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 
    if(!isset($_POST['first_name']) || 
     !isset($_POST['last_name']) || 
     !isset($_POST['email']) || 
     !isset($_POST['telephone']) || 
     !isset($_POST['comments'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.');   
    } 

    $first_name = $_POST['first_name']; // required 
    $last_name = $_POST['last_name']; // required 
    $email_from = $_POST['email']; // required 
    $telephone = $_POST['telephone']; // not required 
    $comments = $_POST['comments']; // required 

    $error_message = ""; 
    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
    if(!preg_match($email_exp,$email_from)) { 
     $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 
    $string_exp = "/^[A-Za-z .'-]+$/"; 
    if(!preg_match($string_exp,$first_name)) { 
     $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
    } 
    if(!preg_match($string_exp,$last_name)) { 
     $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 
    } 
    if(strlen($comments) < 2) { 
     $error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
    } 
    if(strlen($error_message) > 0) { 
     died($error_message); 
    } 
    $email_message = "Form details below.\n\n"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "First Name: ".clean_string($first_name)."\n"; 
    $email_message .= "Last Name: ".clean_string($last_name)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "Telephone: ".clean_string($telephone)."\n"; 
    $email_message .= "Comments: ".clean_string($comments)."\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); 
?> 

<!-- place your own success html below --> 

Thank you for contacting us. We will be in touch with you very soon. 

<?php 
} 
die(); 
?> 

的PHP是不是HTML表單內聯,但由於某些原因的PHP代碼不會沒有姓氏和電話的工作。所以我只是離開了它,因爲我不知道如何在沒有它的情況下運作。

+0

嗯,這是非常可能的。但是,你擁有的只是一個HTML表單...沒有任何PHP的地方 – Machavity

+0

我會立即更新它:) – Jayy

+0

已更新的php代碼 – Jayy

回答

0

您可以向表單添加隱藏的輸入字段,並向下拉列表中添加事件處理程序(即,Onclick/onchange)。例如:

<input type="hidden" id="email_to" name="email_to"> 

... 

<a href="javascript:void(0)" onclick="document.getElementById("email_to").value = "[email protected]">Someone</a> 
相關問題