doulizhi1247 2013-07-07 17:38
浏览 51
已采纳

动态下拉列表无法正常工作

I am trying to pass a js var to a php var, then use that php var to go through and array and populate a drop down list. I can see that the php var is getting the right value but list2 and list3 are not being populated.

If I put the string in myself $list1 = 'somestring'; it works, but when I use $list1 = $_POST['choice']; it doesn't work.

<script>
$(document).ready(function(){ 
    $("#list1").change(function(){
        var selected = this.value;
    $.post("my.php", { choice: selected }, function(data){ 
          alert(data); 
    });
  });
});
</script>

<?php
echo'
<form name="menu">
 <div>
  <select name="list1" size="1" onchange="setOptions(document.menu.list1.options
  [document.menu.list1.selectedIndex].value,document.menu.list2,document.menu.list3);">
    <option value=" " selected></option>
    <option value="one">item1</option>
    <option value="two">item2</option>
    <option value="three">item3</option>
    <option value="four">item4</option>
  </select><br><br>
  <select name="list2" size="1"onchange="setOptions(document.menu.list2.options
  [document.menu.list2.selectedIndex].value,document.menu.list3,' ');">
    <option value=" " selected>Select an option</option>
  </select><br><br>
  <select name="list3" size="1">
    <option value=" " selected>Select an option</option>
  </select><br>
 </div>
</form>';
?>

Drop down list

<script>
  function setOptions(chosen, selbox, selbox2) 
  {
  selbox.options.length = 0;

  if (chosen == " ")
  {
    selbox.options[selbox.options.length] = new Option('Select an option',' ');
    selbox2.options[selbox2.options.length] = new Option("Select an option"," ");
    setTimeout(setOptions(' ',document.menu.list3),5);
  }

     if (chosen == "one")
     {
       showList2(selbox);
     }
     if (chosen == "two")
     {
       showList2(selbox);
     }
     if (chosen == "three")
     {
       showList2(selbox);
     }
     if (chosen == "four")
     {
       showList2(selbox);
     }

     //some code snipped
   }


  function showList2(selbox)
  {
  <?php
  $n = 0;
  $list1 = $_POST['choice'];

    foreach($list[$list1] as $key => $value)
    {
      $key_array[$n] =  $key;
      $Lname[$n] = $list[$list1][$key]['name'];

    $n++;
    }

  $jsArray = json_encode($key_array);
  echo "var jsKeys = ". $jsArray . ";";
  $jsArray = json_encode($Lname);
  echo "var jsNames = ". $jsArray . ";";
  ?>

    for (var i=0;i<jsKeys.length;i++)
    {
    var result = selbox2.options[selbox2.options.length] = new Option(jsNames[i],jsKeys[i]);

      if(i == jsKeys.length-1)
      {
        var result = setTimeout(setOptions(jsKeys[0],document.menu.list3,' '),5);
      }
    }
  return(result);
  }
</script>
  • 写回答

2条回答 默认 最新

  • dpi96151 2013-07-08 06:55
    关注

    The $_POST superglobal is only set when the js function fires, ie when you choose something from the dropdown list. Otherwise remains empty.

    You can check what's in the $_POST array with print_r($_POST)

    Normally, I use a utility method to retrieve data from arrays:

    function arrGet($array, $key, $default = NULL)
    {
        return isset($array[$key]) ? $array[$key] : $default;
    }
    

    This returns the value for the key or the default. Usage like:

    if(arrGet($_POST, 'choice')){
        //do something
    }
    

    Hope this helps...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python