R9102
R9102

Reputation: 727

php-Not getting values while i am selecting values from drop down?

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.

Upvotes: 0

Views: 78

Answers (1)

bashleigh
bashleigh

Reputation: 9314

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']'

Upvotes: 1

Related Questions