PHP - Selection code - Asked By Bigboy on 29-Jun-12 05:20 AM

Hie, can I please have PHP code for selecting an item in a combo box and the next combobox would dispaly respective items. For example: in one combo box a user can select 'mining' and in the next combobox, only 'underground mining' and 'open cast mining' would be displayed.

thank you
Jitendra Faye replied to Bigboy on 29-Jun-12 08:28 AM
Reference from-

http://stackoverflow.com/questions/6857287/how-to-make-a-cascading-drop-down-list-in-php-using-jquery

solution-


Here is sample code to implement cascading dropDownList in PHP.

$("#country").change(function(){
    $
('#city').find('option').remove().end(); //clear the city ddl
   
var country = $(this).find("option:selected").text();
    alert
(country);
   
//do the ajax call
    $
.ajax({
        url
:'getCity.php'
        type
:'GET',
        data
:{city:country},
        dataType
:'json',
        cache
:false,
        success
:function(data){

        data
=JSON.parse(data); //no need if dataType is set to json
         
var ddl = document.getElementById('city');                      

         
for(var c=0;c<obj.length;c++)
             
{              
               
var option = document.createElement('option');
               option
.value = obj[c];
               option
.text  = obj[c];                          
               ddl
.appendChild(option);
             
}


   
},
        error
:function(jxhr){
        alert
(jxhr.responseText);

   
}
   
});

});

in your getCity.php

$country = $_GET['city'];

//do the db query here

$query  
= "your query";
$result
= mysql_query($query);
$temp
= array();
while ($row = mysql_fetch_assoc($result)) {

 
if(empty($temp))
 
{
   $temp
=array($row['city']);
 
}
 
else
 
{  
   array_push
($temp,$row['city']);
 
}

}
echo
(json_encode($temp));
Super Man replied to Bigboy on 29-Jun-12 02:33 PM

Following example is using two combobox. which show how to get data from table and echo (print) it. you can make use of it. and first clear the combo box and add new data.


<?php
//**************************************
// Page load dropdown results //
//**************************************
function getTierOne()
{
$result = mysql_query("SELECT DISTINCT country FROM $tablename")
or die(mysql_error());

while($tier = mysql_fetch_array( $result ))

{
echo ''.$tier['country'].'';
}

}

//**************************************
// First selection results //
//**************************************
if($_GET['func'] == "country" && isset($_GET['func'])) {
material($_GET['drop_var']);
}

function material($drop_var)
{
include_once('config.php');
$result = mysql_query("SELECT * FROM $tablename WHERE country='$drop_var'")
or die(mysql_error());

echo '';

while($drop_2 = mysql_fetch_array( $result ))
{
echo ''.$drop_2['cities'].'';
}

echo ' ';
// echo '';
}
?>

bharti odedra replied to Bigboy on 02-Jul-12 07:49 AM
try this
1)

if you're using them within a form im sure that you would still use the POST method and then pick it up from the next page as $_POST['menuID'] like a normal form... or if it's not a form you could use $_GET though $_POST is better as the info won't show in the address bar 2)

On the select element you have to use the onChange event and trigger the function that will fill the other select 3)

select box 1

<select  name="list1"  id="list1">
 
<option value="">select</option>
<option
value="1">option1</option>
 
<option value="2">option2</option>
<option
value="3">option3</option>
</select>


your dynamic combo

<div  id="ajax_input">
<select
name="list2" id="list2">
<option
value="">select</option>
</select>

</div>


script

$('#list1').change(function(){

var
value  = $(this).val();

$
(
"#ajax_input").load("serverpage.php?value="+value);

});


you can generate your dynamic select box on

serverpage.php

this will work in

onchange

event not in submit

Bigboy replied to bharti odedra on 03-Jul-12 04:11 AM
My problem is I am new to this, can you add all the code for me for the server.php file. Thanks