Forms in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devesa
Forum Newbie
Posts: 10
Joined: Fri Sep 11, 2009 2:49 am

Forms in PHP

Post 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!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Forms in PHP

Post by jackpf »

Define "depend on".

Also, what's your code?
devesa
Forum Newbie
Posts: 10
Joined: Fri Sep 11, 2009 2:49 am

Re: Forms in PHP

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Forms in PHP

Post 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 :)
devesa
Forum Newbie
Posts: 10
Joined: Fri Sep 11, 2009 2:49 am

Re: Forms in PHP

Post by devesa »

Thank you, it was very helpful! :D
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Forms in PHP

Post by jackpf »

Cool, no problem :)
Post Reply