0

I Have posted this question before but with no success, Please can someone help with this?

I have a working captcha form, but it cant seem to send the list/menu that set to allow multiple select it still sends one at a time, I am not sure which code to add to the php.

I did get some help from (Benjamin Oman) but the form sends only the word Array not the actual main areas.

This is what he suggested:

form:

<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>";

This is my 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";

}
?>

And this is the form:

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

Can someone help me out with this, Thanks

Drakinfly
  • 17
  • 3

1 Answers1

0

Benjamin's solution for the form is correct - that is for sure.

When you pass the values of this <select>-Element in the form of

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

the actual value of the $_POST array will be something like this:

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)

The Values in the <select>-element are actually processed as an array! So if you do

echo $_POST['main_area'];

the output will be:

Array

If you want to get the values as a string, you can do an implode:

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

The output will be:

 Durban, South Coast 

Tip: You should sanitize your input via $_POST - for a starting point, see PHP -Sanitize values of a array

Community
  • 1
  • 1
Florian
  • 3,366
  • 1
  • 29
  • 35
  • Tanks for the feed back your method works great, but I put the main area because it was the shortest one in the form I have one that has a commodities that has 389 option to select from so the array will have 389 strings that is way too many, is there a faster way to do this? thanks for the help. – Drakinfly May 19 '12 at 17:36
  • You could use concatenation instead of one implode, but with an array of 389 items this will probably not be that big of a ressource saver; a quick search brought up this: http://stackoverflow.com/questions/4502654/php-many-concats-or-one-implode – Florian May 22 '12 at 14:10