We help IT Professionals succeed at work.
Get Started

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

karl_0090 asked
Last Modified: 2013-12-13

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

<form name="events" method="post"  action="<?= $_SERVER['PHP_SELF']?>">
	// 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 align="right" valign="top"><p>First Name:</p></td>
      <input type="text" name="first" id="first" class="heading" value="<?php echo $heading['first']; ?>">
    <td align="right" valign="top"><p>Email:</p></td>
      <input type="text" name="email" id="email" class="date"value="<?php echo $heading['email']; ?>">
    <td align="right" valign="top"><p>Company:</p></td>
      <textarea name="company" id="company" class="details"value="<?php echo $heading['company']; ?>"></textarea>
      <input name="submit" type="submit" class="submitForm" value="Submit">

Open in new window

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