Page 1 of 1

Forms in PHP

Posted: Tue Sep 22, 2009 11:09 am
by devesa
Dear colleagues, I have a problem working with forms in PHP.
I have a SELECT tag to choose one item, and in the same form I have another SELECT tag whose options depend on which item was selected in the first one.

How can I do that? Thanks!

Re: Forms in PHP

Posted: Tue Sep 22, 2009 11:24 am
by jackpf
Define "depend on".

Also, what's your code?

Re: Forms in PHP

Posted: Tue Sep 22, 2009 11:33 am
by devesa
Thanks for your fast reply,

I am still thinking about my code but I'll try to explain:

Code: Select all

<select name="temp">
<?php $res2=mysql_query("SELECT * FROM Temporadas");
for($i=0;$i<mysql_num_rows($res2);$i++)
{ ?> <option value="<?php echo mysql_result($res2,$i,"Temporada");?>"><?php echo $i;?></option> <?php } ?>
</select>
    
<select name="comp">
<?php $res3=mysql_query("SELECT * FROM Competiciones WHERE Id_temp=[color=#FF0000]HERE IS WHERE I NEED THE PREVIOUS ITEM[/color]");
for($i=0;$i<mysql_num_rows($res3);$i++)
{ ?> <option value="<?php echo mysql_result($res3,$i,"Id_competicion");?>"><?php echo $i;?></option> <?php } ?>
</select>
So on the second select I need to know which item of the first one was selected.

Thanks

Re: Forms in PHP

Posted: Tue Sep 22, 2009 11:42 am
by jackpf
You'd have to submit the form once they select the first option. You can put this on the select element:

Code: Select all

onchange="document.forms['formid'].submit();"
Then you can check if they have chosen something like this:

Code: Select all

if(isset($_POST['the_select_element']))
{
    //display the relative select options here
}
I'd personally use javascript, but if you're not familiar with it (or you prefer PHP), then you should probably just do this.

You might also want to echo back the post data into the inputs so that the user's data is preserved :)

Re: Forms in PHP

Posted: Tue Sep 22, 2009 11:46 am
by devesa
Thank you, it was very helpful! :D

Re: Forms in PHP

Posted: Tue Sep 22, 2009 11:58 am
by jackpf
Cool, no problem :)