The selected value is received from db in the drop-down list using the php mysql option

I need to get the selected value from db in the select box. please tell me how to do this. Here is the code. Note. The meaning of "options" depends on the category.

<?php $sql = "select * from mine where username = '$user' "; $res = mysql_query($sql); while($list = mysql_fetch_assoc($res)){ $category = $list['category']; $username = $list['username']; $options = $list['options']; ?> <input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" /> <select name="course"> <option value="0">Please Select Option</option> <option value="PHP">PHP</option> <option value="ASP">ASP</option> </select> <?php } ?> 
+11
source share
10 answers

I think you are looking for below code changes:

 <select name="course"> <option value="0">Please Select Option</option> <option value="PHP" <?php if($options=="PHP") echo 'selected="selected"'; ?> >PHP</option> <option value="ASP" <?php if($options=="ASP") echo 'selected="selected"'; ?> >ASP</option> </select> 
+25
source

The easiest way I can think of is this:

Php

 <?php $selection=array('PHP','ASP'); echo '<select> <option value="0">Please Select Option</option>'; foreach($selection as $selection){ $selected=($options == $selection)? "selected" : ""; echo '<option '.$selected.' value="'.$selection.'">'.$selection.'</option>'; } echo '</select>'; 

The code basically puts all your parameters in an array that is called in a foreach loop. The loop checks to see if your $ options variable matches the currently selected item, if it matches, and then $ selected will = be selected, if not, it will be empty. Finally, an option tag is returned containing the selection from the array, and if that particular selection is equal to your $ options variable, it is set as the selected option.

+7
source

for example .. and please use mysqli () next time because mysql () is deprecated.

 <?php $select="select * from tbl_assign where id='".$_GET['uid']."'"; $q=mysql_query($select) or die($select); $row=mysql_fetch_array($q); ?> <select name="sclient" id="sclient" class="reginput"/> <option value="">Select Client</option> <?php $s="select * from tbl_new_user where type='client'"; $q=mysql_query($s) or die($s); while($rw=mysql_fetch_array($q)) { ?> <option value="<?php echo $rw['login_name']; ?>"<?php if($row['clientname']==$rw['login_name']) echo 'selected="selected"'; ?>><?php echo $rw['login_name']; ?></option> <?php } ?> </select> 
+5
source
  <select name="course"> <option value="">Please Select Option</option> <option value="PHP" <?php if($options=="PHP") echo "selected"'; ?> >PHP</option> <option value="ASP" <?php if($options=="ASP") echo "selected"'; ?> >ASP</option> </select> 'selected="selected"' instead only "selected" and i think will work 
+2
source

Select a value from the drop-down list.

  <select class="form-control" name="category" id="sel1"> <?php foreach($data as $key =>$value){ ?> <option value="<?php echo $data[$key]->name; ?>"<?php if($id_name[0]->p_name==$data[$key]->name) echo 'selected="selected"'; ?>><?php echo $data[$key]->name; ?></option> <?php } ?> </select> 
+2
source

BEST code and simple

 <select id="example-getting-started" multiple="multiple" name="category"> <?php $query = "select * from mine"; $results = mysql_query($query); while ($rows = mysql_fetch_assoc(@$results)){ ?> <option value="<?php echo $rows['category'];?>"><?php echo $rows['category'];?></option> <?php } ?> </select> 
+1
source

This can help you.

 ?php $sql = "select * from mine where username = '$user' "; $res = mysql_query($sql); while($list = mysql_fetch_assoc($res)) { $category = $list['category']; $username = $list['username']; $options = $list['options']; ?> <input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" /> <select name="course"> <option value="0">Please Select Option</option> // Assuming $list['options'] is a coma seperated options string $arr=explode(",",$list['options']); <?php foreach ($arr as $value) { ?> <option value="<?php echo $value; ?>"><?php echo $value; ?></option> <?php } > </select> <?php } ?> 
0
source

You can also do this ...

 <?php $countryname = $all_meta_for_user['country']; ?> <select id="mycountry" name="country" class="user"> <?php $myrows = $wpdb->get_results( "SELECT * FROM wp_countries order by country_name" ); foreach($myrows as $rows){ if( $countryname == $rows->id ){ echo "<option selected = 'selected' value='".$rows->id."'>".$rows->country_name."</option>"; } else{ echo "<option value='".$rows->id."'>".$rows->country_name."</option>"; } } ?> </select> 
0
source

USING POD

 <?php $username = "root"; $password = ""; $db = "db_name"; $dns = "mysql:host=localhost;dbname=$db;charset=utf8mb4"; $conn = new PDO($dns,$username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "select * from mine where username = ? "; $stmt1 = $conn->prepare($sql); $stmt1 -> execute(array($_POST['user'])); $all = $stmt1->fetchAll(); ?> <div class="controls"> <select data-rel="chosen" name="degree_id" id="selectError"> <?php foreach($all as $nt) { echo "<option value =$nt[id]>$nt[name]</option>";}?> </select> </div> 
0
source

Just add an additional hidden option and print the selected value from the database

 <option value="<?php echo $options;?>" hidden><?php echo $options;?></option> <option value="PHP">PHP</option> <option value="ASP">ASP</option> 
0
source

Source: https://habr.com/ru/post/1501585/


All Articles