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条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?