sonnieboy Posted November 16, 2017 Share Posted November 16, 2017 <?php $conn=mysqli_connect("localhost","myuuser","mypass","mydb"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $select_query="Select pastorname, ministryname from xphias_clients"; $select_query = mysqli_query( $conn,$select_query); echo "<select name='pastorsname' id='pastorsname' class='form-control'>"; while ($rc= mysqli_fetch_array($select_query) ) { echo "<option value='" .$rc['pastorname'] . "'>" . $rc['pastorname'] . "</option>"; } echo "</select>"; ?></td> <td><INPUT TYPE="TEXT" class="form-control" NAME="ministriesname" id='ministriesname' SIZE="16"></td> //JS <script type="text/javascript"> $(document).ready(function(){ $('#pastorsname').change(function(){ var ministriesname = $(this).val(); $('#ministriesname').val(ministriesname); }); }); </script> Greetings again. I need your expert advise once one. I have a dropdown populated dynamically from the database. Then I have an input text box. What we would like to do is select a value from the dropdown list and then the value associated with the dropdown list value gets automatically populated in the textbox. The issue here is that the value I need to be populated in a textbox is not part of the dropdown list. For instance, the database fieldname of the value that automatically populates a dropdown is called pastorname. However, the database fieldname of the value we would like to get automatically populate a textbox when associated value is selected from the dropdown, is not part of the dropdown. Any ideas how to handle this? Here is the current code. Many thanks for your assistance. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 16, 2017 Share Posted November 16, 2017 try echo "<option value='{$rc['ministryname']}'>{$rc['pastorname']}</option>"; Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted November 16, 2017 Author Share Posted November 16, 2017 Thanks a lot Barand. I actually thought about that option but then then how do I insert the value of pastorname into the database? In .NET, we would insert the textItem, in this case pastorname. What's the equivalence in php? Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 16, 2017 Share Posted November 16, 2017 Thanks a lot Barand. I actually thought about that option but then then how do I insert the value of pastorname into the database? In .NET, we would insert the textItem, in this case pastorname. What's the equivalence in php? If you are going to store the pastorname, then why do you want the value to be the ministrytname? Honestly, I think you are going about this all wrong. Your table should have a unique identifier and THAT should be the value of the select list. And THAT should be the value you store as their selection. Select id, pastorname, ministryname from xphias_clients Then you can make the label either name or even both. But, you should capture and store the ID of the selected option. echo "<option value='{$rc['id']}'>{$rc['pastorname']} ({'{$rc['ministryname']}})</option>"; Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted November 16, 2017 Author Share Posted November 16, 2017 The user wants the value of ministryname to have its own textbox, not to part of pastorname value. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 16, 2017 Share Posted November 16, 2017 What is the relationship between pastor and ministry? Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted November 17, 2017 Author Share Posted November 17, 2017 Pastor is the Pastor of a Ministry if I understand you correctly. So, that table is where pastor and ministry information is stored. Then there is another table stores pastor/ministry related information. So, to add information to this table, user is required to select the pastor from dropdown. Once that pastor is selected, his/her ministry is automatically selected too based on the pastor of that ministry. 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.