dongzang7182 2016-05-29 18:51
浏览 45

php-当我从下拉列表中选择值时,没有获取值?

I am trying to get a values from the drop down displayed from table .

i have tried in diff ways but i don't know where i have gone wrong.

Can any one help me on this..

Below is my code.

<tr class="form-field" id="appid">
<div>
<th valign="top" scope="row" >
<label for="country"><?php _e('country', 'custom_table_example')?></label>
</th>
<td>
<select id="country" name="country" class="code" >;
<option value="">select country</option>
<?php 
global $wpdb;
$coun_name = $wpdb->get_col("select country_name FROM countries") ;
//print_r($coun_name);
foreach($coun_name as $a)
    {
echo '<option value="'. strtolower($a) .'" />' . "$a </option>";

    }
  ?>
</td>
</div>
</tr>

The above code is displaying values into drop down.

now the problem is i need to get the selected values.

echo '<option value="'. strtolower($a) .'"<?php echo $item['country']==".$a."?'selected="selected"':'' ?> />' . "$a </option>";

$item is the variable where i am storing all data.

country =name attribute.

  • 写回答

1条回答 默认 最新

  • dsunj08246 2016-05-29 19:02
    关注

    Have you set $item from $_POST? Are you using POST as your form action? Do something like this:

    <form name='countryTest' method='POST' action='<?/*where your action is going to*/?>'>
        <select name='country'>
        <?foreach($coun_name as $c){
            ?><option value='<?echo$c;?>'<?if($_POST['country']==$c)echo' selected="selected"';?><?
        }?>
        </select>
    </form>
    

    OR! If you want to be really cool (yea ok not so cool but nerdy)! Make a function or class function to do all this for you!

    class formHelper{
        public function select_form($name,$options=array([0]=>'Please select'),$selected=array(),$multiple=false){//name of select, options, selected options, multiple select
            if(!is_array($selected))$selected=array($selected);
            $sel='<select name="'.(($multiple===true)?$name.'[]':$name).'"';
            if($multiple===true)$sel.=' multiple';
            $sel.='>';
            foreach($options as $value=>$shown){
                $sel.='<option value="'.$value.'" '.((in_array($value,$selected))?'selected="selected"':'').'>'.$shown.'</option>';
            }
            return$sel.='</select>';
        }
    }
    

    Now to use it just do this

    $coun_name=array(merge(array('Please select a country'),$coun_name));
    formHelper::select_form('country',$coun_name,$_POST['country']);
    

    EDIT

    your error is you've set your value to lower but when you're comparing it's not lowered. See strtolower. What you want to do is compare both as lower as $item will be lower. I'd recommend using an integer when comparing like this:

    array(
        [1]=>'England',
        [2]=>'Wales',
        [3]=>'Scotland'
    );
    

    So that your values will be

    <option value='1'>England</option>
    <option value='2'>Wales</option>
    <option value='3'>Scotland</option>
    

    But ye your issue is $item['country']==$a. Needs to be $item['country']==strtolower($a). And remove the string quotes with the full stops. "england" does not equal ".England.". The reason it's "england" already is because you've already set the string to lower. Unless $item is not $_POST['county']'

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口