0

This is my first question ever in this website which I hope I can explain it well. I did find the same problem but mostly in PHP and I don't really understand (e.g- How to replace url parameter with javascript/jquery?). Here my explaination:

I have a function in Javascript (in page testStudentFile.jsp):

<script language="javascript">

function onParamChange(){

 var thisForm = document.frmParam;
 
 var strBranch = thisForm.reqBranch.value;
 var strFaculty = thisForm.reqFaculty.value;
 var strCourse = thisForm.reqCourse.value;
 
 location.href ="testStudentFile.jsp?reqBranch="+strBranch+"&reqFaculty="+strFaculty+"&reqCourse="+strCourse;
}   

 </script>

Which get the value from here (and post at the same page):

<form name="frmParam" action="testStudentFile.jsp" method="POST">
      
<tr>
  <td width="20%"><strong>Branch:</strong></td>
  <td>
    <%renderDropDownList2(out, "reqBranch",  arrBranch,  requestValue.strBranch,"onchange=\"onParamChange()\"");%><%=strBranchDesc%>
  </td>
</tr>
<tr>
  <td width="20%"><strong>Faculty:</strong></td>
  <td>
    <%renderDropDownList2(out, "reqFaculty",  arrFaculty,  requestValue.strFaculty,"onchange=\"onParamChange()\"");%><%=strFacultyDesc%>
  </td>
</tr>
<tr align="left">
  <td><strong>Course: </strong></td>
  <td>
    <%renderDropDownList2(out, "reqCourse", arrCourse, requestValue.strCourse,"onchange=\"onParamChange()\"");%><%=strCourseDesc%>
  </td>
</tr>  
<tr align="left">
  <td>&nbsp;</td><td><strong><%out.print(arrStudent.size() + " students");%></strong>  
  </td>
</tr> 
<tr>
  <td></td>
  <td><input name="reqSearch" class="iform" type="submit" value="Search"></td>
</tr>   
</form>

And the data was retrieved from the method void that called data using sql query. Example one of the query:-

  <%!
public void populateBranch(JspWriter out, Connection ConnTC, ArrayList arrBranch)throws Exception{

 String sqlSelect =  " SELECT DISTINCT branch FROM university"+
      " WHERE active='Y'"+
      " AND branch='PK'"+
      " OR branch='ZZ'";
 //out.print(sqlSelect);
 PreparedStatement stmtSelect = ConnTC.prepareStatement(sqlSelect);
 ResultSet rsSelect = stmtSelect.executeQuery();
 while(rsSelect.next()){
  String strBranch  = getValue(rsSelect.getString("fbrncd"));
  arrBranch.add(strBranch);
  } 
 rsSelect.close();
 stmtSelect.close();
}
   %>

Here is my problem:

When I choose PK for branch, it will automatically list down the related faculties and next the related courses from the dropdownlists, but when I choose ZZ, the faculty and course choosen from PK branch didnt reset to the default(empty) before I can choose a faculty and a course for branch ZZ. How do I reset the dropdownlist (without reset button) to the default in the value of the parameter of the URL as stated in the Javascript above whenever the branch changed?

(Example)

From--> http://xxx.xxx.xx.x/portal/testApp/request/testStudentFile.jsp?reqBranch=PK&reqFaculty=FAS&reqCourse=AV

To--> http://xxx.xxx.xx.x/portal/testApp/request/testStudentFile.jsp?reqBranch=ZZ&reqFaculty=&reqCourse=

Warm Regards <3,

Eja ;-)

Community
  • 1
  • 1
Eja
  • 9
  • 6
  • You could have a different function which creates your query string. So every time you change a value in a dropdown box, replace the value for Y, PK or ZZ with the newly selected value. – jokarl Apr 13 '15 at 06:12
  • @hochas: Im sorry, I'm still too amateur, so I don't really understand what you try to tell me. – Eja Apr 13 '15 at 06:42
  • My bad! I added a response with JSFiddle and some code instead, hope that helps. – jokarl Apr 13 '15 at 06:44

2 Answers2

0

Do you want clear strFaculty and strCourse when branch is ZZ?? Then, just try this

var thisForm = document.frmParam;

var strBranch = thisForm.reqBranch.value;
var strFaculty = strBranch == 'ZZ' ? '' : thisForm.reqFaculty.value;
var strCourse = strBranch == 'ZZ' ? '' : thisForm.reqCourse.value;

location.href = ....

I hope that this is useful for you. Thanks.

indiflex
  • 11
  • 5
  • Thanks for your answer. Maybe my question I did not explain well. What I mean is, How do I reset the dropdownlist of faculty and course (without reset button) to the default(empty) whenever the branch changed (either PK or ZZ)? ;D – Eja Apr 13 '15 at 06:36
  • ah ha! If you change the branch, the faculty and course set the default(empty) in client side; – indiflex Apr 13 '15 at 07:31
  • after run above code, location.href change to like this, http://xxx.xxx.xx.x/portal/testApp/request/testStudentFile.jsp?reqBranch=ZZ&reqFaculty=&reqCourse= so, server reply empty faculity and course. that's all. otherwise, if you want reset that in client side, study the above answer(manipulate dom). thanks. – indiflex Apr 13 '15 at 07:38
  • Its still not working. ermm..i will try learn the manipulate dom as u told me and as Mr. @hochas taught me. Thanks to you both. I will update if any solution i get. I do understand the answer you both give but have to think how to use it on my coding. Thanks again... ;D – Eja Apr 14 '15 at 06:22
0

Here is my solution to reset a dropdown list when another changes. Based on this thread and this one.

Simply call a function when the value is changed in one dropdown:

onchange="reset()"

In your reset function, select the element you wish to change index of and set it to 0:

var element = document.getElementById('id');
element.value = 0; // first value, an empty option in this example

Updated JSFiddle

Community
  • 1
  • 1
jokarl
  • 1,913
  • 2
  • 24
  • 52
  • Hochas: Thank you so much for the hint! (^.^) I did found this solution elsewhere in the Internet, but in the dropdownlist as u can see in my HTML codes, already have the onchange=onParamChange() function and lead to the function in the Javascript, can I just put the code as u shown just now inside the existing function onParamChange() and how? – Eja Apr 13 '15 at 06:56
  • I guess you want some kind of hierarchical structure to which dropdowns are reset dependent on when others change? Like course is dependent on faculty. – jokarl Apr 13 '15 at 07:00
  • yes, something like that. Im sorry for trouble you. Thanks for sharing your knowledge. ;D – Eja Apr 13 '15 at 07:09
  • Im still confuse how to use the code into my javascript T.T – Eja Apr 13 '15 at 08:24
  • @Eja I added a new JSFiddle, check it out. – jokarl Apr 13 '15 at 09:02
  • Im so sorry, I dont know what I do wrong. I have try so many ways to do with the codes, i do understand a bit how it passing the value. but its seems not working for my renderdropdownlist. Its using string name, not id, so i change getElementById to getElementByName, then I put the location.href= into displayresult() function. And I already try change document.getElementByID into document.frmParam (my form name).Also change the action for the dropdownlist: <%...,"onchange=\"reset(this)\"");%> Still, Im failed. Im totally confuse and lost now. X( – Eja Apr 14 '15 at 01:32
  • if i put onchange=reset(this) and reset(e) function, the value for Branch can't even choose even its in the dropdownlist. When i change onParamChange(), The branch can choose but didn't affect anything on the faculty and course. The important is, nothings happen on location.href= T.T – Eja Apr 14 '15 at 01:46
  • @Eja It's really hard to tell what is wrong with just small samples from your code in these comments. Could you update your original post or use JSFiddle or something similar? – jokarl Apr 15 '15 at 12:00