简体   繁体   中英

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

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!

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>;
    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>

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>
<option value="<?php $baris['nama_supplier'] ?>

应该

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM