Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.