Forum Moderators: coopster

Message Too Old, No Replies

Problem with a form

Used to work in my html form...

         

johnnybt

8:26 am on Jan 22, 2004 (gmt 0)

10+ Year Member



Ok in my old html form I had two selection boxes. One box had all the selections, and you could move the ones you wanted over to the right box. On submit, the form executed this command:

<INPUT onclick=selectAll(document.combo_box.right box) name=rbox; type=submit value=Submit name=submit_button>

It had to basically select all the values in the right box in order for the formmail script to mail me the values.

I want to use the same selection process in my PHP form but can't for the life of me figure out how to do the same thing.

coopster

9:54 am on Jan 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I have scripts that do this as well. The solution is to use PHP's "array from form element" feature, which is simply appending brackets to the
name
attribute of the
<select>
tag:

<select name="my_list[]" multiple="multiple" size="10">

The PHP and HTML [php.net] FAQ should help quite a bit.

johnnybt

4:51 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



thanks for the help...i did that but when i hit submit i just get "array" instead of any of the information in the select box

BitBanger

4:59 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



thanks for the help...i did that but when i hit submit i just get "array" instead of any of the information in the select box

You must loop through the array for all the selected items.


foreach ($my_list as $selection) {
echo $selection . '<br />';
}

johnnybt

5:03 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



Ok I know I'm stupid, but I'm not sure where to put that code. Does it go in my form, or in the php file that posts it? and what do i put in to replace the my_list and selection?

johnnybt

5:46 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



Here is my coding, if it'll help:

Submitform.php

<form action="confirmform.php" method="POST">

<select style="WIDTH: 250px" multiple size=20 name=list1>
<option value="Player 1">1</option>
<option value="Player 2">2</option>
</select>

<INPUT id=button2 onclick=move(this.form.list1,this.form.team) type=button value=">>" name=button2>
<INPUT id=button1 onclick=move(this.form.team,this.form.list1) type=button value="<<" name=button1>

<select name="team[]" size=10 multiple="multiple" id=team style="WIDTH: 250px">
</SELECT>

<input type=submit value=Submit name=submit_button>
<input name="Go" type="submit" id="Submit" value="Go" />
</form>

And then the confirmform.php

<form action=submitted.php method=post>

Info - <?php echo $_POST["team[]"];?>

<?php foreach ("team[]" as $selection) { echo $selection . '<br />'; }?>

<input type=button value="Go Back" onClick="history.go(-1)">

<input name="That's Right" type="submit" id="Submit" value="That's Right" />

[edited by: jatar_k at 5:59 pm (utc) on Jan. 22, 2004]
[edit reason] trimmed down to relevant code [/edit]

BitBanger

6:01 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



In the confirmform.php file right where you have it. Only change it from:

Info - <?php echo $_POST["team[]"];?></strong></span><br />
</p>

<?php foreach ("team[]" as $selection) { echo $selection . '<br />'; }?>

</p>


To:

Info - <?php echo $_POST['team'];?></strong></span><br />
</p>

<?php foreach ($_POST['team'] as $selection) { echo $selection . '<br />'; }?>

</p>

Note the highlight where the changes occur.

My sample code did not refer to the variable through the $_POST super global. Also, you don't need the empty [] on the team variable when extracting values from the array. You may want to study arrays in the php [php.net] documentation.

johnnybt

6:13 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



Thanks for your help! honestly i'd be lost without forums!

I changed it but am getting an "invalid argument" error

i dont know if i have it named properly in the submitform.php file, but i think i do.

i'm new to it all, so it's incredibly frustrating.

BitBanger

7:20 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



What file and line does the error occur in? I don't see a problem at the moment.

johnnybt

2:05 am on Jan 23, 2004 (gmt 0)

10+ Year Member



I'm getting the error in the php line in my confirmform.php

did you try the code on your own? i really have no idea what's wrong. i've stripped off just about everything and here is what i have left:

submitform.php =

<form action="confirmform.php" method="POST">
<select style="WIDTH: 250px" multiple size=20 name=list1>
<option value="1">1</option>
<option value="2">2</option>
</select>
<INPUT id=button2 onclick=move(this.form.list1,this.form.team) type=button value=">>" name=button2>
<INPUT id=button1 onclick=move(this.form.team,this.form.list1) type=button value="<<" name=button1>

<select name="team[]" size=10 multiple="yes" id=team style="WIDTH: 250px">
</SELECT>

<INPUT type=submit value=Submit name=submit_button>
</form>

and confirmform.php =

<?php foreach ($_POST['team'] as $selection) { echo $selection . '<br />'; }?> </p>

It's got to have something to do with the 'team' box not having any values in it to begin with, and then getting some from the 'list1' box?

[edited by: jatar_k at 3:54 am (utc) on Jan. 23, 2004]
[edit reason] way too much code [/edit]

johnnybt

3:08 am on Jan 23, 2004 (gmt 0)

10+ Year Member



I figured it out!

the code was fine, the problem was that the onclick=selectall command wasn't coded properly.

thanks!

jatar_k

3:56 am on Jan 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would suggest running your html pages through

[validator.w3.org...]

there are more than a few problems in there that won't make your life any easier. The triple body tag being one. These may change behaviours or cause other errors that may only serve to confuse you.

and congrats on figuring it out :)