Forum Moderators: coopster

Message Too Old, No Replies

Recalling arrays in the if statement

         

weemel

1:32 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



Hello

I want to create a form that will mail different people depending on the selection using the <option> values.

So far, i've put the options into arrays like so:

<select name=to[]>
<option></option>
<option></option>
</select>

Is this correct?

I think the problem i'm having is how to recall the specific option in the if statement:

using if($_POST['to[0]']) doesn't work.

Can somebody help please? Much appreciated =)

eaden

2:15 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



you could try..

$tomail = $_POST['to'];

if($tomail[0])

or...

foreach($_POST['to'] as $something) {
....
}

coopster

5:57 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The PHP manual pages regarding PHP and HTML [php.net] answer this and a few other frequently asked questions.

willis1480

6:05 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



creating this page would be open to serious hacker attacks. I recomend verifying that all $_POST to that page come from the previous page or cross check all email address.

weemel

10:03 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



thanks =) much appreciated.

weemel

10:18 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



Erm,

Eaden, it actually doesn't work - no matter what option is selected, the first option's ouptput is displayed yet the mail is not sent! What could be the problem?

jatar_k

11:05 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you don't need the [] in the select

<select name=to> will work just fine

the selected value will be present in the $_POST array as $_POST['to'] so just echo that and see if it behaves properly

weemel

11:27 pm on Nov 9, 2004 (gmt 0)

10+ Year Member



So i tried

<select name=to>
<option></option>
<option></option>
<option></option>
</select>

<?php

$tomail = $_POST['to'];

switch ($tomail) {

case 0:
mail(...);
echo ...;
break;

case 1:
mail(...);
echo ...;
break;
?>

It still doesn't work - does case 0 even though the second option is selected. =(

What do i do?

jatar_k

11:58 pm on Nov 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this works

testform.php

<?php 
if ($_POST['to'] == "none") echo "<p>please select someone to send to";
$tomail = $_POST['to'];
switch ($tomail) {
case 1:
echo "<p>bob";
break;
case 2:
echo "<p>doug";
break;
case 3:
echo "<p>take off eh";
break;
default:
break;
}
?>
<p><form method="post" action="testform.php">
<select name="to">
<option value="none">select please</option>
<option value="1">bob</option>
<option value="2">doug</option>
<option value="3">mckenzie</option>
</select>
<input type="submit" value="go for it">
</form>

I would assign the value in the switch and call the mail function at the end of the script but that is just a preference thing. It would mean you only have to manage a single function call instead of many calls to the same thing.

weemel

4:55 pm on Nov 10, 2004 (gmt 0)

10+ Year Member



I should've thought of that... thank you!