Search code examples
javahtmljspscriptlet

jsp page drop down list Main Heading default behavior


When editing an entry in the management summary page the “Main Heading” field defaults to “Operations” which is OK when adding a new entry, but NOT OK when editing an existing one with different contents in this field (i.e.Future Plans) which is then over-written to “Operations”. This happens in both Firefox and Chrome.

So, why does the drop down list of "Main Heading" defaults to “Operations” when clicking the "Edit" button. I don't want it to default to “Operations”, but rather I need it to load the original "Main Heading" option whether it was “Operations” or "Future Plans"

code below:

      <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%
/* New MSR Template */
String scriptName = "ad_summary.jsp";
String nextPage = "#";
String prevPage = "#";
%>
<%@include file="includes/getSession.jsp" %>
<%@include file="includes/getGlobal.jsp" %>
<%

String action="";   
if (request.getParameter("action") !=null) {action = (String)request.getParameter("action");}
String rowid="";
if (request.getParameter("rowid") !=null) {rowid = (String)request.getParameter("rowid");}
String service="";
if (request.getParameter("service") !=null) {service = (String)request.getParameter("service");}

String mainHeading="";
if (request.getParameter("mainHeading") !=null) {mainHeading = (String)request.getParameter("mainHeading");}

if (!mainHeading.equals("")){
  mainHeading = Eacute(mainHeading);
}

String serviceName="";
if (request.getParameter("serviceName") !=null) {serviceName = (String)request.getParameter("serviceName");}
String description="";
if (request.getParameter("description") !=null) {description = (String)request.getParameter("description");}

if (!description.equals("")){
  description = Eacute(description);
}

String submit="";   
if (request.getParameter("submit") !=null) {submit = (String)request.getParameter("submit");}
String cancel="";   
if (request.getParameter("cancel") !=null) {cancel = (String)request.getParameter("cancel");}
String itemOrder="1";
if (request.getParameter("itemOrder") !=null) {itemOrder = (String)request.getParameter("itemOrder");}
int whichOrder = Integer.valueOf(itemOrder);

boolean ErrorsDetected = false;
java.util.ArrayList<String> ErrorList = new java.util.ArrayList<String>();

Connection conn = null;
PreparedStatement statement = null;
PreparedStatement statement1 = null;
PreparedStatement statement2 = null;
ResultSet resultset = null;
boolean resultsetHasData = false;

Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/LocalMSR");
conn = ds.getConnection();

String sqlString = "";

//Pick up maximum value 
int MaxValue = 0;
sqlString = "SELECT MAX(item_order)";
sqlString = sqlString + " FROM msr_summary_notices ";
sqlString = sqlString + " WHERE (year = "+MSR_YEAR+") AND (month ="+MSR_MONTH_NUMBER+") AND (topic NOT IN ('Notices','Services'))";

statement = conn.prepareStatement(sqlString);
resultset = statement.executeQuery();
resultsetHasData = resultset.next();

if (resultsetHasData){
 MaxValue = Integer.valueOf(resultset.getString("MAX(item_order)")!=null?resultset.getString("MAX(item_order)"):"0");   
}

resultset.close();
statement.close();

if (action.equals("move_up")){
  action="add";

  if (whichOrder != 1){
    int CurrentOrder = whichOrder;
    int NewOrder = whichOrder - 1;
    String sqlString1 = "UPDATE msr_summary_notices set item_order = "+String.valueOf(NewOrder)+" WHERE item_order = "+String.valueOf(CurrentOrder);
    String sqlString2 = "UPDATE msr_summary_notices set item_order = "+String.valueOf(CurrentOrder)+" WHERE item_order = "+String.valueOf(NewOrder)+" AND absid <>"+rowid;
    statement1 = conn.prepareStatement(sqlString1);
    int RowsAffected1 = (statement1.executeUpdate());
    statement1.close();
    statement2 = conn.prepareStatement(sqlString2);
    int RowsAffected2 = (statement2.executeUpdate());
    statement2.close();
  }else{
    // Do nothing coz you can move up above 1  
  }
}

if (action.equals("move_down")){
  action="add";

  if (whichOrder != MaxValue){
    int CurrentOrder = whichOrder;
    int NewOrder = whichOrder + 1;
    String sqlString1 = "UPDATE msr_summary_notices set item_order = "+String.valueOf(NewOrder)+" WHERE item_order = "+String.valueOf(CurrentOrder);
    String sqlString2 = "UPDATE msr_summary_notices set item_order = "+String.valueOf(CurrentOrder)+" WHERE item_order = "+String.valueOf(NewOrder)+" AND absid <>"+rowid;
    statement1 = conn.prepareStatement(sqlString1);
    int RowsAffected1 = (statement1.executeUpdate());
    statement1.close();
    statement2 = conn.prepareStatement(sqlString2);
    int RowsAffected2 = (statement2.executeUpdate());
    statement2.close();
  }else{
    // Do nothing coz you can move down below MaxValue  

  }
}



if (submit.equals("Add Item")){

  if (mainHeading.equals("")){
     ErrorsDetected = true;
     ErrorList.add("Main Heading Can Not Be Blank");
  }

//  if (serviceName.equals("")){
//     ErrorsDetected = true;
//     ErrorList.add("Sub Topic Can Not Be Blank");
//  }

  if (description.equals("")){
      ErrorsDetected = true;
      ErrorList.add("Description Can Not Be Blank");
  }

  if  (!ErrorsDetected){

  sqlString = " INSERT INTO msr_summary_notices (year,month,topic,sub_topic,item,item_order) ";
  sqlString +=" VALUES ( ";
  sqlString += MSR_YEAR+",";
  sqlString += MSR_MONTH_NUMBER+",";
  sqlString += "'"+ mainHeading.replaceAll("'","''") +"',";
  sqlString += "'"+ serviceName.replaceAll("'","''") +"',";
  sqlString += "'"+ description.replaceAll("'","''") +"',";
  sqlString += String.valueOf(MaxValue+1);
  sqlString += ")";

  statement = conn.prepareStatement(sqlString);
  int RowsAffected = (statement.executeUpdate());
  statement.close();
  }

  action = "add";
//  mainHeading = "";
//  serviceName = "";
  description="";
} 

if (submit.equals("Update Item")){

  if (mainHeading.equals("")){
     ErrorsDetected = true;
     ErrorList.add("Main Heading Can Not Be Blank");
  }

//  if (serviceName.equals("")){
//     ErrorsDetected = true;
//     ErrorList.add("Sub Topic Can Not Be Blank");
//  }

  if (description.equals("")){
     ErrorsDetected = true;
     ErrorList.add("Number Can Not Be Blank");
  }

  if  (!ErrorsDetected){

          sqlString =  " UPDATE msr_summary_notices ";
      sqlString += " SET topic = '"+ mainHeading.replaceAll("'","''") +"',";
      sqlString += " sub_topic = '"+ serviceName.replaceAll("'","''") +"',";
      sqlString += " item = '"+ description.replaceAll("'","''") +"'";
      sqlString += " WHERE absid = "+rowid;

      statement = conn.prepareStatement(sqlString);
      int RowsAffected = (statement.executeUpdate());
      statement.close();
    }  
      action = "add";
      mainHeading = "";
      serviceName = "";
      description="";

} 

if (submit.equals("Delete Item")){
  sqlString =  " DELETE FROM msr_summary_notices ";
  sqlString += " WHERE absid = "+rowid;

  statement = conn.prepareStatement(sqlString);
  int RowsAffected = (statement.executeUpdate());
  statement.close();

  action = "add";
  mainHeading = "";
  serviceName = "";  
  description="";

}

if (cancel.equals("Cancel")){

  action = "add";
  mainHeading = "";
  serviceName = "";  
  description="";

} 


String absid = "";
String item = "";
String sub_topic = "";
String topic = "";
String item_order = "";

String item_no_hash = "";
String sub_topic_no_hash = "";

sqlString = "SELECT absid, topic, sub_topic, item ,item_order";
sqlString = sqlString + " FROM msr_summary_notices ";
sqlString = sqlString + " WHERE (year = "+MSR_YEAR+") AND (month ="+MSR_MONTH_NUMBER+") AND (topic NOT IN ('Notices','Services')) ";
sqlString = sqlString + " ORDER BY item_order ";

statement = conn.prepareStatement(sqlString);
resultset = statement.executeQuery();
resultsetHasData = resultset.next();

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>G&Eacute;ANT Monthly Service Reports</title>
<meta http-equiv="content-type" content="text/html;charset=ISO-8859-1"/>

<!-- Template CSS -->
<link rel="stylesheet" type="text/css" href="./media/css/geant.css" />
<link rel="stylesheet" type="text/css" href="./media/css/ddaccordion.css" />
<link rel="stylesheet" type="text/css" href="./media/css/tweaks.css" />

<!-- Dynamic Drive Smooth Navigational Menu Script -->
<script type="text/javascript" src="./media/js/ddaccordion.js"></script>
<script type="text/javascript" src="./media/js/jquery-1.2.6.js"></script>
<script type="text/javascript" src="./media/js/jquery-1.3.2.js"></script>

<!-- start of sub menu  -->

<%@include file="includes/administration_submenu.jsp" %>

<!-- end of sub menu -->

<script type="text/javascript">

function showRow(RowId){

 var Row = document.getElementById("tr_"+RowId);
 Row.style.backgroundColor="#CCCCFF";

}
function hideRow(RowId){

     var Row = document.getElementById("tr_"+RowId);
     Row.style.backgroundColor="#FFFFFF";

}

function doDummy(id) {

<% if ((action.equals("move_up"))||(action.equals("move_down"))){ 
    out.println("showRow("+rowid+")");
}%>

<% if ((action.equals("edit"))||(action.equals("delete"))){ %>
<%    if (mainHeading.equals("Operations")){ %>
    document.forms["addNotices"].mainHeading.value = "Operations";
<%}%>
<%    if (mainHeading.equals("Future Plans")){%>
    document.forms["addNotices"].mainHeading.value = "Future Plans";
<%}%>
<%}%>


}

</script>

</head>

<body onLoad="doDummy(0)">
<div class='nineninefour whitebg'>

<!-- start of header  -->
    <%@include file="includes/header.jsp" %>
<!-- end of header  -->

<div class='clearboth weeny'>&nbsp;</div>

<div class='ninesevensix dottedline'>
    <div class='floatleft breadcrumbs'>
      <span><a href="home.jsp">MSR</a></span><span> &gt; </span><span>Administration</span><span> &gt; </span><span>Management Summary</span>
    </div>
    <div class='floatright breadcrumbs'>
      <script type="text/javascript" src="./media/js/datetime.js"></script>
    </div>
    <div class='clearboth weeny'>&nbsp;</div>
</div>

<div class='ninesevensix paddedtop'>


<!-- start of main menu  -->

<%@include file="includes/mainmenu.jsp" %>

<!-- end of main menu -->

<div class='seventhirty floatright'>
    <div class='floatleft seventwenty'>

        <div id="Main.text">
           <table width="720" border="0" cellspacing="0" cellpadding="0">
           <tr><td>
           <h3 class="blueblack nopad">Management Summary - <%=MSR_MONTH%> <%=MSR_YEAR%></h3>
           </td>
           <%@include file="includes/pagemenu_default.jsp" %>
           </tr>
           <tr><td colspan="2" class="hr_seventwenty maindottedline">&nbsp;</td></tr>
                   </table>

           <table width="720" border="0" cellspacing="5" cellpadding="0">
                   <form name="addNotices" action="ad_summary.jsp" method="post">
                   <input type="hidden" name="action" value="<%=action%>"/>
                   <input type="hidden" name="rowid" value="<%=rowid%>"/>

     <%
        /* Array to return any errors that may have been thrown at the top of the page */
        if (ErrorsDetected) {           
            for (int i=0; i< ErrorList.size(); i++){
                out.print("<tr bgcolor = '#FF6666'><td colspan='2' align='center'><strong>"+ErrorList.get(i)+"</strong></td></tr>");    
            }
        }  
        if (action.equals("delete")) {          
          out.print("<tr bgcolor = '#FFFF66'><td colspan='2' align='center'><strong>Are You Sure You Want To Delete This Record?</strong></td></tr>");  
        }  


     %>
       <tr>
         <td valign="top" width="150">Main Heading</td>
         <td>
         <select name="mainHeading" id="mainHeading">
            <option value="Operations">Operations</option>
            <option value="Future Plans">Future Plans</option>
         </select>
         </td>

       </tr>
       <tr>
         <td valign="top">Sub Topic</td>
         <td><input type="text" size="50" name="serviceName" id="serviceName" value="<%=serviceName%>" /></td>
       </tr>
       <tr>
         <td valign="top">Description</td>
         <td><textarea name="description" id="decription" rows="5" cols="68" ><%=description%></textarea></td>
       </tr>

       <tr>
<%     if (action.equals("edit")){%> 
        <td colspan="2" align="center"><input type="submit" name="submit" value="Update Item"></td>
<%     }%> 
<%     if (action.equals("delete")){%> 
        <td colspan="2" align="center"><input type="submit" name="submit" value="Delete Item">&nbsp;<input type="submit" name="cancel" value="Cancel"></td>
<%     }%> 
<%     if (action.equals("add")||action.equals("")){%> 
        <td colspan="2" align="center"><input type="submit" name="submit" value="Add Item"></td>
<%     }%> 
      </tr> 
      </form>
      </table>

      <table width="720" border="0" cellspacing="0" cellpadding="0">
      <tr><td>
      <h3 class="blueblack nopad">Current Management Summary - <%=MSR_MONTH%> <%=MSR_YEAR%></h3>
      </td>
      </tr>
      <tr><td class="hr_seventwenty maindottedline">&nbsp;</td></tr>
      </table>

 <% if (resultsetHasData) { %>     
       <table  class="datatable" width="720" border="0" cellspacing="0" cellpadding="0">
       <tr bgcolor="#BEDC00">
       <th class="tablepad thborder_bottom">Main Heading</th>
       <th class="tableborder_left_white">Sub Topic</th>
       <th class="tableborder_left_white">Description</th>
       <th colspan="4" class="tableborder_left_white">Actions</th>
       </tr>

   <%
     int RowCount = 0;
     while (resultsetHasData) {
       RowCount++;
       absid = resultset.getString("absid")!=null?resultset.getString("absid"):"";
       item = resultset.getString("item")!=null?resultset.getString("item"):"";
       sub_topic = resultset.getString("sub_topic")!=null?resultset.getString("sub_topic"):"";
       topic = resultset.getString("topic")!=null?resultset.getString("topic"):"";
       item_order = resultset.getString("item_order")!=null?resultset.getString("item_order"):"0";

       item_no_hash = UnHashIt(item);
       sub_topic_no_hash = UnHashIt(sub_topic);


// Convert Geant into E-acute version       
//       item = Eacute(item);
//       sub_topic = Eacute(sub_topic); 
//       topic = Eacute(topic); 
  %>
     <tr id="tr_<%=String.valueOf(RowCount)%>" onmouseover="showRow(<%=String.valueOf(RowCount)%>)" onmouseout="hideRow(<%=String.valueOf(RowCount)%>)">
     <td   class="tablepad tableborder_bottom" valign="top" width="200px">(<%=String.valueOf(RowCount)%>) <%=topic%></td>
     <td valign="top" width="200px" class="tableborder_left"><%=sub_topic%></td>
     <td valign="top" width="280px" class="tableborder_left"><%=item%></td>
     <td valign="top" class="tableborder_left"><a href="ad_summary.jsp?action=edit&rowid=<%=absid%>&description=<%=item_no_hash%>&serviceName=<%=sub_topic_no_hash%>&mainHeading=<%=topic%>" target="_top"><img src="./media/images/edit.jpeg" border="0"/></a></td>
     <td valign="top" class="tableborder_bottom"><a href="ad_summary.jsp?action=delete&rowid=<%=absid%>&description=<%=item_no_hash%>&serviceName=<%=sub_topic_no_hash%>&mainHeading=<%=topic%>" target="_top"><img src="./media/images/delete.jpeg" border="0"/></a></td>
     <td valign="top" class="tableborder_bottom"><a href="ad_summary.jsp?action=move_up&rowid=<%=absid%>&itemOrder=<%=item_order%>" target="_top"><img src="./media/images/16_arrow_up.png" border="0"/></a></td>
     <td valign="top" class="tableborder_bottom"><a href="ad_summary.jsp?action=move_down&rowid=<%=absid%>&itemOrder=<%=item_order%>" target="_top"><img src="./media/images/16_arrow_down.png" border="0"/></a></td></tr> 

 <% resultsetHasData = resultset.next();    } %>



     </table>
 <% } else { // endif resultset1HasData%>
 <p>There are no Management Summary Items for this period.</p>
 <% } %>

                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>

        </div>

        <h3 class='blueblack'>&nbsp;</h3>
    </div>

<!-- Right hand menu was here -->   

    <div class='clearboth weeny'>&nbsp;</div>
</div>
        <div class='clearboth weeny'>&nbsp;</div>   
    </div>
</div>

<!-- start of global footer  -->

<%@include file="includes/footer.jsp" %>

<!-- end of global footer  -->

</body>
</html>
<%
if (resultset != null) {
  try {
      resultset.close();
  }
  catch (SQLException e) {
       // Process Errors
  }
  resultset = null;
}   

if (statement != null) {
  try {
      statement.close();
  }
  catch (SQLException e) {
      // Process Errors
  }
  statement = null;
} 

if (conn != null) {
  try {
      conn.close();
  }
  catch (SQLException e) {
       // Process Errors
  }
  conn = null;
}   
%>
<%!
private String Eacute(String inputString){

    String convString = inputString;
    String replaceString = "";

    if (convString.indexOf("GEANT")!= -1){
      replaceString = "G&Eacute;ANT";
      convString = convString.replaceAll("GEANT",replaceString);
    }   

    if (convString.indexOf("Geant")!= -1){
      replaceString = "G&Eacute;ANT";
      convString = convString.replaceAll("Geant",replaceString);
    }

    if (convString.indexOf("geant")!= -1){
      replaceString = "G&Eacute;ANT";
      convString = convString.replaceAll("geant",replaceString);
    }

    if (convString.indexOf("GÉANT")!= -1){
      replaceString = "G&Eacute;ANT";
      convString = convString.replaceAll("GÉANT",replaceString);
    }   

    if (convString.indexOf("Géant")!= -1){
      replaceString = "G&Eacute;ANT";
      convString = convString.replaceAll("Géant",replaceString);
    }


    return convString;

}

private String UnHashIt(String inputString){
    String encodedValue = "";
    try{
        encodedValue = java.net.URLEncoder.encode(inputString, "iso-8859-1");
    } catch (Exception e) {
        //do something
    }
    return encodedValue;
}

private String HashIt(String inputString){

    String decodedValue = "";
    try{
        decodedValue = java.net.URLDecoder.decode(inputString, "iso-8859-1");
    } catch (Exception e) {
        //do something
    }
    return decodedValue;

}
%>

I am mainly concerned with "edit" button

Please see an example below with 2 screenshots

1- Entry type of "Future Plans" and I click on "Edit" button enter image description here

2- "Main Heading" defaults to "Operations" although the entry in screenshot above is "Future Plans". I want this "Main Heading" to show "Future Plans"

enter image description here


Solution

  • Try this:

    replace:

      <select name="mainHeading" id="mainHeading">
                <option value="Operations">Operations</option>
                <option value="Future Plans">Future Plans</option>
      </select>
    

    with:

      <select name="mainHeading" id="mainHeading">
    
    <%if(mainHeading != null && mainHeading.equals("Future Plans")){%>
        <option value="Future Plans">Future Plans</option>
        <option value="Operations">Operations</option>
    <%}%>
    
    <%if(mainHeading != null && mainHeading.equals("Operations")){%>
        <option value="Operations">Operations</option>
        <option value="Future Plans">Future Plans</option>
    <%}%>
    
    <%if(mainHeading == null){%>
        <option value="Operations">Operations</option>
        <option value="Future Plans">Future Plans</option>
    <%}%>
    
      </select>
    

    Remove the doDummy function, you won't be needing it anymore. (also remove it from the onload body tag)

    EDIT:

    Okay so the solution was to do this (with only javascript):

    <script>
    //script at the bottom of the body to change select dropdown based on currently selected mainheading value.
    //old method tried to use scriptlets mixed with javascript, but it's easier to do this with javascript.
    const urlParams = new URLSearchParams(window.location.search);
    const myParam = urlParams.get('mainHeading'); 
    //if mainHeading parameter is null or equal to GÉANT Operations show GÉANT Operations dropdown
    if(myParam === 'GÉANT Operations' || myParam === null) {
        document.getElementById('mainHeading').value = 'GÉANT Operations';
    }else{
        document.getElementById('mainHeading').value = 'GÉANT Future Plans';
    }
    </script>