简体   繁体   English

PHP中select元素的默认值

[英]Default value for select element from PHP

I have created a contacts database using PHP, MySQL on a XAMPP server. 我已经在XAMPP服务器上使用PHP和MySQL创建了一个通讯录数据库。 The page opens with a 'Contacts' table with Add, Edit & Delete buttons. 该页面以带有添加,编辑和删除按钮的“联系人”表打开。 Add & Delete are working fine. 添加和删​​除工作正常。 Edit button opens a modal form with all the usual inputs which are filled in with values from the table row using... “编辑”按钮将打开一个模态表单,其中包含所有常用输入,并使用...从表行的值中填充...

$("#dlgeditContactName").val(ContactName);
$("#dlgeditEmail").val(Email);

etc. 等等

There is also a select element (Contact Type) which is populated using PHP... 还有一个选择元素(联系人类型),它使用PHP填充...

<?php

$conn  =   new mysqli('xxxx', 'xxxx', 'xxxx', 'xxxx')  or die ('Cannot connect to db');

    $result  =   $conn->query("select contacttypeid, contacttype from tblcontacttypes");

    echo "<select id =  'dlgeditcontacttypeid'>";

    while ($row  =   $result->fetch_assoc()) {
        unset($id, $name);
        $id  =   $row['contacttypeid'];
        $name  =   $row['contacttype']; 
        echo '<option value =  "'.$id.'">'.$name.'</option>';
    }
    echo "</select>";

?>

The contacttypeid & contacttype are int and string eg. contacttypeid和contacttype是int和string,例如。 1, Personal 2, Family etc. 1,个人2,家庭等

At the moment this select box acts as expected and user can choose an option during an edit session. 此刻,此选择框将按预期运行,并且用户可以在编辑会话期间选择一个选项。

I just don't know the best way to put a default value in this select box based upon what's in the value in the table row. 我只是不知道根据表行中的值将默认值放入此选择框的最佳方法。 Filling the other input elements was easy but using the following for the select does not work... 填充其他输入元素很容易,但是使用以下内容进行选择不起作用...

$("#dlgeditcontacttypeid").val(ContactTypeID);

I think I will have to use 'selected' as in ... 我想我将不得不使用“选择”,如...

<option value="audi" selected>Audi</option>

(from W3Schools), but not sure if this is best or even how to do it. (来自W3Schools),但不确定这是最佳方法还是方法。

I thought of a hidden div on the form with the ID but no luck getting the value to PHP 我想到了带有ID的表单上的隐藏div,但是没有运气将值传递给PHP

I tried putting some Java inside the PHP ... 我尝试将一些Java放入PHP中...

echo "<script> function(); </script>";

but it looks 'messy' and I don't believe it's the best approach 但看起来“混乱”,我不认为这是最好的方法

There must be a standard method for this issue - anyone had to deal with this? 必须有一个解决此问题的标准方法-任何人都必须处理这个问题?

Is this what you are looking for: 这是你想要的:

$ContactTypeID = 2; //Considering you have default value with you.

while ($row  =   $result->fetch_assoc()) {
    unset($id, $name);
    $id  =   $row['contacttypeid'];
    $name  =   $row['contacttype'];

    if($id == $ContactTypeID) //If default value and id in loop are same
    echo '<option value =  "'.$id.'" selected="selected">'.$name.'</option>';
    else
    echo '<option value =  "'.$id.'">'.$name.'</option>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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