2

Possible Duplicate:
option in dropdown box that changes an input box

I am building a scheduler using php-mysql. I have a drop down list for course code and an input box that displays automatically the subject name of the selected course code from the drop down list. Any one can help me with this?

This drop down list should be dynamically getting data from the database and what ever Course Code I select in this list should appear in the input box.

How do I code this in php?

<select name="cCode">
    <option>Subject Code 1</option>
    <option>Subject Code 2</option>
</select>

The subject name of the course code I selected in the list should appear in an input box.

<input type="text" name="sSubject" disabled value="Subject Name"/>
Community
  • 1
  • 1
King Lagahit
  • 21
  • 1
  • 2
  • I assume you'd want to do this without a page refresh. Look into AJAX. Here is a great tutorial, which includes some similar functionality. http://w3schools.com/ajax/default.asp – phpisuber01 Oct 22 '12 at 14:43

1 Answers1

0

Assuming your course list is not that long you could avoid Ajax calls. If that is a case here is what you could do:

.1. Ajust your PHP to generate select tag as below:

<select name="cCode" id="cCode" onchange="javascript:selectChanged();">
<option courseTitle="Course Title 1">Subject Code 1</option>
<option courseTitle="Course Title 2">Subject Code 2</option>
</select>

Please note ID="cCode"; custom tag attribute courseTitle and event onchange="javascript:selectChanged()".

.2. Change input tag as follow:

<input id="courseTitle" type="text" name="sSubject" disabled value="Subject Name"/>

Please note ID="courseTitle".

.3. Use Javascript function below to update input field:

function selectChanged () { var obj = document.getElementById("cCode"); var courseTitle = document.getElementById("courseTitle"); courseTitle.value = obj.options[obj.selectedIndex].getAttribute('courseTitle', 2); }

You can see live demo here: http://jsfiddle.net/salih0vicX/Ptk2M/

salih0vicX
  • 1,363
  • 1
  • 8
  • 9
  • You are very welcome. Since it solved your problem, please consider accepting the answer (by clicking the green check mark) to show others, who might find this question in the future, what the solution is... Thank you. – salih0vicX Oct 22 '12 at 15:45