在select mysqli request row dropdown选项上填充表单字段

时间:2015-11-13 16:29:46

标签: javascript php mysql ajax mysqli

请附上我的工作示例副本,其中包含检索与当前用户登录相关联的地址。

mysqli查询成功打印保存到mysql数据库的地址选项,与会话用户名相关联。使用会话用户名也可以成功保存地址。

但是,我想添加一些功能,用select类中的选定选项填充表单字段。

表单用于结帐页面,登录用户可以选择以前保存的地址以完成结帐流程。

非常感谢任何帮助。

表架构:

enter image description here

将用户名列更新为唯一,现在收到重复密钥错误。

代码更新如下:

<div class="delivery-address-select">
    <select class="delivery-address-form-control" name="address">
        <option>Select An Address</option>



$servername = "####";
$username = "####";
$password = "###";
$dbname = "####";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
 } 
$username = $_SESSION['username'];

$sql = "SELECT streetAddress, extendedAddress, locality, region, postalCode FROM addresses WHERE username='$username'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
 while ($row = $result->fetch_array()) {
if (!empty($_POST['address']) && $_POST['address'] == $row['id']) {
    $selected = 'selected="selected"';
} else {
    $selected = '';
}
echo '<option '.$selected.' value="'.$row["id"].'">'.$row["streetAddress"].', '.$row["extendedAddress"].', '.$row["locality"].', '.$row["region"].', '.$row["postalCode"].'</option>';
}
} else {
     echo "0 results";
}

$conn->close();?> 

                            </select>
                    </div>
                              <form id="checkout" method="post" action="server.php">
                              <form id="saveAddress" method="post" action="insert.php">
                    <div class="add-an-address">
                        <div class="add-an-address-title">
                            <button id="add-new-address" class="add-new-address">Add new address</button>
                        </div>
                        <div class="add-an-address-container">
                            <div class="add-an-address-form">
                                    <div class="add-address-form-group">
                                        <div class="add-address-name-section">
                                        <input type="hidden" name="username" value="<?php echo $username; ?>" />
                                            <input type="text" name="firstName" class="delivery-address-form-control-first-name" placeholder="First Name" />
                                        </div>
                                        <div class="add-address-name-section">
                                            <input type="text" name="lastName" class="add-address-form-control" placeholder="Last Name" />
                                        </div>
                                    </div>
                                    <div class="add-address-form-group">
                                        <input type="text" name="company" class="add-address-form-control" placeholder="Business Name" />
                                    </div>
                                    <div class="add-address-form-group">
                                        <input type="number" name="phone" class="add-address-form-control" placeholder="Mobile Phone" />
                                    </div>
                                    <div class="add-address-form-group">
                                        <div class="street-left-section">
                                            <input type="text" name="streetAddress" class="delivery-address-form-control-first-name" placeholder="Street Address" />
                                        </div>
                                        <div class="street-right-section">
                                            <input type="text" name="extendedAddress" class="add-address-form-control" placeholder="Apt, Suite" />
                                        </div>
                                    </div>
                                    <div class="add-address-form-group">
                                        <div class="add-address-name-section">
                                            <input type="text" name="locality" class="delivery-address-form-control-first-name" placeholder="City" />
                                        </div>
                                        <div class="street-right-section">
                                            <input type="text" name="region" class="delivery-address-form-control-first-name" placeholder="State" />
                                        </div>
                                        <div class="street-right-section">
                                            <input type="text" name="postalCode" class="add-address-form-control" placeholder="Postal Code" />
                                        </div>
                                    </div>
                                    <div class="add-address-form-group">
                                        <input type="text" name="add-address-business-notes" class="add-address-form-control" placeholder="Notes" />
                                    </div>
                                    <div class="add-address-form-group-buttons">
                                        <button class="add-address-cancel" type="reset" id="add-address-cancel">Cancel</button>
                                        <button type="submit"  method="post" formaction="insert.php">Save</button>
                                    </div>
                                        </form>                     
                            </div>
                        </div>
                    </div>`

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码。请注意,您必须使用预准备语句来避免SELECT查询中的SQL注入。另外,请确保列usernameunique

为您的选择框添加名称属性。

<select class="delivery-address-form-control" name="address">

然后在你的while循环中:

while ($row = $result->fetch_array()) {
    if (!empty($_POST['address']) && $_POST['address'] == $row['id']) {
        $selected = 'selected="selected"';
    } else {
        $selected = '';
    }
    echo '<option '.$selected.' value="'.$row["id"].'">'.$row["streetAddress"].', '.$row["extendedAddress"].', '.$row["locality"].', '.$row["region"].', '.$row["postalCode"].'</option>';
}