Jump to content

How do I pass value from <SELECT..</SELECT> attribute to <INPUT textbox?


sonnieboy

Recommended Posts

      <?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.

 

 

 

 

 

Link to comment
Share on other sites

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>";
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.