geebee geebee - 1 month ago 4
PHP Question

Trying to Link a populated drop down list PHP

As the title suggests I am trying to link a populated drop down list to a form on another page.

My dropdown list is currently connected to my database which displays the addressID's of 6 people. So when the user selects for example AddressID 3 it will take them to the next page (customerdetails.php) which will then allow them to update the form which will update the database accordingly.

My current code is as follows

<?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>";

?>


Now on the customerdetails.php page i have the code:

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


For the life of me I cannot seem to connect the 2 pages together.

Am i anywhere near the correct path? I would prefer not to use javascript as I have no prior knowledge of it.

Many thanks in advance

UPDATE
customerdetails.php page

<?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'];
?>


Contact Form



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

Answer

First solution (no Javascript)

For a solution without Javascript, you will need to use the select within a form element and use a submit button too to send the information completed/selected in the form to the desired page:

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

UPDATE - Second solution (with Javascript)

For using the getAddressID Javascript function to send the ID instead of using a form, you will need to update the function a bit:

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