how to get asp page to recognize javascript return false

Posted on 2012-08-18
Last Modified: 2012-08-20
Hello experts,
My boss wrote an asp/javascript webpage and wants me to fix it. I know very little about these technologies. Apparently when the javascript returns false it should prevent the page from showing the score at the bottom. But this line still causes the score to show at the bottom: if request.form("submit")="yes" then...
Can anyone tell me what's wrong? Thanks for your help.

<%  dim page_name
    page_name="Page Name" 
    'Log in
    dim go_back
    go_back="<a href='menu.asp'>Log In Menu"

<!--#include virtual=include_files/template_header.asp-->
<!--#include virtual=include_files/incl_styles.asp-->

<script type="text/javascript" language="javascript">
    //Error Handling
    function error_check() {
        if (document.stress.q1.value === undefined) {
            alert("You must answer Question 1.");
            return false;


response.write "Q1: "&request.form("q1")&"<br>"

'Insert a record
if request.form("submit")="yes" then
	%><!--#include virtual=include_files/incl_connect_update.asp--><%
	SQL = " insert into stress "
	SQL = SQL &" (date, score) "
	SQL = SQL &" values('"&now()&"', "
	SQL = SQL &" '"&session("total")&"') "

	oRS.Open SQL,db
	set db = nothing

end if

'Get average score and total takers
if request.form("submit")="yes" then
	%><!--#include virtual=include_files/incl_connect_select.asp--><%
	SQL = " select max(stress_id) totpeople, sum(score)/max(stress_id) avgscore from stress "

	oRS.Open SQL,db

    dim totpeople

    dim avgscore

	set db = nothing

end if

if request.form("submit")="yes" then
response.write "<font color=red><b>Your score is shown at the bottom of the page.</b></font>"
end if

response.write "<form method=post name=stress action=stress_dmp.asp onsubmit='return error_check()'>"%>

<p><table><tr bgcolor=teal>
<td valign=bottom><b><font color=white>Item</td>
<td valign=bottom align=center width=75><b><font color=white>Almost Always</td>
<td valign=bottom align=center width=75><b><font color=white>Usually</td>
<td valign=bottom align=center width=75><b><font color=white>Sometimes</td>
<td valign=bottom align=center width=75><b><font color=white>Almost Never</td>
<td valign=bottom align=center width=75><b><font color=white>Never</td></tr>

<tr bgcolor="#99ffcc"><td>1. Answer the first question.</td>
<td align=center><input type=radio name=q1 value=0 <%if request.form("q1")=0 then response.write "checked" end if %> /></td>
<td align=center><input type=radio name=q1 value=1 <%if request.form("q1")=1 then response.write "checked" end if %>/></td>
<td align=center><input type=radio name=q1 value=2 <%if request.form("q1")=2 then response.write "checked" end if %>/></td>
<td align=center><input type=radio name=q1 value=3 <%if request.form("q1")=3 then response.write "checked" end if %>/></td>
<td align=center><input type=radio name=q1 value=4 <%if request.form("q1")=4 then response.write "checked" end if %>/></td></tr>

<input type=hidden name=submit value=yes />
<input type=submit value=Submit /></form>

<%if request.form("submit")="yes" then %>

<p>Your score is <%response.write ""&session("total")&".<br>"%>
There have been <%response.write ""&totpeople&" tests taken with an average score of "&formatnumber(avgscore,1)&". " %>

<%end if  %>

<!--#include virtual=include_files/template_footer.asp-->

Open in new window

Question by:dpicco
    LVL 52

    Expert Comment

    by:Scott Fell, EE MVE
    Asp /vb runs on the server and javascript runs in the browser, unless of course in classic asp you are running jscript on the server.  In this case though, the js is running on the browser.  This means you can't directly share variables.  At least variables that are in memory because the server side scripting is storing in the memory of the server and the js is storing variables in the users computer.   A couple of ways to pass variables between the two is by sharing a cookie or reading a querystring.    

    It looks like the page is doing some error checking with javascript.  This means the error checking is done before the data is sent to the server.  However, if you have js turned off...then the error checking will not be done.

    Why not just do your error checking on the server with vb instad of js?  Replace the javascript code with the following.

    if request.form q1="" then ' it was not filled out
          errMessage=errMessage&"Please answer Question 1<br>"
          else 'it was filled out and lets see if the answer is in range
          if isnumeric(q1) then 'we are almost there
                if cint(q1)  < 0 or cint(q1)  >  4 then ' it is out of range
                        errMessage=errMessage&"Please mark one of the choices in question 1<br>"
                 end if
          end if
    end if

    Open in new window

    This way if js is turned off, you can still do your error checking.  Also, I would never assume the answer is going to be 0 - 4 just because here is a form and that is why I have checked for that.  

    There are other ways to check.

    if q1<>"" then ' at least there is an answer
         if instr(1,q1,q1test,1) = 0 then ' not in the set
               errMessage=errMessage&"Please answer Question 1<br>"
         end if
    end if

    Open in new window


    q1test = "0,1,2,3,4"
    for each x in arrQ1test
       if cstr(x) = cstr(q1) then
        end if
    if match=0 then 'no match
          errMessage=errMessage&"Please answer Question 1<br>"
    end if

    Open in new window

    Many ways to get to the same end.  But I would choose using server side scripting for something like this.
    LVL 3

    Author Comment

    Thanks for your detailed response. Are you saying javascript can't interpret document.stress.q1.value because that is a server control?
    I think the primary problem here is the use of radio buttons. The browser we're using is requiring one radio button in each group to be selected at all times even with checked="no" on each radio button. So even using the code you supplied, I can't get it to recognize when a user did not make a selection because it uses the default value.
    LVL 3

    Author Comment

    I guess what I need to do is add a Not Specified radio button to each group as the default. Then your code will check server-side if the user selected something else. Thanks again for your response.
    LVL 52

    Accepted Solution

    The javascript can detect if a radio has been clicked and which one.  What I am suggesting is to do the error checking on the server side because it's possible the user has javascript turned off, then no error checking.

    The serverside code can't detect that anything has or has not been clicked.  It can only check the value once it has been submitted.  So if the  question is submitted without being checked, you simply return the message after the form is submitted without doing any processing such as saving to the database.

    If you have to start out with at least one item checked, then as you say you can add another radio option.

    <td align=center><input type=radio name="q1" value="99" <%if request.form("q1")=99 then response.write "checked" end if %>/></td></tr>

    I just noticed in your code, you do not have quotes around the name and value.  Maybe that is why your js is not working.
    <td align=center><input type=radio name=q1 value=0 <%if request.form("q1")=0 then response.write "checked" end if %> /></td>

    should be

    <td align=center><input type=radio name="q1" value="0" <%if request.form("q1")=0 then response.write "checked" end if %> /></td>
    LVL 3

    Author Closing Comment

    Thanks so much for your help.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Read about why website design really matters in today's demanding market.
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now