how to set the selected value tag <select> html from database in php?

烂漫一生 提交于 2019-12-11 12:46:58

问题


I'm trying to create a drop down menu that will select a value that is stored in the database. here's the code :

require 'koneksi.php';
    $sql_select = "SELECT * FROM supplier"; 
    $hasil = mysql_query($sql_select);
    if(!$hasil) {
        echo "data supplier not found ".mysql_error();
    }

    $id = $_GET["id"];
    $hasil2 = mysql_query("SELECT * FROM brg_supplier WHERE id_brg=".$id);
    $data = mysql_fetch_array($hasil2);
    if($hasil2) {
    $supplier = $data['nama_supplier'];
    }


<select name="supplier">
     <option value="">---pilih supplier---</option>
     <?php
        while($baris = mysql_fetch_array($hasil)){
     ?>
     <option value="<?php $baris['nama_supplier'] ?>" <?php if     ($supplier==$baris['nama_supplier']) echo 'selected="selected"'; ?> > <?php echo  $baris['nama_supplier']; ?> </option>; 
<?php   }?>
</select>

the problem is my code creates a dropdown with nothing selected. here's the screenshot : link

i've tried all the solutions in the stackoverflow. but the dropdown value still nothing selected. i know that it has to be something simple that i am missing but seriously i cannot figure it out. please anyone help, thanks!


回答1:


I think the problem lies in this line:

<option value="<?php $baris['nama_supplier'] ?>" <?php if     ($supplier==$baris['nama_supplier']) echo 'selected="selected"'; ?> > <?php echo  $baris['nama_supplier']; ?> </option>;

You're missing an echo and it looks funny :/ Try instead:

<option <?php $val=$baris['nama_supplier']; echo "value='$val'";  if($supplier==$val) echo "selected='selected'>";echo $val;?> </option>;



回答2:


    Try this way..

$sel="select f.*,c.category from final f, category c where f.category_id=c.id and f.id='$id'";
$data=mysql_query($sel);
$res=mysql_fetch_assoc($data);    

     <select name="cat">
                       <?php

                        $sql = mysql_query("SELECT * FROM category");
                        while ($row = mysql_fetch_array($sql)){
                           if($row['id'] ==  $res['category_id']){
                        echo '<option value="'.$row['id'].'" selected="selected">'.$row['category'].'</option>';
                           }else{
                       echo '<option value="'.$row['id'].'">'.$row['category'].'</option>';
                           }
                        }
                       ?>
                  </select>



回答3:


Try this out

require 'koneksi.php';
$sql_select = "SELECT * FROM supplier"; 
$hasil = mysql_query($sql_select);
if(!$hasil) {
    echo "data supplier not found ".mysql_error();
}

$id = $_GET["id"];
$hasil2 = mysql_query("SELECT * FROM brg_supplier WHERE id_brg=".$id);
$data = mysql_fetch_array($hasil2);
if(mysql_num_rows($hasil2) > 0) {
$supplier = $data['nama_supplier'];
}


<select name="supplier">
 <option value="">---pilih supplier---</option>
 <?php
    while($baris = mysql_fetch_array($hasil)){
 ?>
 <option value="<?php echo $baris['nama_supplier'] ?>" <?php if      ($supplier==$baris['nama_supplier']) {echo 'selected="selected"';} ?> > <?php echo  $baris['nama_supplier']; ?> </option>; 
<?php   }?>
</select>



回答4:


<option value="<?php $baris['nama_supplier'] ?>

should be

<option value="<?php echo $baris['nama_supplier'] ?>



回答5:


I spent some time trying to find the best solution for this, and came up with a tiny little jQuery code.

First of all you should be using PDO or mysqli instead of mysql, since it's deprecated. Let's assume you've fixed that.

Inside the <form> tag, add an <input type="hidden"/> so that it can storage your database value, for example:

HTML

<form>
    <input id="valueFromDatabase" type="hidden" value="<?php echo $stringFromDB ?>"/>
</form>

Note: in this case, $stringFromDB is a variable that holds your query's return from DB.

So now we have the value of our database inside our HTML code. Now we just need to check if any of the options inside the <select> tag is equal to this value. We'll be doing it with jQuery:

jQuery

$( document ).ready(function() {
    $('option').each(function(){
        if (this.value == $('#valueFromDatabase').val()){
            this.setAttribute('selected', 'selected');
        }
    });
});

What's happening here? We are telling to jQuery to analyze all the <option> tags in the HTML and compare its value with our value from database; if its equal, we add the selected attribute to the equivalent <option>.

You can it working here (used a calendar example).

Hope that helps!



来源:https://stackoverflow.com/questions/23511406/how-to-set-the-selected-value-tag-select-html-from-database-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!