View Full Version : How to display value of selected item(dropdown list) on textbox

03-17-2009, 02:35 AM
hi..i need some ideas...I want to calculate student fees payment on my php page, but my coding here its not working. One more thing is when selected item click and its can display value of the item in other textbox(ex: if course Diploma Multimedia is selected, then the course fee is 19000 is display on txtCourseFee).I'm using java script for the calculation and save into mysql database. The code is here :

<script language="JavaScript" type="text/javascript">
function CalculateFee (form)
var coursefee;
var payamaount = form.txtpayamount.value; (user input)
var balfee;
balfee = (coursefee - payamount);

if (document.form.course[0].selected)
Diploma Multimedia = 19000;
coursefee = Diploma Multimedia;

else if (document.form.course[1].selected)
Diploma Computer Science = 17000;
coursefee = Diploma Computer Science;
form.txtbalfee.value = (balfee);

03-20-2009, 10:47 AM

Assuming you're using a select element for courses and that there is only one form in page you're script needs modifications.

- Don't use blanks in variable names (as in Diploma Multimedia = 19000;)
- Don't execute '(balfee = coursefee - payamount);' before you've assigned a value to coursefee since 'coursefee' won't contain anything at that moment.
- Use the value attribute in the options elements to set the fee for each course, like:

<select name="course" onchange="CalculateFee(document.forms[0])">
<option value="19000">Diploma Multimedia</option>
<option value="17000">Diploma Computer Science</option>

Try this one (assuming you trigger this function upon select onchange event)

<script language="JavaScript" type="text/javascript">
function CalculateFee (form) {
coursefee = form.course.value; //If you use the options value attribute for course fees
payamaount = form.txtpayamount.value; //(user input)
balfee = (coursefee - payamount);
form.txtbalfee.value = (balfee);

Hope this helps