karl_009 Posted March 3, 2009 Share Posted March 3, 2009 Hello, I am trying to create a dropdown menu list that has a column of data from a table in MySQL that is the first step. The next which I think is a bit harder, when a user selects the data from the dropdown menu / list I would like a number of fields to be populated from the from the table data that the dropdown menu comes from. Then I would like to submit the data that was not entered into the form by the drop down menu to another table. Is there any help that any one can give or point me in the direction of, I have tried to search on Google but not found anything. Many Thanks Karl Quote Link to comment Share on other sites More sharing options...
karl_009 Posted March 3, 2009 Author Share Posted March 3, 2009 Here is an exmple, the dropdown menu is working but when data is selected from the dropdown menu the data is not showing up in the form. Any ideas? Here is the code <html> <head> <title>DropDown</title> </head> <body> <form name="events" method="post" action="<?= $_SERVER['PHP_SELF']?>"> <table> <tr> <td> </td> <td> <?php // open connection to MySQL server $connection = mysql_connect('localhost', 'root', 'pass') or die ('Unable to connect!'); //select database mysql_select_db('db') 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 $row['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 $row['email']; ?>"> </td> </tr> <tr> <td align="right" valign="top"><p>Company:</p></td> <td> <textarea name="company" id="company" class="details"value="<?php echo $row['details']; ?>"></textarea> </td> </tr> <tr> <td> </td> <td> <input name="submit" type="submit" class="submitForm" value="Submit"> </td> </tr> </table> </form> </body> </html> Many Thanks Karl Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 4, 2009 Share Posted March 4, 2009 Hi Looking at that there seem to be a couple of issues. You are looping through populating the drop down list, but the other fields are populated from the $row outside the loop where $row has any values (added to which you have not brought back the required fields in the SQL). Think what you need to do it use an on change event on the drop down list to trigger a javascript function, and use the javascript function to populate the other fields. To do this you would need to have a couple of arrays of the data you want to populate the fields with. For example in your loop build up a couple of strings which would be Javascript arrays, and pull the data from there. All the best Keith Quote Link to comment Share on other sites More sharing options...
Yesideez Posted March 4, 2009 Share Posted March 4, 2009 Here's some code for you to play with - this populates a selection (drop-down combo) box with the contents of a table... $optList='<select name="counties">'; $query=mysql_query("SELECT * FROM counties ORDER BY name ASC"); while ($row=mysql_fetch_assoc($query)) { $optList.='<option value="'.$row['id'].'"'.($row['id']==$selid ? ' selected="selected"' : '').'>'.$row['name'].'</option>'; } $optList.='</select>'; If the existing selected county (id in $selid) matches then the "ternary condition" insertes the "selected" part of the HTML. http://www.addedbytes.com/php/ternary-conditionals/ Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 4, 2009 Share Posted March 4, 2009 Hi Right, had a bit of a play. Think this will give you what you need, with changing the value of the input fields depending on what your user selects from the drop down fields. Not the most efficient solution if you have loads of companies. <?php $vbCrLf = chr(13).chr(10); // open connection to MySQL server $connection = mysql_connect('localhost', 'root', 'pass') or die ('Unable to connect!'); //select database mysql_select_db('db') or die ('Unable to select database!'); //create and execute query $query = 'SELECT company, first, email, details FROM man_contacts ORDER BY company'; $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error()); //create selection list $DropDownList .= "<select name='Company' id='DropDownListId' onchange='javascript:SelectChanged();'> ".$vbCrLf; $DropDownList .= "<option value=''></option> ".$vbCrLf; while($row = mysql_fetch_row($result)) { $heading = $row[0]; $DropDownList .= "<option value='$heading'>$heading</option> ".$vbCrLf; $FirstArrayPhp .= 'FirstArray["'.$heading.'"]="'.$row[1].'";'.$vbCrLf; $EmailArrayPhp .= 'EmailArray["'.$heading.'"]="'.$row[2].'";'.$vbCrLf; $DetailsArrayPhp .= 'DetailsArray["'.$heading.'"]="'.$row[3].'";'.$vbCrLf; } $DropDownList .= "</select> ".$vbCrLf; ?> <html> <head> <title>DropDown</title> <script language="Javascript" type="text/javascript"> var FirstArray = new Array(); <?php echo $FirstArrayPhp; ?> var EmailArray = new Array(); <?php echo $EmailArrayPhp; ?> var DetailsArray = new Array(); <?php echo $DetailsArrayPhp; ?> function SelectChanged() { var Company = document.getElementById('DropDownListId').value; document.getElementById('first').value = FirstArray[Company]; document.getElementById('email').value = EmailArray[Company]; document.getElementById('company').value = DetailsArray[Company]; } </script> </head> <body> <form name="events" method="post" action="<?= $_SERVER['PHP_SELF']?>"> <table> <tr> <td> </td> <td> <?php echo $DropDownList; ?> </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 $row['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 $row['email']; ?>"> </td> </tr> <tr> <td align="right" valign="top"><p>Company:</p></td> <td> <textarea name="company" id="company" class="details"value="<?php echo $row['details']; ?>"></textarea> </td> </tr> <tr> <td> </td> <td> <input name="submit" type="submit" class="submitForm" value="Submit"> </td> </tr> </table> </form> </body> </html> I knocked up a test database and tried that and it worked fine for what I think you want. If you used an autonumber field for the company (ie an ID field, with the company name seperate) this would avoid the possible issue here if you have duplicate company names. By the way, the $vbCrLf is just there to force new lines and make the generated HTML more easily read. Showing my roots having done VB and ASP programming for too long. All the best Keith Quote Link to comment Share on other sites More sharing options...
karl_009 Posted March 4, 2009 Author Share Posted March 4, 2009 Hello kickstart, Thank you for the code it works great exactly what I was thinking of. I do have a auto id field called "conid", I have had a play around trying to include the auto id field in the code so that duplicates wont only display the first record it comes to. If I understand I would have to make users select the company name by id number and not by name, on my tests its only displayed the id number not the name and then duplicates have worked how would have make this work with the names nor just the id number? I don’t think I will allow duplicates, but if I did what to what would I need to change in the code to be able to do so? Also the code gives these messages; Notice: Undefined variable: DropDownList in C:\wamp\www\cmstesting\DDtest.php on line 17 Notice: Undefined variable: ConIDArrayPhp in C:\wamp\www\cmstesting\DDtest.php on line 30 Notice: Undefined variable: FirstArrayPhp in C:\wamp\www\cmstesting\DDtest.php on line 31 Notice: Undefined variable: EmailArrayPhp in C:\wamp\www\cmstesting\DDtest.php on line 32 Notice: Undefined variable: CompanyArrayPhp in C:\wamp\www\cmstesting\DDtest.php on line 33 But the code still works I have just suppressed the notice messages, any idea why these might be popping up? Many Thanks for your help; I thought this was going to be a lost battle. Thanks again Karl Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.