Jump to content

Combo box problems


reckdan

Recommended Posts

am designing a web based school registry system and i have a form with 2 combo boxs on containing classes and the other containing names of students but i the student name combo box information to be dependent on the item selected in the class combo box. That is when someone chooses a class from the class combo box the student combox box will be automatically populated with students in the class selected in the first combo box.

Note: Selecting a class from the class combo box should automatically populate the student combo box without clicking on any submit button

Link to comment
https://forums.phpfreaks.com/topic/53848-combo-box-problems/
Share on other sites

Hi

 

Two ways of doing it:

1 using javascript to populate the next box (messy),

2 using a simply js instruction to submit when you change the value of the first box... much better.

 

Heres a very brief summary of what i mean in method 2:

<?php

echo "
<html>
<form method='post'>
<select name='class' onchange='JavaScript: submit();'>
<option value='class1'>Class 1</option>
<option value='class2'>Class 2</option>
</select>
<br>
<select name='students'>";

$class = $_POST["class"];
if ($class = "class1")
{
$options = "<option value='john'>John</option>"; // ... etc;
}
else if if ($class = "class2")
{
$options = "<option value='sarah'>sarah</option>"; // ... etc;
}

echo "
$options
</select>
</form>
</html>";

 

Feel free to ask for more detail...

 

cheers,

tdw

Link to comment
https://forums.phpfreaks.com/topic/53848-combo-box-problems/#findComment-266188
Share on other sites

Thank you very much for your help because i didn't have any idea of how to go about it. but my problem is that when i just tried the code you gave me, i couldn't select the other class (class 2) since it always goes back to class 1, there for there seem to be some problem with it.

Secondly both class and students are in a database so how do i pass the selected class to the database to retrieve the corresponding students in the class to populate the combo box..

I'm a novice and really want to learn at a fast pace

Link to comment
https://forums.phpfreaks.com/topic/53848-combo-box-problems/#findComment-266212
Share on other sites

Hi okay, add the line commented below to select its posted value

 

* note, i rearranged the code here... i tend to always to all the php first, then echo out the html at the end.

 

does this help you?

 

<?php

$class = $_POST["class"];

$classoptions = "
<option value='class1'>Class 1</option>
<option value='class2'>Class 2</option>";
$classoptions = str_replace("value='$class'>","value='$class' selected>", $classoptions); // this will selected the posted value

if ($class = "class1")
{
$studoptions = "<option value='john'>John</option>"; // ... etc;
}
else if if ($class = "class2")
{
$studoptions = "<option value='sarah'>sarah</option>"; // ... etc;
}

echo "
<html>
<form method='post'>
<select name='class' onchange='JavaScript: submit();'>
$classoptions
</select>
<br>
<select name='students'>
$studoptions
</select>
</form>
</html>";

Link to comment
https://forums.phpfreaks.com/topic/53848-combo-box-problems/#findComment-266218
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.