Solomon John Roy
Solomon John Roy

Reputation: 11

PHP and Javascript

What I have is a drop down of the available rooms from a database. Like Classroom 1, followed by the maximum capacity in it so it looks like this, Classroom 1(28), Classroom 2(30) so on and so forth. Above this drop down list is a text box to enter the number of students enrolling so you can key in any number.

My question is, how do I eliminate the classrooms that do not fit the number keyed in the text box. So example I key in 30 and when i click the drop down list Classroom 1(28) isn't there anymore, instead Classroom 2(30) is listed.

Upvotes: 1

Views: 118

Answers (1)

Matt
Matt

Reputation: 3848

First you need an event on the textbox that is triggered when its text changes.

Then you need to loop through each option. If the textbox value is greater than the parsed out classrom size value, you hide the option.

Jquery will be useful here.

In my opinion this is not an SQL or ajax problem. You want to fetch all the existing classrooms and have them available as selections, and then filter the selections based on what the user types in a textbox.

jquery would look something like this

$('#Count_textbox').change(function(event)
{
  var countValue=$(this).val();

  //loops through each option
  $('#classroom_select').find('option').each(function(index)
  {
     //write parseOutValue to get the value out of the string of text
     var classroomValue=parseOutValue($(this).text());

     if( countValue > classroomValue )
     {
        $(this).attr("disabled","disabled");

     }
     else
     {
        $(this).attr("disabled","");
     }
  }
});

Upvotes: 3

Related Questions