Solved

Alert Box via Response.Write

Posted on 2003-12-09
30
2,125 Views
Last Modified: 2012-08-13
I've got a form that submits to itself to check and see if the data in a field already exists in the database.  I've got the SQL and all working fine, but I need to throw an alert box up if the username already exists.  I'm trying to use response.write to do this.  I have a gut feeling that the alert box isn't popping up because when the form is submitted to itself, it first has to write the code and there's nothing to execute it.  I'd like to use an alert box rather than a VBScript msgbox because I believe the title on the actual dialog is different using the VBS msg and I'd like my site to be consistent.  Here's the piece of code that I am having trouble with:

(a bunch of SQL before this...)
   
if rsCheckUsername.Fields.Item("CLIENT_USERNAME").Value <> "" then
        response.write("<SCRIPT LANGUAGE=javascript>")
            response.write("Alert(""Username is already being used.  Please try another."")")
            response.write("</SCRIPT>")
   else
        'response.redirect("done.asp")  
   end if

Thanks for reading,
Michael
0
Comment
Question by:movoni
  • 8
  • 6
  • 5
  • +3
30 Comments
 
LVL 12

Accepted Solution

by:
kingsfan76 earned 125 total points
ID: 9906593
"Alert" should be spelled "alert"
0
 
LVL 12

Expert Comment

by:kingsfan76
ID: 9906598
other than that I don't see a problem in your codes.

kingsfan
0
 
LVL 8

Expert Comment

by:NowaY
ID: 9906609
  <script type="text/javascript">
      function showMessage(mesg) {
         alert(mesg);
      }
   </script>
   </head>
   <body>

<%
If rsCheckUsername.Fields.Item("CLIENT_USERNAME").Value <> "" then
%>

<script type="text/javascript">
   showMessage("Username is already being used.  Please try another.");
</script>

<%
End If
%>
0
 
LVL 19

Assisted Solution

by:Dexstar
Dexstar earned 125 total points
ID: 9906636
@movoni:

> I'm trying to use response.write to do this.  I have a gut feeling that the
> alert box isn't popping up because when the form is submitted to itself, it
> first has to write the code and there's nothing to execute it.

You are very close!  You need to Response.Write an entire JavaScript function, and then invoke that function with the OnLoad event of the "body" element.

Use code like this:
   if rsCheckUsername.Fields.Item("CLIENT_USERNAME").Value <> "" then
%>
        <HTML>
        <HEAD>
        <SCRIPT LANGUAGE=JavaScript>
        function ShowAlert()
        {
                alert("Username is already being used.  Please try another.");
        }
        </SCRIPT>
        </HEAD>
        <BODY OnLoad='ShowAlert();'>
                Rest of the body goes here
        </BODY>
        </HTML>
<%
   Else
        response.redirect("done.asp")  
   end if
%>

Hope That Helps,
Dex*
0
 
LVL 6

Expert Comment

by:pillbug22
ID: 9906638
What about response.writing out a message to the user insteasd of an alert box?

Just print out the error info at the top of the form (probably make it red or such so they see it), so they can look at their error(s) and be right there at the form to fix them?

For each validation you do, just use a simple counter to increment each time there is an error that needs to be fixed.  Right before you do the SQL update, check to see if the counter is still 0.  If so, do the update.  If not, they need to fix something (which is now listed on the screen).


As for what you have above, have you tried switching the double-double quotes to single quotes?

     response.write("Alert('Username is already being used.  Please try another.');")

Also, sometimes system/browser/compiler settings don't like it when you try to response.write "</script>" .  Try breaking it up, just to be sure...

     response.write"</scr" & "ipt>"
0
 
LVL 6

Expert Comment

by:pillbug22
ID: 9906641
wow - when I started typing there wasn't anything there...
0
 

Author Comment

by:movoni
ID: 9908217
Haha... that was good info though pillbug ;)  I want my errors to be consistant throughout the site and therefore would prefer not writing errors directly to the page.  I think Dexster's info may be the best for my situation and he confirmed that I thought I had the problem figured out (that the code was being written, but not executed) and the OnLoad is probably the answer.  I will let you all know when I get home to test it out.

Thanks for the replies!
- Michael


0
 

Author Comment

by:movoni
ID: 9908267
One quick question for Dex --

In your code, the Javascript function is only written if the IF statemement returns TRUE.  But, the call to that function is always in the BODY tag.  Do you know of any errors certain browswers may throw if a call is made to a JavaScript function that does not exist?

Thanks,
Michael
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9908291
Well, do like I posted it.  If rsCheckUsername.Fields.Item("CLIENT_USERNAME").Value <> "", then there is no body or html tags at all.  Just the "Response.Redirect" line.  You'll have to put that at the very beginning of the page, after all the other DB code.

If you don't like that, then don't include the "OnLoad" event if you didn't generate the function in the code, because, yes, it will cause browsers to complain.

D*
0
 

Author Comment

by:movoni
ID: 9908297
Sorry.. one more thing (arrrg...) for Dex --

It actually looks like your are not using response.write for that code, but rather, the function is being written no matter what.  Am I incorrect?  If it's always there, the alert box will pop up every time the page is loaded.  Don't I have to use response.write to spit all of that Javascript out?

Ok... I won't type any more until you answer :)

- Michael
0
 

Author Comment

by:movoni
ID: 9908321
Gotcha (we cross-posted at the same time).  I will try all of this tonight and come back to close out if good.

Thanks!
Michael
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9908346
Okay, I see why you are confused...  In ASP, you can use <% and %> tags to switch between HTML code and ASP code (everything between the <% and %> is ASP code, everything else is HTML).  So if you do this:

<HTML>
<HEAD>
</HEAD>
<BODY>
<% If Weekday(Date()) = 3 Then %>
TODAY IS TUESDAY
<% Else %>
TODAY IS NOT TUESDAY
<% End If %>
</BODY>
</HTML>

Notice I didn't use Response Write at all.  But in the browser, the user will see either "TODAY IS TUESDAY" or "TODAY IS NOT TUESDAY", but never both.  This same code could also be written like this:

<HTML>
<HEAD>
</HEAD>
<BODY>
<%
     If Weekday(Date()) = 3 Then
          Response.Write("TODAY IS TUESDAY")
     Else
          Response.Write("TODAY IS NOT TUESDAY")
     End If
%>
</BODY>
</HTML>

See?  So Your code should look like this (same as above, except I put in where your other DB code goes):

<%
     ' Your Other DB code goes up here
     if rsCheckUsername.Fields.Item("CLIENT_USERNAME").Value <> "" then
%>
        <HTML>
        <HEAD>
        <SCRIPT LANGUAGE=JavaScript>
        function ShowAlert()
        {
                alert("Username is already being used.  Please try another.");
        }
        </SCRIPT>
        </HEAD>
        <BODY OnLoad='ShowAlert();'>
                Rest of the body goes here
        </BODY>
        </HTML>
<%
   Else
        response.redirect("done.asp")  
   end if
%>

Does that make sense?  So, in their browser, the user will either get a complete HTML page OR they will get the redirect command, but they won't get both.  Furthermore, once you add HTML (either via Response.Write, or via HTML mode), you can't use "Response.Redirect".  You have to do it before any content has been sent to the browser.

I just think it is sometimes easier to switch back to "HTML" mode instead of issuing a bunch of "Response.Write" commands, but the result is going to be the same.

HTH,
D*
0
 
LVL 7

Expert Comment

by:gam3r_3xtr3m3
ID: 9908530
simplier,

<%
'Your codes here
'Your codes here
'Your codes here
If rsCheckUsername.Fields.Item("CLIENT_USERNAME").Value <> "" Then
%>
<script language="JavaScript" type="text/javascript">
alert("Username is already being used.  Please try another.");
history.back() //go back to previous page from history
</script>
<%
Else
        Response.Redirect("done.asp")  
End If
%>
0
 
LVL 7

Expert Comment

by:gam3r_3xtr3m3
ID: 9908554
some tip... lol:
for JavaScript, you dont need to call onLoad if your going to call raw functions since the browser executes the function as it sees it.
both
<script language="JavaScript" type="text/javascript">
alert("Username is already being used.  Please try another.");
</script>
and
<script language="JavaScript" type="text/javascript">
function doAlert(){alert("Username is already being used.  Please try another.");}
</script>
<body onLoad="doAlert()">
would do the same. but the one w/o alert would cut off communication from the server on alert but then continues after the alert box returns true.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 19

Expert Comment

by:Dexstar
ID: 9908690
Well, what do you know.  They do both work.  But, using the OnLoad makes it nicer for the user because without it, you just see the blank page in the background until you click Okay.  With OnLoad, you don't get the Alert window until the page is loaded.

I thought you'd have to use the OnLoad event because when movoni posted his original question, he did it without the OnLoad event, and he said it didn't work.  But it must've been because he used "Alert" instead of "alert".  However, after seeing them both work, I stand by my original suggestion because it is a nicer presentation to the user.

Either way, the basic premise is the same...
     1) Check the DB
     2) If the value is duplicate, generate an HTML page with whatever alert mechanism you want to use.
     3) Otherwise, redirect the response to "done.asp".

@gam3r_3xtr3m3:  Were you mocking my response?  What's up with that?

Dex*
0
 
LVL 12

Expert Comment

by:kingsfan76
ID: 9908977
javascript is case sensitive so "Alert" does cause error.

I really didn't see any problem with the original codes besides the spelling of "alert()", and i tested the code before posting.  

The original code is efficient because it gets written out only when it hits the if statement.  and i think using Response.write() to write out html code is more efficient, except that you have to do more typing and be mroe careful with formatting.

kingsfan
0
 
LVL 12

Expert Comment

by:kingsfan76
ID: 9908992
p.s. except that you don;t need 3 calls to response.write() to write out the string.  one call is enough.

kingsfan
0
 

Author Comment

by:movoni
ID: 9909205
Guys... I do not want to cause any problems here and I feel like a dweeb because the first answer from King was correct.  I had a capital "A" and after hours of toying with it before I posted my original question, that was indeed the problem.  However, once I fixed that, I actually didn't like that the page was blank when the error popped up... so, I did change the code to what Dexter offered.  Dex also taught me something I didn't know in his post about writing HTML without a response.write (c'mon... I'm using Dreamweaver with lots of built in stuff 'cause I didn't want to learn so much ;)

Suggestions on splitting points so I don't tick anyone off?

Thanks... feeling stoopid now ;)
0
 
LVL 12

Expert Comment

by:kingsfan76
ID: 9909256
glad you get a satisfied solution.  well, if you feel like splitting the points, you should give the most points to the the answer that you actually used, which is Dex's.

kingsfan
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9910181
Split them however you feel like.  If it were me, I'd split them 50/50 between myself and kingsfan76.  His was really the only answer you needed, but if my responses were helpful to you, then most agree that is worth something too...  His made it work, mine made it work pretty.  Sounds like you used both solutions to me.

Dex*
0
 

Author Comment

by:movoni
ID: 9910341
Sounds like a plan man.  Thanks for the help.

- Michael
0
 

Author Comment

by:movoni
ID: 9910350
Ok, crap.  I'm an idiot and didn't split them... after all of that.  That's what I get for doing this at 2:30 in the morning.  Dex, I will shoot some points your way somehow tomorrow (lobby?).

Sorry... arrrrg.

MT
0
 
LVL 8

Expert Comment

by:NowaY
ID: 9911877
Not to cause trouble, but I had the same answer first.
0
 
LVL 8

Expert Comment

by:NowaY
ID: 9915894
Again, not to cause trouble, but I had the same answer first.
0
 

Author Comment

by:movoni
ID: 9922474
Noway -- I'm a newbie ASP kinda guy and I didn't get from your post that the Alert to alert was the answer.  I see what you are saying on the second part (where you code is similar to dexters), but it took him to explain all that to me (again... I'm no expert) for me to see that.  Your post had no explanitory info.

Does that make sense?  I absolutely mean no disrespect to you... I don't know any of you.  It just made sense to me once dexter explained it all.

Cool?
MT
0
 
LVL 7

Expert Comment

by:gam3r_3xtr3m3
ID: 9924577
movoni: just split it with dexstar & kingsfan76. they both are the ones whom answered it best.
dexstar: sorry about the *mocking* earlier. i just pasted the code too late. lol

gam3r
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9924585
@gam3r:  Apology accepted.

Peace,
Dex*
0
 
LVL 7

Expert Comment

by:gam3r_3xtr3m3
ID: 9924608
thanks dexstar. merry christmas 2 u and to all!

gam3r
0
 
LVL 8

Expert Comment

by:NowaY
ID: 9924730
I understand what you're saying, however, I look at our code and it is very similar.

Of course it is up to you to deliver the points to whoever you think earned it. I have no problem with that.

Thanks!
Dominick
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

16 Experts available now in Live!

Get 1:1 Help Now