[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to get asp page to recognize javascript return false

Posted on 2012-08-18
5
Medium Priority
?
1,330 Views
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.");
            document.stress.q1.focus();
            return false;
        }
    }
</script>

<%session("total")=request.form("q1")+0

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
	db.close
	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
    totpeople=oRS("totpeople")

    dim avgscore
    avgscore=oRS("avgscore")

	db.close
	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>

</table></p>					
<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)&". " %>
</p>

<%end if  %>

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

Open in new window

0
Comment
Question by:dpicco
  • 3
  • 2
5 Comments
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 38308767
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.

<%
errMessage=""
q1=request.form("q1")
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.

q1test="0,1,2,3,4"
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


or

match=0
q1test = "0,1,2,3,4"
arrQ1test=split(q1test,",")
for each x in arrQ1test
   if cstr(x) = cstr(q1) then
       match=match+1
    end if
next
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.
0
 
LVL 3

Author Comment

by:dpicco
ID: 38308995
padas,
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.
0
 
LVL 3

Author Comment

by:dpicco
ID: 38309007
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.
0
 
LVL 54

Accepted Solution

by:
Scott Fell,  EE MVE earned 2000 total points
ID: 38309768
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>
0
 
LVL 3

Author Closing Comment

by:dpicco
ID: 38311763
Thanks so much for your help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
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…
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 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