Skip to main content
Known Participant
February 28, 2011
Question

Populate TextField based on DropDownList selection

  • February 28, 2011
  • 2 replies
  • 2781 views

Hello guys,

I want to fill a textfield based on a menu selection, for example the database has two fields agent_name and agent_number.

agent_name | agent_number

    Scott         |          1

    Dave         |          2

    Matt          |           3

I created a dropdown unsing Dreamweaver CS5, and I fill the values using a database (MySQL), there is a readonly text field called agent_number like the database field, basically I want to put the agent number based on the name selection, if you select Scott, this will autopopulate the number 1.

Can I do this automatically using dreamweaver? Or Do I need to do it using JavaScript?

Regards,

This topic has been closed for replies.

2 replies

Günter_Schenk
Inspiring
February 28, 2011

You might as well use the MySQL Concat() function to display the agent_number value for each list item, example:

<option value="1">Scott (ID: 1)</option>

Here´s a tutorial which explains how to modify your query accordingly

pelonms7Author
Known Participant
February 28, 2011

Thank you very much for your quick answer guys,

Basically there is a table called "agents" that one has the agent_name and agent_number; there is another table called "clients" and that table has the same fields "agent_number and agent_name", when you are adding a new "client" you need to select the agent for that "client" I created a dropdown who is going to show the Names of the agents, and I was trying to create a readonly field who it is going to show the client number.

Here is how it looks

http://lanacionalsa.com/aplicaciones/rutas/clientes/ingresar.php

I want to show the agent number based on the agent selection from the drop down.

Thanks in advance,

Lon_Winters
Inspiring
March 2, 2011

pelonms7 wrote:

I want to show the agent number based on the agent selection from the drop down.

Well, if you really prefer a javascript based solution over achieving the same result with standard SQL methods, you can do it this way:

a) put this javascript function in the document HEAD:

<script type="text/javascript">

<!--

function transfer_agentid () {

for (var i=0;i<document.form.agente.options.length;i++)

if (document.form.agente.options.selected==true)

document.form.agente_numero.value=document.form.agente.options.value;

}

// -->

</script>

b) add an onchange event handler to the SELECT tag, example:

select name="agente" id="agente" onchange="transfer_agentid ()"

However I´d like to remind you that any javascript based approach is not reliable, because you don´t know whether some visitor will have javascript enabled in his/her browser.


I'm guessing the OP wants to write both the Agent name and Agent ID to the Clients table, that's why he wants the ID to display in the text field. If that's the case, then the data is populated into the text field (or a hidden field would work the same) and can be inserted into the DB when the form is submitted.

Lon_Winters
Inspiring
February 28, 2011

First, what is the purpose for this? There may be a better way to accomplish what you're trying to do.

As to your question, the cleanest way would be to use javascript to place the value of the list menu when triggered by the on-change event, or by a "Go" button.

A less elegant way would be to submit the form to the same page and retrieve the value of the selection, having set the text field to that variable. This would require a page reload and the Request code would need to be wrapped in an IF statement as the value would be null the first time the page loads.