SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

Dynamically changing of options of a list box based on period button

In the pervious tutorial we have seen how to populate a drop down list box by taking value from a MySQL table. Here we will manage or restrict the options of the same list box linking it to a period button. If period button is not selected then all the records of the table will populate the list box and once a selection is given to the period button then records satisfying to the value of the period button will be returned. We will be using OnClick event of the period button to reload or refresh the page with value of the period button in the query sting. Then we will read the value from the query string and then populate the list box accordingly. If the global variable is kept off in your PHP.ini settings then you have to use $_POST[variable] to get the variable for further processing.

This tutorial is similar to double drop down list box tutorial where second list box value is dynamically populated based on the selection value of the first list box.

To know how the OnClick event of the period buttons works then visit the JavaScript tutorial section.

Here the only change than the main drop down tutorial is using of checked value of period button. This part is handled by use of JavaScript function reload(). Here is the code that handles the function.

<script type="text/javascript">
function reload()
{
for(var i=0; i < document.form1.type.length; i++){
if(document.form1.type[i].checked)
var val=document.form1.type[i].value
}
self.location='test.php?type=' + val ;
}
</script>


We will receive the value and use them in our SQL query by adding one WHERE clause to restrict the records returned from the table. Here is how the value is received from query string and used for SQL query.

$tp=$_GET['type']; // getting the value from query string
if(strlen($tp) > 1){$query="SELECT name,id FROM student where sex='$tp'";}
else{$query="SELECT name,id FROM student";}
echo $query;
$result = mysql_query ($query);
echo "<form name=form1 method=post action=test.php>
<select name=student value=''>Student Name</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[id]>$nt[name]</option>";
}
echo "</select>";


// radio buttoms frm here ///
echo "
<b>Type</b>
<input type=radio name=type value='male' onclick=\"reload()\";>Male
<input type=radio name=type value='female' onclick=\"reload()\";>Female
<input type=submit value=Submit> </form>";


Demo of list box population with period button is here


The SQL file ( dump ) to create student table with records is here


The PHP Source file of this script




Further readings
Double drop down list on managing second list based on selection of first list
Enable second drop down list after selection of first list box
Double drop down list Frequently Asked Question
Manageing three drop down list
Double drop down list demo
Three drop down list demo
Adding options to a list box from database
Managing list box options from a period button
Retaining the form field values once the page reloads
Controlling second list box by using Ajax & PHP



Join Our Email List
Email:  
For Email Newsletters you can trust
PHP Sections