Jump to content

Mysql Dynamic Drop down with auto populated fields


spbryantusa

Recommended Posts

I have a drop down menu that gets its values from a Mysql database.  What I want to do is once someone chooses the company they want it will give me the phone number and email that is in the database for that company.

<?

$dbhost = "localhost";

$dbname = "name";

$dbuser = "user";

$dbpass = "pass";

 

mysql_connect($dbhost,$dbuser,$dbpass);

mysql_select_db($dbname);

 

$sql="select * from via";

$result=mysql_query($sql);

$zeile = mysql_fetch_array($result);

?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

<?php

 

$query5 = "select * from via ORDER BY Customer_Company";

 

echo "<select name=\"Customer_Company\">";

 

$result5 = mysql_query($query5);

echo "<OPTION selected>$Customer_Company";

 

while ($row=mysql_fetch_array($result5))

 

{

$Customer_Company=$row["Customer_Company"];

echo "<option>$Customer_Company";

}

echo "</select>";

?>

                      <br />

                      <input type="submit" name="button" id="button" value="Search by Customer Name" /></form>

 

//Then I want to have it add to my html the Phone number and email  on the same page below it.//

 

Customer Phone Number: <?php echo "$Customer_Phone_Number" ?>

Customer Email: <?php echo "$Customer_Email" ?>

Link to comment
Share on other sites

JAVASCRIPT:

<script language="JavaScript">

function change(){

var sel;

var new_status;

sel = document.getElementById('select');

for (i = 0; i < sel.options.length; i++)

if (sel.options.selected) {

new_value = sel.options.value;

}

if (new_value!=0){

window.location.href = 'example.php?id='+new_value;

}

}

</script>

PHP:

USE THIS TO SELECT THE COMPANY

<select onchange="change();" id="select">

<option value="0">SELECT COMPANY</option>

<?php

$query = "SELECT id,Customer_Company FROM via ORDER BY Customer_Company ASC";

$result = mysql_query($query) or die("Can't create query: " . mysql_error());

while ($row = mysql_fetch_assoc($result)) {

    // THIS SELECTS THE CUSTOMER NAME IN DROP DOWN AFTER IT HAS BEEN SUBMITTED.

    echo "<option value=\"$row[id]\"";

    if ($_GET['id'] == $row['id'])

        echo " selected=\"selected\"";

    echo ">$row[Customer_Company]</option>\n";

}

?>

</select>

RESULT PHP/HTML DISPLAY:

<?php

if (isset($_GET['id'])&&!is_numeric($_GET['id'])) {

    echo "Please select a customer.";

} else {

    $sql = "SELECT * FROM via WHERE id='$_GET[id]'";

    $result = mysql_query($result) or die(mysql_error());

    while ($row = mysql_fetch_array($result)) {

        echo "

Customer Phone Number: $row[customer_number]

<br />

Customer Email: $row[customer_email]

";

    }

}

 

?>

 

?>

MESS WITH THIS.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.