dscqrkvr9562034621
dscqrkvr9562034621
2013-03-13 08:52
浏览 55
已采纳

php&js combobox触发事件

I have a problem with creating an script which will fill one combobox on base of what is selected in the other combobox.

I am newbie in the field of PHP / JS so please be patient with me. Here it is what I have done so far:

js script which is on click event

<script type="text/javascript">
    function selectDropdown(){
        var dropdownValue=document.getElementById("dropdown").value;
        $field=dropdownValue;
    }
</script>

Base combobox filled with values from database:

<select id="dropdown" name="dropdown" onchange="selectDropdown()">
    <option value="dd">--take an option--</option>
    <?
    $standard = Env::value('standard');
    if (!$status)
        $statement = "select code from standard";
    foreach ($db->query($statement )->fetchall() as $row)
        echo "<option".($row == $standard ? ' selected ' : '').">$row[0]</option>";



    ?>
</select>

and now I want to put the selected option into other combobox - actually I will execute another SQL query with the value chosen previously but the fact that I will have value in variable is more then enough for me to finish the task.

<select name='st' class='txt'>
  <option value="st"><? echo $field; ?></option>
</select>

Posted code is created based on one of the articles I found, I have remake it according to my needs but this does not work. Could anyone point me where I am doing mistake?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dptsivmg82908
    dptsivmg82908 2013-03-13 09:01
    已采纳

    You can't set php variable from javascript, this will not work

    var dropdownValue=document.getElementById("dropdown").value;
    $field=dropdownValue;
    ...
    <option value="st"><? echo $field; ?></option>
    

    you can do two things use ajax to generate other select or use pure javascript, using jQuery it will be something like:

    $('option[value=st]').text(dropdownValue);
    
    点赞 评论

相关推荐