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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.