0

I have a form inside the 3rd accordion panel. When the Submit button is clicked, if the verification code entry does not match, the page will reload and display a message saying "Please try again making sure you entered the correct verification code". But when the page reloads all accordion panels are closed & I need the 3rd panel to stay open (or any that are opened) if this happens. Is there javascript that can handle this or would it be better to open a tooltip/popup with the message? If so, where can I find the code for that?

<%@ Language=VBScript%>
<% response.buffer=true%>
<% 
Dim strrandom
Randomize
Random_Number_Min = 7825874
Random_Number_Max = 487985487
Random_Number = Int(((Random_Number_Max-Random_Number_Min+1) * Rnd) + Random_Number_Min)
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<style>
.one {
    border: 1px solid #f00;
    width:100px; 
    font: italic; 
    text-decoration: line-through; 
    color:#4ba698;
    font-size:11pt;
}
button.accordion {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
    transition: 0.4s;
}

button.accordion.active, button.accordion:hover {
    background-color: #ddd;
}

div.panel {
    padding: 0 18px;
    background-color: white;
    max-height: 0;
    overflow: hidden;
    transition: 0.6s ease-in-out;
    opacity: 0;
}

div.panel.show {
    opacity: 1;
    max-height: 500px;
}
</style>
</head>
<body>

<h2>Animated Accordion</h2>

<button class="accordion">Section 1</button>
<div class="panel">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
  <p><a href="#" class="close">Close</a></p>
</div>

<button class="accordion">Section 2</button>
<div class="panel">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
  <p><a href="#" class="close">Close</a></p>
</div>

<button class="accordion">Section 3</button>
<div id="foo" class="panel">
  <form action="tools_send_mail.asp" method="POST" name="FormName" id="frmSGroups" >
    <%Response.write request.querystring("message")%>
    <table id="request" width="600" border="0" cellspacing="0" cellpadding="10" style="font-size:9pt">
      <tbody>
        <tr>
          <td width="33%" valign="top">Name<br>
            <input type="text" size="20" name="Name" maxlength="30" tabindex="8"></td>
          <td colspan="3" valign="top">Email<br>
            <input type="text" size="48" name="Email" maxlength="50" tabindex="9"></td>
        </tr>
        <tr>
          <td valign="top">Address<br>
            <input type="text" size="20" name="address" maxlength="30" tabindex="4"></td>
          <td width="33%" valign="top">City<br>
            <input type="text" size="20" name="City" maxlength="20" tabindex="5"></td>
          <td width="58" valign="top">State<br>
            <input type="text" size="2" name="State" maxlength="2" tabindex="6"></td>
          <td width="108" valign="top">Zip<br>
            <input type="text" size="9" name="Zip_Code" maxlength="11" tabindex="7"></td>
        </tr>
        <tr>
          <td colspan="2" valign="top">For verification purposes, please enter the numbers you see below:
            <div align="right" class="one">
              <% Response.Write Random_Number %>
            </div></td>
          <td colspan="2" valign="top"><input type="text" name="Code" id="Code" size="20" maxlength="9" tabindex="17" required></td>
        </tr>
        <tr>
          <td colspan="4" valign="top" ><input name="random_num" type="hidden" id="random_num" value="<% Response.Write Random_Number %>">
            <%Response.write request.querystring("message")%></td>
        </tr>
        <tr>
          <td colspan="4"><input name="submit" type="submit" class="btn btnGold" value="SUBMIT >>" border="0" tabindex="18"></td>
        </tr>
      </tbody>
    </table>
  </form>
</div>

<script>
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
    acc[i].onclick = function(){
        this.classList.toggle("active");
        this.nextElementSibling.classList.toggle("show");
  }
}
</script>

</body>
</html>
  • From a UX perspective, it'd be great if the inequality could be checked and a message displayed without requiring a reload of the page. Can the value be compared through JavaScript, or is some communication with the server necessary? (Naturally, keep the server-side check in place just in case, since client code is unreliable.) – Serlite Nov 16 '16 at 20:38
  • I am recycling someone else's code - I see no reason why it can't be compared via JavaScript sans reloading the page. I don't know JavaScript - how would I do that? – Hammie Time Nov 16 '16 at 20:46
  • There are a few key steps to your client-side validation: 1) Attach a function to the onsubmit event of the form, which will prevent submission if it fails, then in that function: 2) Get the value of the generated number (either write it in via the server or select the element with JavaScript), 3) Get the value of the user's input from the text field, and 4) Compare the two values, and if they don't match either use an alert (jarring, but it works) or a JS-generated modal to display the error message. Alternatively you can use JS to highlight the field and add a message above it. – Serlite Nov 16 '16 at 20:58
  • My recommendation would be to try this approach out yourself (a great learning experience, and knowledge of JS is really important for web development), and post a new question if you run into issues with it. Here are some resources to help you along: http://stackoverflow.com/questions/3350247 and http://stackoverflow.com/questions/11563638 – Serlite Nov 16 '16 at 21:02
  • Great, good luck! – Serlite Nov 16 '16 at 21:06
  • Looks like i should NOT use javascript to validate - found this on another post "No one's mentioned this yet, but keep in mind that you MUST perform validation in both client-side (JS) and server-side (PHP). Otherwise anyone could turn off their JavaScript and potentially insert invalid information into your database. #1 rule of web development, don't trust anything coming from the client." – Hammie Time Nov 16 '16 at 21:22
  • I think I should find out how to keep the panel open on reload. – Hammie Time Nov 16 '16 at 21:23
  • If JS is your sole validation method, then yes - that's a bad situation that can be exploited by the client. However, a common practice is to have a JS validation for UX considerations, and a server-side validation "just in case". That's why my first comment ended with "Naturally, keep the server-side check in place just in case, since client code is unreliable." – Serlite Nov 16 '16 at 21:24
  • If you're really dead-set on using only a server-side approach though, then my suggestion for keeping the modal open is to manually apply the classes to the button and accordion element using the `WebControl.Attributes` collection if the server-side validation fails. (Note that any elements you want to manipulate will need an ID and `runat='server'` to be easily accessed from code-behind.) – Serlite Nov 16 '16 at 21:36

0 Answers0