Rahul Dhiman
Rahul Dhiman

Reputation: 1

how can i link two drop down menu box where i can select one menu and it should show my list in other drop down box?

i want to link one drop down box menu and as i select one menu then it should show a list according to the selected menu using php,i used if else but its not working

    <?php>
    if(value="tata")
    {
    <option value="vista">vista</option>
    <option value="nano">nano</option>
    <option value="aria">aria</option>
    <option value="manza">manza</option>
    }
    elseif(value="fiat")
    {
    <option value="linea">linea</option>
    <option value="punto">punto</option>
    }
    elseif(value="maruti")
    {
    <option value="swift">swift</option>
    <option value="desire">desire</option>
    <option value="omni">omni</option>
    <option value="maruti 800">maruti 800</option>
    }
    elseif(value="hundai")
    {
    <option value="santro">santro</option>
    <option value="verna">verna</option>
    }
    ?>

Upvotes: 0

Views: 1343

Answers (3)

user1855348
user1855348

Reputation:

If you want dynamic list using php you shoud use ajax.

Try this Example:

use this ajax script


       $(document).ready(function() {
 
$('#dropdown').change( function() {
$('#myform').submit();
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(response) {
$('#output').html(response);
}
});
return false;
});
});

    <form id=myform method=POST action="process.php">
    <select id="dropdown" name="dropdown">
    <option value="tata">TATA</option>
    <option value="fiat">FIAT</option>
    <option value="maruti">MARUTI</option>
    <option value="hundai">HUNDAI</option>
    </select>
    </form>
    <div id="output"></div>

process.php

    <?php
    $value = $_POST['dropdown'];
    $html = "<select name = 'cars' id='cars'>";
     if ($value == 'tata') {
$html .= "<option value='vista'>vista</option><option value='nano'>nano</option><option value='aria'>aria</option><option value='manza'>manza</option>";
     } elseif($value == 'fiat') {
$html .= "<option value='linea'>linea</option><option value='punto'>punto</option>";
     } elseif($value == 'maruti') {
        $html .= "<option value='swift'>swift</option><option value='desire'>desire</option><option value='omni'>omni</option><option value='maruti 800'>maruti 800</option>";
     } elseif($value == 'hundai') {
$html .= "<option value='santro'>santro</option><option value='verna'>verna</option>";
     }
     $html .= "</select>";

     echo $html;
     exit;

Upvotes: 2

Prafulla
Prafulla

Reputation: 590

//make array like this on header.php

$list = array(
    'tata' => array(
        'vista', 'nano', 'aria'
    ),
    'fiat' => array(
        'linea', 'punto'
    ),
    'maruti' => array(
        'swift', 'desire'
    )
};  

// your car.php where you going to do you action..call header.php here

<select name="Manuf" id="Manuf" onchange="show_car(this.value)">
    <option value="">/option>
     <?php 
        foreach($list as $x => $x_value)
        {
           echo '<option value="'.$x.'" >'.$x.'</option>';
        }
    ?>

</select>                                           
<select name="car" id="car"></select>

// Ajax Function For display car

function show_car(manuf)
{  
    var xmlhttp;
    if (manuf.length==0)
    { 
        alert("Select manufacturer");
        return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("car").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","ajax.php?manuf="+manuf,true);
    xmlhttp.send();
}

// code on ajax.php ..call header.php here too

foreach($list as $x => $x_value)
{
if($x == $_GET['manuf'])
  {
    for($i=0; $i<sizeof($x_value);$i++)
     {
         echo '<option value="'.$x_value[$i].'" >'.$x_value[$i].'</option>';    
    }
  }
}

Upvotes: 0

AlexP
AlexP

Reputation: 9857

PHP, being a server side language will require the page to be refreshed (the form posted) before the value of the first selected drop down is available.

To do this without a page refresh, you will need to do so on the client side, using JavaScript. If the list is generated via PHP then you should look into AJAX.

Upvotes: 0

Related Questions