Onchange

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Onchange

 
0
  #1
Jul 10th, 2008
Hi
I am new to javascript.
I have two drop downs used to reload the page displaying selected information.

How do I get the OnChange function to pass year and user form the drop down.
I also need to pass data in the array $month.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <body>
  4. <script language="JavaScript" type="text/JavaScript">
  5. <!--
  6.  
  7. function OnChange(dropdown)
  8. {
  9. //reload page after drop down selection
  10. window.location.href="http://localhost/month_view.php?u_user_id=" +
  11. dropdown.options[dropdown.selectedIndex].value "&year="+ "&month="+ ;
  12. }
  13. --->
  14.  
  15.  
  16. <table>
  17. <tr><td>
  18. <!--year drop down-->
  19. <select name ="year" onChange ="javascript:OnChange(this)"
  20. <option value="">Select</option>
  21. <option value="2007">2007</option>
  22. <option value="2008">2008</option>
  23. </select>
  24.  
  25.  
  26.  
  27. <select name = "user_id" onChange ="javascript:OnChange(this)"
  28. <option value="">Select</option>
  29. <option value="1">Frank</option>
  30. <option value="2">Mark</option>
  31. </select>
  32.  
  33. </td>
  34. </tr>
  35. <tr>
  36. <td>
  37. <?php
  38. //display information based on selected year, month and user
  39. display_info($year, $month, $user);
  40. ?>
  41.  
  42. </td>
  43. </tr>
  44. </table>
  45. </body>
  46. </head>
  47. </html>
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 17
Reputation: kceleb9 is an unknown quantity at this point 
Solved Threads: 0
kceleb9 kceleb9 is offline Offline
Newbie Poster

Re: Onchange

 
0
  #2
Jul 10th, 2008
First - it looks like you are trying to re-load the page to early. It is the first line in your function - so you will lose whatever was selected in the drop down.

Clean way is just make a var:

var year;

and assign it to the value of the dropdown at the **beginning** of your function

year = options[dropdown.selectedIndex].value;

if you need to use both year AND user when you refresh the page - then instead of passing (this) from the onchange hander - just give an id to each SELECT element and then look up each one with document.getElementById() in your function.

Then you can get the year and the user before you refresh the page again.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Re: Onchange

 
0
  #3
Jul 13th, 2008
Hi

Thanks for the response and suggestion.
I could not a single function to work.
I could get 2 seperate functions working and it does the job.

I am curious why your suggestion won;t work as one function but will wor as two?

solution

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script language="javascript" type="text/javascript">
  4.  
  5. function YearChange()
  6. {
  7. var year = document.getElementById('year').value;
  8. window.location.href="http://localhost/month_view.php?year=" + year;
  9. }
  10.  
  11. function ProChange()
  12. {
  13. var providerId = document.getElementById('provider_id').value;
  14. window.location.href="http://localhost/month_view.php?u_org_pro_id=" + providerId;
  15. }
  16.  
  17. </script>
  18. </head>
  19. <body>
  20. <table>
  21. <tr><td>
  22.  
  23. <select name= "year" onchange="javascript:YearChange();">
  24. <?
  25. //dynamic year dropdown 14 years (4 back, current and 9 forward)
  26. year_only($year);
  27. ?>
  28. </select>
  29.  
  30.  
  31. <select name= "user_id" onchange="javascript:ProChange();">
  32. <?
  33. //display information based on selected year, month and user
  34. display_info($year, $month, $user);
  35. ?>
  36. </select>
  37.  
  38. </td>
  39. </tr>
  40. </table>
  41. </body>
  42. </html>
Last edited by assgar; Jul 13th, 2008 at 10:10 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC