2012-05-18 43 views
0

我以前發佈過這個問題,但沒有成功,請有人可以幫忙嗎?php的captcha表單多選擇列表/菜單不工作

我有一個工作驗證碼錶單,但它似乎無法發送列表/菜單設置爲允許多次選擇它仍然一次發送一個,我不知道哪些代碼添加到php。

我確實從(本傑明阿曼)得到了一些幫助,但表格只發送數組而不是實際的主要區域。

這是他建議:

形式:

<select name="main_area[]" size="1" multiple="multiple"> 
<option value="Durban">Durban</option> 
<option value="South Coast">South Coast</option> 
</select> 

PHP:

echo "Main Area(s): \"" . print_r($_POST["main_area"], true) . "\" <br>"; 

這是我的PHP:

<? 

session_start(); 

// *** We need to make sure theyre coming from a posted form - 
// If not, quit now *** 
if ($_SERVER["REQUEST_METHOD"] <> "POST") 
die("You can only reach this page by posting from the enquiry form"); 

// *** The text input will come in through the variable $_POST["captcha_input"], 
// while the correct answer is stored in the cookie $_COOKIE["pass"] *** 
if ($_POST["captcha_input"] == $_SESSION["pass"]) 
{ 
// *** They passed the test! *** 
// *** This is where you would post a comment to your database, etc *** 
echo "<h2><span>Thank</span> You</h2>"; 
echo "Correct! You have entered the correct code. <br><br>"; 
echo "Your enquiry has been sent <br><br>"; 

echo "Overview of the information you have sent <br><br>"; 

echo "Onditions of contract: \"" . $_POST["onditions_of_contract"] . "\" <br>"; 

echo "Tender results: \"" . $_POST["tender_results"] . "\" <br>"; 

echo "Resume: \"" . $_POST["resume"] . "\" <br>"; 

echo "Commodities: \"" . $_POST["commodities"] . "\" <br>"; // *** multiple select *** 

echo "Main Area: \"" . $_POST["main_area"] . "\" <br>"; // *** multiple select *** 

echo "Sub Area: \"" . $_POST["sub_area"] . "\" <br>"; // *** multiple select *** 

echo "Company: \"" . $_POST["company"] . "\" <br>"; 

echo "Company Vat Number: \"" . $_POST["company_vat_number"] . "\" <br>"; 

echo "Reg. Number: \"" . $_POST["reg_number"] . "\" <br>"; 

echo "E-mail Address: \"" . $_POST["email_address"] . "\" <br>"; 

echo "Correspondence Address: \"" . $_POST["correspondence_address"] . "\" <br>"; 

echo "Street Address: \"" . $_POST["street_address"] . "\" <br>"; 

echo "For Attention Of: \"" . $_POST["for_attention_of"] . "\" <br>"; 

echo "Telephone Number: \"" . $_POST["telephone_number"] . "\" <br>"; 

echo "Fax Number: \"" . $_POST["fax_number"] . "\" <br>"; 


//sends email via php to the following address 
$mailuser = "[email protected]"; 

//echo 'default chosen address: '.$mailuser; 

$header = "Return-Path: ".$mailuser."\r\n"; 
$header .= "From: Security Form <".$mailuser.">\r\n"; 
$header .= "Content-Type: text/html;"; 

$mail_body = ' 
Onditions of contract: '. $_POST[onditions_of_contract] . '<br> 
Tender results: '. $_POST[tender_results] . '<br> 
Resume: '. $_POST[resume] . '<br> 
Commodities: '. $_POST[commodities] . '<br> 
Main Area: '. $_POST[main_area] . '<br> 
Sub Area: '. $_POST[sub_area] . '<br> 
Company: '. $_POST[company] . '<br> 
Company Vat Number: '. $_POST[company_vat_number] . '<br> 
Reg. Number: '. $_POST[reg_number] . '<br> 
E-mail Address: '. $_POST[email_address] . '<br> 
Correspondence Address: '. $_POST[correspondence_address] . '<br> 
Street Address: '. $_POST[street_address] . '<br> 
For Attention Of: '. $_POST[for_attention_of] . '<br> 
Telephone Number: '. $_POST[telephone_number] . '<br> 
Fax Number: '. $_POST[fax_number] . '<br><br>' 
;  
mail ($mailuser, 'Multi-Quant Trial', $mail_body, $header); 
echo '<br>Thank You '; 


} else { 
// *** The input text did not match the stored text, so they failed *** 
// *** You would probably not want to include the correct answer, but 
// unless someone is trying on purpose to circumvent you specifically, 
// its not a big deal. If someone is trying to circumvent you, then 
// you should probably use a more secure way besides storing the 
// info in a cookie that always has the same name! :) *** 
echo "<h2><span>Oops!</span></h2>"; 
echo "Sorry, you did not enter the correct code.<br><br>"; 
echo "You said " . $_POST["captcha_input"]; 
echo "<br>while the correct answer was " . $_SESSION["pass"]; 
echo "<br><br>Please click back and try again"; 

} 
?> 

這是形式:

<form action="mail/captcha.php" method="post" id="sendemail"> 
<ol> 
<li> 
<label for="conditions_of_contract">Conditions of contract</label> 
<div class="box"> 
<input name="conditions_of_contract" type="radio" value="yes" /> Yes 
</div> 
<div class="box"> 
<input name="conditions_of_contract" type="radio" value="no" /> No 
</div> 
</li> 
<li> 
<label for="main_area">Main area <small>*</small><br /> 
<span>(* To select multiple main area keep CTRL button down and click on selecttion)</span> 
</label> 
<select name="main_area" size="1" multiple="multiple"> 
<option value="Durban">Durban</option> 
<option value="South Coast">South Coast</option> 
<option value="North Coast">North Coast</option> 
<option value="Pietrmaritzburg">Pietrmaritzburg</option> 
<option value="Northern Natal">Northern Natal</option> 
<option value="Eastern Cape">Eastern Cape</option> 
<option value="Free State">Free State</option> 
<option value="Mpumalanga">Mpumalanga</option> 
<option value="Swaziland">Swaziland</option> 
<option value="Cape">Cape</option> 
</select> 
</li> 
<li> 
<label>Please enter the code</label> 
<img src="mail/captcha_image.php" class="img" /> 
<input name="captcha_input" type="text" class="num" size="15"> 
</li> 
<li> 
<input name="Send" type="submit" class="button" id="Send" value="Submit" /> 
<input name="reset" type="reset" class="button" id="reset" value="Reset" /> 
</li> 
</ol> 
</form> 

有人可以幫我這個,感謝

回答

0

本傑明對形式的解決方案是正確的 - 這是肯定的。

當你傳遞的

<select name="main_area[]" size="1" multiple="multiple"> 
    <option value="Durban">Durban</option> 
    <option value="South Coast">South Coast</option> 
</select> 

,形式是<select> - 元素的值$ _POST數組的實際值將是這樣的:

array 
    'conditions_of_contract' => string 'yes' (length=3) 
    'main' => 
     array 
      0 => string 'Durban' (length=11) 
      1 => string 'South Coast' (length=15) 
    'captcha_input' => string '<Your Captcha Value>' (length=3) 
    'Send' => string 'Submit' (length=6) 

的價值觀<select> -element實際上是作爲一個數組處理的!所以,如果你

echo $_POST['main_area']; 

輸出將是:

Array 

如果你想要得到的值作爲一個字符串,你可以做一個破滅:

$mainAreaContents = implode(", ", $_POST['main_area']); 
echo $mainAreaContents; 

輸出將成爲:

Durban, South Coast 

提示:您應該saniti通過$ _POST輸入您的輸入 - 起始點,請參閱PHP -Sanitize values of a array

+0

坦克反饋您的方法很好,但我把主要區域,因爲它是最短的一個形式我有一個商品有389個選項可供選擇,所以數組將有389個字符串,方式太多了,有沒有更快的方法來做到這一點?謝謝您的幫助。 – Drakinfly

+0

你可以使用連接而不是一個implode,但是對於一個389個數組的數組,這可能不會是一個資源保護程序的大;快速搜索提出了這個:http://stackoverflow.com/questions/4502654/php-many-concats-or-one-implode – Florian