尝试链接填充的下拉列表PHP

时间:2016-10-21 07:03:52

标签: php html mysql database

正如标题所示,我试图将填充的下拉列表链接到另一页上的表单。

我的下拉列表当前已连接到我的数据库,该数据库显示6人的地址ID。因此,当用户选择例如AddressID 3时,它将把它们带到下一页(customerdetails.php),这将允许他们更新将相应更新数据库的表单。

我目前的代码如下

<?php
//adding the database connection
$username = "root";
$password = "";
$hostname = "localhost";

//connection to the databse
$dbhandle = mysql_connect ($hostname, $username, $password)
    or die ("Unable to Connect to MySQL");
echo "Connected to MySQL";

//selecting the database we want to work with
$selected = mysql_select_db("my_guitar_shop2", $dbhandle)
  or die("Could not select my_guitar_shop2");
?>
<p>AddressID:</p>    <br>

<?php
$sql = "SELECT addressID FROM addresses";
$result = mysql_query($sql);

echo "<select name='addressID' onchange = 'getAddressID(this)'>";

while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['addressID'] ."'>" . $row['addressID']   ."</option>";
}
    echo "</select>";

?>

现在在customerdetails.php页面上我有代码:

<?php
$adrresIDSelected = $_GET['addressID'];
?>

对于我的生活,我似乎无法将两页连在一起。

我是否在正确的道路附近?我不想使用javascript,因为我事先并不了解它。

非常感谢提前

更新 customerdetails.php页面

<?php
 //adding the database connection
 $username = "root";
 $password = "";
 $hostname = "localhost";

 //connection to the databse
 $dbhandle = mysql_connect ($hostname, $username, $password)
    or die ("Unable to Connect to MySQL");
 echo "Connected to MySQL";

 //selecting the database we want to work with
 $selected = mysql_select_db("my_guitar_shop2", $dbhandle)
   or die("Could not select my_guitar_shop2");
 ?>

 <?php
   $addrresIDSelected = $_GET['addressID'];
 ?>

联系表格

<form class="form">

    <p class="first">
                    <label for="name">FirstLine</label>
        <input type="text" name="firstline" id="first"  />

    </p>

    <p class="second">
                    <label for="email">SecondLine</label>
        <input type="text" name="secondline" id="second"  />

    </p>

    <p class="city">
                    <label for="web">City</label>
        <input type="text" name="city" id="web"  />

    </p>        

    <p class="state">
                    <label for="web">State</label>
        <input type="text" name="state" id="web"  />

    </p>    

            <p class="zip">
                    <label for="web">Zip Code</label>
                    <input type="number" name="zip" id="web" />

    </p>    

    <p class="update">
        <input type="button" value="Update" />
    </p>
            <p class="remove">
        <input type="button" value="Remove" />
    </p>
</form>

1 个答案:

答案 0 :(得分:0)

第一个解决方案(没有Javascript)

对于没有Javascript的解决方案,您需要使用select元素中的form并使用提交按钮将表单中已完成/选中的信息发送到所需的页面:< / p>

...
<form action="customerdetails.php" method="get">
<select name="addressID">
<?php
$sql = "SELECT addressID FROM addresses";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['addressID'] ."'>" . $row['addressID']   ."</option>";
}

?>
</select>
<input type="submit" value="Take me to the other page">
</form>
...

更新 - 第二个解决方案(使用Javascript)

要使用getAddressID Javascript函数发送ID而不是使用表单,您需要稍微更新一下这个函数:

<script>
function getAddressID (option) {
    var addressID = option.value;
    // you do not need the <your_domain> prefix here, as probably both your php scripts are on the same server/domain and same folder
    window.location.replace ("customerdetails.php?addressID =" + addressID);
    //-----------------------------------------------------^
    // Extra space must be removed!
}
</script>