I pull data from a MYSQL database to populate a Drop down
<td class="<?php print $Bank_ca_error;?>">Bank Name</td> <td> <select name="Bank" id="Bank" tabindex=24 style="color: <?php print $TextColour;?>"/> <option><?php print $_SESSION['Bank_ca'] ;?></option> <?php //Get Data to populate drop down $BankQuery = "SELECT BankName FROM tblbank ORDER BY BankName"; $BankResult = mysql_query ($BankQuery); While($nt=mysql_fetch_array($BankResult)) { print"<option $nt[BankName]>$nt[BankName]</option>"; } ?> </select> </td>
I would like based on the value selected populate a text input. So Basically Select the Bank from the List and have it autopopulate the Universal Branch Code in the text input.
I saw an example using Jquery, But I am a complete noob when it comes to this and I cannot get it to work properly
I added the following in the Head section
<script type="text/javascript" src="jquery-1.4.2.min.js"></script> <script type="text/javascript"> jQuery(document).ready(function(){ jQuery('#Bank').live('change', function(event) { $.ajax({ url : 'getData.php', type : 'POST', dataType: 'json', data : $('#myform').serialize(), success: function( data ) { for(var id in data) { $(id).val( data[id] ); } } }); }); }); </script>
I then Added this into the getData.php file
<?php include "../../../includes/dbinfo.inc"; //Connect to database mysql_connect($db_host, $db_username, $db_password); @mysql_select_db($db_database) or die("Unable to select database"); $BankName = $_POST['Bank']; // Selected Bank $query = "SELECT * FROM tblbank WHERE BankName ='{$BankName}'"; $result = mysql_query($query); $row = mysql_fetch_array($result) $BranchCode = $row['UniversalCode']; $arr = array( 'input#BranchCode' => $BranchCode ); echo json_encode( $arr ); ?>
and added the Following to around the inputs and dropdown concerned
<form id='myform'> </form>
I tried to use a solution elsewhere on this site but could not get it to work
Your assistance is greatly appreciated