PHP & MySQL - 如何在下拉菜单上显示选定的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22701850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:11:42  来源:igfitidea点击:

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

phphtmlmysqlselecttextarea

提问by user3466927

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.

我使 edit.php 以从 MySQL 的形式显示所有数据。
所有数据都正确显示在表单上,​​但不适用于下拉列表和文本区域。

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

下拉值仍为默认值,未选择值且 TextArea 为空

<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

感谢您的帮助

回答by John Conde

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).

您最大的问题是您错误地访问了数据库值。mysql_fetch_array()不返回对象。它返回一个数组。因此,您使用数组语法 ( $rows['key']) 而不是对象语法 ( $rows->key)。

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

只需检查选项值是否与 的值匹配$rows['agama']。如果是,请添加该selected属性。

<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 valueattribute. You need to place the content in between the <textarea></textarea>tags:

要解决您的 textarea 问题,<textarea>没有value属性。您需要将内容放在<textarea></textarea>标签之间:

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

回答by YouSer

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

好的,让我们假设有一个变量保存选定的值,我们命名它$selected,我们选择的选项将存储在$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

在您的 edit.php 文件中,您应该尝试通过 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>" ;
        }
    }
?>

回答by Grand Marshal Braev

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

textarea申报的价值有误。没有价值标签textarea。你需要把你的东西textarea是这样的:

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

回答by James Palawaga

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

您想要选择的选项需要在选项标签中具有“selected”属性。

Islam.

伊斯兰教。

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

textarea 的内容应该存在于打开/关闭的内部,例如

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

回答by Helpful

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

看起来您遇到了一些问题:如前所述,<textarea>标签不使用该value属性,而是您更改了内部 HTML,因此您有:

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

应该这样做 - 这样,您可以将所有选项保存在一个简单循环的数组中。这些<?= ($var) ?>标签是 php 短标签,相当于<?php echo ($var); ?>让事情更短一些。

回答by Tapash

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

你可以试试这个……textarea应该存在于打开/关闭的内部,例如

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