Trying to set a flag in the post to allow processing or not. The javascript isn’t working for me I want to have a dialog box pop up and then set a hidden post bool 0 for canceled and 1 for ok. Is there a better way to set a post value based on the confirm return? Everything I search for brings up ASP.NET and it’s postBack value.
The form is generated using simple JSP (I know it’s outdated and JSTL is better this suites my needs):
<form name="delPlayers" method="post" action="deletePlayer.jsp" class="col6 leftpad3 rightpad3">
<input type="hidden" name="confirmed" value="0" />
<select name="playerName">
<% while (results.next())
{
out.print("<option value=\"");
out.print(results.getString("username"));
out.print("\">");
out.print(results.getString("username"));
out.print("</option>");
} %>
</select>
<input type="submit" name="Submit" class="button" value="Submit" onSubmit="return confirmSubmit()" />
</form>
How I was trying to check the confirm return and set the value before the post.
<script type="text/javascript">
<!--
function confirmSubmit()
{
var r = confirm("Remove " + document.forms['delPlayers']["playerName"].value + "?");
if (r)
document.forms['delPlayers']["confirmed"].value = r;
return true ;
else
return false ;
}
-->
</script>
This part :
should appear inside the
<form>tag, not the<input submit>tag. Like this :[EDIT]
You didn’t get it working cause your javascript structure is not as good as needed. I tried to change your code into something like this, and things seem to be better:
Just remember to always use the curly brackets { and } when you write if command inside javascript. You’re not obliged to use them, but it’s a god habit to avoid such nonsensical problem like this.
Technical note : In JS, the
if(condition)without curly brakets {} works with only 1 line of command right after it . In your case, there were 2 command, so the “else” statment was illegal. That’s the reason why you alway should use if with curly bracketsis OK. But
will cause an error. So the best for all case was :