繁体   English   中英

我如何使用php通过id获取选择选项

[英]How can i get select option by id using php

我想使用php通过id访问下拉列表选项,就像我在javascript中所做的一样。我也想在php中进行此工作。有没有办法做到这一点。请指导。我的JS代码是。

var opt = amove.options[amove.selectedIndex];

   if(opt.id =="Exchange"){          
      document.getElementById('barcode2lb').style.display='block';

      document.getElementById('barcode2txt').style.display='block';
}
else{

     document.getElementById('barcode2lb').style.display='none';

     document.getElementById('barcode2txt').style.display='none';
}  

如果您使用原始PHP,则

<select name="">
  <option value=""> select a option </option>
<?php
  $sql = 'SELECT * FROM table ORDER BY id ASC';
  $result_select = mysql_query($sql);
  $rows = array();
  while($row = mysql_fetch_array($result_select)){
    if($row['which column you want to compare'] == value){
      echo "<option value="".$row['value index']."">".$row['value index']." </option>"
   }
  }
?>

例如,

<select>
<?php foreach($yourArr as $row):
if(yourConidtion){
  $selected='selected';
}else{
  $selected='';
}?>
<option value="<?= $row->id?>" <?= $selected ?>><?= $row->name ?></option>
<?php endforeach;?>
</select>

我想这就是你要的

PHP是一种服务器端语言,因此您将需要JS来使用AJAX发布ID值。 通过JS代码进行AJAX调用,并处理PHP代码中的数据

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM