PHP & MySQL - How to Show Selected value on Dropdown Menu

孤街醉人 提交于 2019-12-09 05:00:18

问题


I make edit.php with show all data in form from MySQL.
All data is show on form rightly, but it's not work on dropdown and textarea.

I need help and this is my code

<form method="post" action="editdata.php">
<?php 
  include 'config.php';
  $id = $_GET['id'];
  $sqlTampil = "select * from data_korban Where kasus_id=$id"; 
  $qryTampil = mysql_query($sqlTampil); 
  $dataTampil = mysql_fetch_array($qryTampil); 
?>  

Dropdown value is still default, not selected value and TextArea is blank

<select name="agama" id="agama" value="<?php    echo $rows -> agama;?>">
    <option value="Islam">Islam</option>
    <option value="Khatolik">Khatolik</option>
    <option value="Protestan">Protestan</option>
    <option value="Hindu">Hindu</option>
    <option value="Buddha">Buddha</option>
    <option value="Lain-Lain">Lain-Lain</option>
</select>

<textarea id="alamatkorban" rows="5" name="alamatkorban" 
          value="<?php echo $rows -> alamatkorban;?>" 
          cols="33">
</textarea>

Thank You for Your Help


回答1:


Your biggest issue is you are accessing your database values incorrectly. mysql_fetch_array() does not return an object. It returns an array. So you use array syntax ($rows['key']) not object syntax ($rows->key).

Just check to see if the option value matches the value of $rows['agama']. If so, add the selected attribute.

<select name="agama" id="agama">
    <option value="Islam"<?php if ($rows['agama'] === 'Islam') echo ' selected="selected"'>Islam</option>
    <option value="Khatolik"<?php if ($rows['agama'] === 'Khatolik') echo ' selected="selected"'>Khatolik</option>
    <option value="Protestan"<?php if ($rows['agama'] === 'Protestan') echo ' selected="selected"'>Protestan</option>
    <option value="Hindu"<?php if ($rows['agama'] === 'Hindu') echo ' selected="selected"'>Hindu</option>
    <option value="Buddha"<?php if ($rows['agama'] === 'Buddha') echo ' selected="selected"'>Buddha</option>
    <option value="Lain-Lain"<?php if ($rows['agama'] === 'Lain-Lain') echo ' selected="selected"'>Lain-Lain</option>
</select>

An even better way would be to put all of your options in an array and loop through them to generate your options. Then you can check their values as you loop through them. This would be less code an easier to maintain.

<select name="agama" id="agama">
<?php
$agamas = array('Islam', 'Khatolik', 'Protestan', 'Hindu', 'Buddha', 'Lain-Lain');
foreach ($agamas as $agama) {
      $selected = ($rows['agama'] === $agama) ? ' selected="selected"' : '';
?>
    <option value="Islam"<?php echo $selected; ?>>Islam</option>
<?php
}
?>
</select>

To fix your textarea issue, <textarea> does not have a value attribute. You need to place the content in between the <textarea></textarea> tags:

<textarea id="alamatkorban" rows="5" name="alamatkorban" cols="33"><?php echo $rows['alamatkorban'] ;?></textarea>



回答2:


Okay let us assume that there is a variable that holds the selected value and we name it $selected and the options for our select will be stored in $options.

$selected = "Buddha" ;
$options  = array('Islam', 'Khatolik', 'Protestan', 'Hindu', 'Buddha', 'Lain-lain');

In your edit.php file you should try creating the select element via php echo

<?php
    foreach($options as $option){
        if($selected == $option){
            echo "<option selected='selected' value='$option'>$option</option>" ;
        }else{
            echo "<option value='$option'>$option</option>" ;
        }
    }
?>



回答3:


the option that you want selected needs to have the "selected" property in the option tag.

Islam.

The content of the textarea should exist inside of the open/close e.g.

<textarea id="alamatkorban" rows="5" name="alamatkorban" cols="33"><?php    echo $rows -> alamatkorban;?></textarea>



回答4:


You had your textarea declared value wrong. There's no value tag for textarea. What you need to put to your textarea is like this:

<textarea id="alamatkorban" rows="5" name="alamatkorban" cols="33"><?php echo $rows -> alamatkorban; ?></textarea>



回答5:


Looks like you've got a couple problems: As mentioned, <textarea> tags don't use the value property, but rather you change the inner HTML, so you have:

    <textarea>Text inside the text area is written here, like <?php echo $var; ?></textarea>

Your other problem is because you need a 'Selected' option inside the tag for the option you want selected by default. So:

    <select id="selector">
<?php
$optionArray=array("Option 1","Option 2","Option 3");
foreach ($optionArray as $option){?>
    <option id="<?= $option? >"<? if ($rows[$option]==$option){ echo " selected"; } ?>><?= $option ?></option>
<?}?>

Should do it - that way, you can keep all of your options in an array that is simply looped through. The <?= ($var) ?> tags are php short tags equivalent to <?php echo ($var); ?> to keep things a little shorter.




回答6:


You may try this...textarea should exist inside of the open/close e.g.

<textarea rows="5" cols="33" id="alamatkorban" name="alamatkorban" autofocus autocomplete="off"><?php echo @$row["alamatkorban"]; ?></textarea>


来源:https://stackoverflow.com/questions/22701850/php-mysql-how-to-show-selected-value-on-dropdown-menu

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