We help IT Professionals succeed at work.
Get Started

PHP MySQL, Select Data from dropdown menu an populate other fields

karl_0090
karl_0090 asked
on
2,143 Views
Last Modified: 2013-12-13
Hello,

I have a dropdown menu / list that gets data from a MySQL database.

When a user selects the data from the dropdown menu I would like the data that corresponds with that data to be populated into a number of fields in the form.

Here is a example of an attempted solution I found, but it dose not work, something like this would be ideal.

Many Thanks
Karl

<html>
<head>
<title>DropDown</title>
</head>
 
<body>
<form name="events" method="post"  action="<?= $_SERVER['PHP_SELF']?>">
<table>
  <tr>
    <td>&nbsp;</td>
    <td>
      <?php 
	// open connection to MySQL server
	$connection = mysql_connect('localhost', 'root', 'pass')
	or die ('Unable to connect!');
					
	//select database
	mysql_select_db('database') or die ('Unable to select database!');
					
	//create and execute query
	$query = 'SELECT company FROM man_contacts ORDER BY company';
	$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
					
	//create selection list
	echo "<select name='Company'>\n";
					
	while($row = mysql_fetch_row($result))
	{
		$heading = $row[0];
		echo "<option value='$heading'>$heading\n";
	}
	echo "</select>"
	
      ?>
     
    </td>
  </tr>
  <tr>
    <td align="right" valign="top"><p>First Name:</p></td>
    <td>
      <input type="text" name="first" id="first" class="heading" value="<?php echo $heading['first']; ?>">
    </td>
  </tr>
  <tr>
    <td align="right" valign="top"><p>Email:</p></td>
    <td>
      <input type="text" name="email" id="email" class="date"value="<?php echo $heading['email']; ?>">
    </td>
  </tr>
  <tr>
    <td align="right" valign="top"><p>Company:</p></td>
    <td>
      <textarea name="company" id="company" class="details"value="<?php echo $heading['company']; ?>"></textarea>
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>
      <input name="submit" type="submit" class="submitForm" value="Submit">
    </td>
  </tr>
</table>
</form>
</body>
</html>

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 13 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE