使用其ID时无法从选择框中检索数据

时间:2013-09-12 05:02:47

标签: php mysql

这是我的选择标签,但在网格中编辑和添加时我无法检索国家/地区值只显示代码值,我该怎么办?

<select name="country" id="country" >

<option value="" >select country</option>
<?php $select_query= mysql_query("Select * from country");
     while($select_query_array = mysql_fetch_array($select_query))
     {  
?>

<option value="<?php echo $select_query_array['code'];?>" <?php if($_POST['country']==$select_query_array['code']) { ?> selected="selected" <?php } ?> > <?php echo $select_query_array['country']; ?> </option>
<?php } ?>
</select>

4 个答案:

答案 0 :(得分:1)

检查此解决方案: -

<?php $select_query= mysql_query("Select * from country"); ?>
<select name="country" id="country" >
<option value="" >select country</option>
<?php
 while($row = mysql_fetch_array($select_query))
  {
   if($_POST['country']==$select_query_array['code'])
    $selected='selected="selected"';
   else
    $selected='';
   echo '<option value="'.$row['code'].'"'.$selected.' >'.$row['country'].'</option>';
?>
</select>

答案 1 :(得分:0)

<强>实施例

<select name="test">
    <option value="1|Test one">Test one</option>
    <option value="2|Test two">Test two</option>
</select>

然后:

$test = explode('|', $_POST['test']);

然后你最终会以 $ test [0]为“1”而$ test [1]为“Test one”。

答案 2 :(得分:0)

我认为你正在使用jquery ..

尝试这样的事情:

$('#country option:selected').text()

答案 3 :(得分:0)

首先删除while循环,只获取数据然后检查条件

<option value="<?php echo $select_query_array['code'];?>" <?=(isset($_POST['country']) && !empty($_POST['code']) && $_POST['country'] == $select_query_array['code']) ? $select_query_array['code'] : ""; ?> > <?php echo $select_query_array['code']; ?> </option>