user3386779
user3386779

Reputation: 7175

how to select the select box item based on another and vice versa

how to select the select box item based on one another and vice versa.here I use ajax for redirect to query page.

while($fet = mysql_fetch_assoc($sql1)) 
{
    echo "<option value=".$fet['username'].">".$fet['username']."</option>";
}

echo '</select></td>';
echo "<td><div id='myDiv'><select id=id name=id onchange=loadXMLDoc()>";

$sql = "select * from sample";
$sql1 = mysql_query($sql);
while($fet = mysql_fetch_assoc($sql1))
{
    echo "<option value=".$fet['id'].">".$fet['id']."</option>";
}
echo '</select></div></td>';

Upvotes: 0

Views: 171

Answers (1)

Fisherman
Fisherman

Reputation: 6121

You Need To Use Ajax For That, For Example You Have A Select like This

<select id=id1 name=id1 onchange=loadXMLDoc()> here is the second one you want to change <select id=id2 name=id2>

On Javascript You Call a php file via ajax Like This

function loadXMLDoc(){
         $.ajax({ // i think you used jquery on your project
            type: "post",
            url: "getdata.php",
            data: $('input[\'name=id1\']'),
            dataType: "json",
            success:(function(result){
                    html = '';
                   if(result.data.length > 0){
                       $.each(result.data, function( index, value ) {
                          //  alert( index + ": " + value );
                            html += '<option value="'+value.id+'">'+value.title+'</option>';
                        });
                        $('#id2').html(html); 
                   }else{
                     // nothing
                   }
             }));

        }); 

   }

then on php file you just echo the json_encode result you want to get

$sql = mysql_query('SELECT id,title FROM your_table Where your_field='.$_POST['id1']);
$query= mysql_query($sql);                
while( $row = mysql_fetch_array($query)){
   $json['data'][] = array('id' =>$row->id,'title'=>$row->title )
            }
 echo json_encode($json);

Upvotes: 1

Related Questions