Link to home
Start Free TrialLog in
Avatar of movoni
movoni

asked on

Alert Box via Response.Write

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
ASKER CERTIFIED SOLUTION
Avatar of kingsfan76
kingsfan76

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kingsfan76
kingsfan76

other than that I don't see a problem in your codes.

kingsfan
  <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
%>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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>"
wow - when I started typing there wasn't anything there...
Avatar of movoni

ASKER

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


Avatar of movoni

ASKER

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
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*
Avatar of movoni

ASKER

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
Avatar of movoni

ASKER

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
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*
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
%>
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.
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*
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
p.s. except that you don;t need 3 calls to response.write() to write out the string.  one call is enough.

kingsfan
Avatar of movoni

ASKER

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 ;)
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
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*
Avatar of movoni

ASKER

Sounds like a plan man.  Thanks for the help.

- Michael
Avatar of movoni

ASKER

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
Not to cause trouble, but I had the same answer first.
Again, not to cause trouble, but I had the same answer first.
Avatar of movoni

ASKER

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
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
@gam3r:  Apology accepted.

Peace,
Dex*
thanks dexstar. merry christmas 2 u and to all!

gam3r
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