Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Box to populate db with one value and form textbox with another

Status
Not open for further replies.

Jay319

Technical User
Feb 21, 2003
16
US
I have a database with these fields ...

insertID
insertAccount
insertAccount_No
insertPublication

I have a list box that is dynamically created so the label shows the insertAccount and value equals insertAccount_No

I have a javascript that when you select an Account from the list box it populates a textbox with the appropriate Account_No . This works just fine.
Code:
<script language="javascript">
function MoveacctNumber()
{
var myVar = document.WADAInsertForm.bkAccount.value;
document.WADAInsertForm.bkAccount_No.value = myVar;
}
</script>

Code:
<select name="bkAccount" id="bkAccount" onChange="MoveacctNumber()">
			    <option value="value" >Select An Account</option>
			    <%
While (NOT rsAccount.EOF)
%>
			    <option value="<%=(rsAccount.Fields.Item("insertAccount_No").Value)%>"><%=(rsAccount.Fields.Item("insertAccount").Value)%></option>
			    <%
  rsAccount.MoveNext()
Wend
If (rsAccount.CursorType > 0) Then
  rsAccount.MoveFirst
Else
  rsAccount.Requery
End If
%>
              </select>

When I submit the form it is populating a second database with the Account_No in both the Account and Account Number fields. This is because the value I have selected is the Account_No.

I would like the list menu to perform two functions ... one to populate the Account field in the database with the Account name and two populate the textbox on the form with the Account_No.

any suggestions would be appreciated.

thanks, jay
 
By the looks of it, you would like the list menu to perform two functions:

1 - populate the account field in the db with the account name

2 - populate the textbox on the form with the account number

Is this correct?

If so, you would be looking to use an AJAX style call. There are a number of libraries you could use, but I find jQuery is the easiest. You could even go commando.

The basic way I would tackle this is (using jQuery):

1 - For simplicity, create a separate file to make the call to containing the database connection and sql statements to insert your information into the relevant field in the table.

2 - Make use of jQuery's [tt].change()[/tt] event. Inside that, fire an AJAX call to your page you made in step one.

3 - Populate your text field with your desired data.

Basic JS example:
Code:
//catch the change event for the desired element
//starting from step 2
$("#bkAccount").change({
  //get the account id to write to db
  var acc_id = $(this).val();
  $(this).load("samplepage.php" + acc_id, function(response, status, xhr){
      if (status == 'success'){
        //step 3
        $("#target_textbox_id").val(acc_id);
      }
      else{
        alert("Write to db failed.");
      }
   })
});

Hope it's enough to guide you in the correct direction.

________________________________
Top 10 reasons to procrastinate:
1)
 
whoops:

[tt]"samplepage.php" + acc_id[/tt] should read:
[tt]"samplepage.php?a=" + acc_id[/tt]

________________________________
Top 10 reasons to procrastinate:
1)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top