Search code examples
javascripthtmlperlcgimojolicious

Javascript in CGI display error


I do have this HTML code inside CGI program:

0  @@ process.html.ep
1  <% if ($errormsgs) { %>
2  <% for my $e (@$errormsgs){ %>
3  <%=$e%>
4  <br>
5  <% } %>
6  <a href="javascript:history.back();">Go back to fix the form</a><br>
7  <% } %>
8  <% if ($successmsg) { %>
9  <% for my $s (@$successmsg) { %>
10 <%=$s %>
11 <% } %>
12 <a href="<%= url_for('/') %>">Send another?</a><br>
13 <% } %>

so when line 1 is true, the line 12 doesn't print and that's what I want, but when line 1 is false, then line 6 print, but that shouldn't happen because I surrounded it with if statement brackets, so what I want is when $errormsgs is false, then <a href="javascript:history.back();">Go back to fix the form</a><br> shouldn't be displayed.

Any ideas?


Solution

  • $errormsgs is most likely an empty arrayref, which always evaluates to true. Try this:

    <% if (@$errormsgs) { %>