简体   繁体   English

我如何链接两个下拉菜单框,我可以选择一个菜单,它应该在其他下拉框中显示我的列表?

[英]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的列表,我使用,如果否则但它不工作

    <?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>
    }
    ?>

If you want dynamic list using php you shoud use ajax. 如果你想使用php的动态列表,你应该使用ajax。

Try this Example: 试试这个例子:

use this ajax script 使用这个ajax脚本


       $(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;

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. 作为服务器端语言的PHP将需要刷新页面(发布的表单),然后才能获得第一个选定下拉列表的值。

To do this without a page refresh, you will need to do so on the client side, using JavaScript. 要在没有页面刷新的情况下执行此操作,您需要在客户端使用JavaScript执行此操作。 If the list is generated via PHP then you should look into AJAX. 如果列表是通过PHP生成的,那么你应该研究一下AJAX。

//make array like this on header.php //在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 //你的car.php你要去哪里动作..来自这里的header.php

<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 // Ajax功能用于展示车

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 //这里的代码也在ajax.php ..call header.php上

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>';    
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击父级复选框然后打开父级子列表时,如何设置下拉菜单? - How I can set drop down menu when click on check box of parent then open the sub list of parent? 如何根据其他下拉菜单动态填充下拉菜单? - How can i dynamically fill drop down menu depending on some other drop down menu? 如何从下拉选择框列表中将用户选择的选项的值存储到mysql数据库中? - How can I store the value of a user selected option from a drop down select box list into mysql database? 如何在下拉菜单中显示数据库中的列数据作为选择? - How can I show column data from my databasel in a drop down menu as selections? 如何在php中创建动态下拉框? - how can i make a dynamic drop down box in php? 如何在Wordpress主题的下拉菜单上隐藏图像? - How I can hide image on drop down menu in wordpress theme? 我怎样才能在下拉菜单上达到几年 - How can I achieve years on drop down menu 如何获得下拉菜单的标题而不是价值? - How can I get the title of a drop down menu instead on value? 如何将 daterangepicker 日期放入下拉菜单? - How can I make daterangepicker dates into a drop down menu? PHP:如何与两个单独的表单输入共享一个下拉菜单(一个文本区域,一个文件上传) - PHP: how can I share one drop-down menu with two separate form inputs (one textarea, one file upload)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM