php验证码形式多选列表/菜单不起作用

时间:2012-05-18 11:20:34

标签: php mysql

我之前发过这个问题但没有成功,请有人帮忙解决这个问题吗?

我有一个有效的验证码表单,但它似乎无法发送设置为允许多个选择的列表/菜单,它仍然一次发送一个,我不确定将哪个代码添加到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 = "my@email.com";

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

有人可以帮我解决这个问题,谢谢

1 个答案:

答案 0 :(得分:0)

本杰明对表格的解决方案是正确的 - 这是肯定的。

的形式传递此<select> - 元素的值时
<select name="main_area[]" size="1" multiple="multiple">
    <option value="Durban">Durban</option>
    <option value="South Coast">South Coast</option>
</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> - 元素中的值实际上是作为数组处理的!所以,如果你这样做

echo $_POST['main_area'];

输出将是:

Array

如果要将值作为字符串获取,可以进行内爆:

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

输出将是:

 Durban, South Coast 

提示:您应该通过$ _POST清理输入 - 作为起点,请参阅PHP -Sanitize values of a array