Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Message Box on ASP 1

Status
Not open for further replies.

david7777777777

Programmer
Sep 26, 2001
417
US
How, exactly, do I code a MsgBox on my ASP page? I want it to appear when the btnUpdate button is clicked on my page. Thanks.
 
Dave,

The question tells me you're using VBScript on the client. Here's the code for that. I would persuade you from using that on any site that you want to access with Netscape. Netscape doesn't understand client side VBScript.

Code:
MsgBox("Some Message Here")

Let me know if you need an explanation between client side and server side script. Because you can have whatever you want on the server side between the <% %> tags. But on the client side, usually script that is called when a button is clicked, you really want to use JavaScript.

ToddWW
 
Yes Todd, I'm afraid I do need more explanation. I've been reading about this and it hasn't clicked yet. I've tried several scripts, server and client with different syntax with no success. I think I know the difference between client and server-side script, I just need the syntax spelled out for me to be able to put the big picture together. People have been giving me little hints here and there but I'm still trying to put it all together.

I'm using VBScript on the server as my platform.

Thanks.
 
OK, stick with VBScript on the server. That's everything between the <% and %> tags. On the client, use JavaScript. It's widely used and there's a huge forum here on Tek Tips for it. forum216

Here's how a basic form submittal works with JavaScript as the client side script and VBScript as the server side script. I'll give you examples of two pages here.

Page 1 - test1.asp
Code:
<%@ Language=VBScript %>
<% Option Explicit %>
<% Response.Buffer = True %>
<html>
<head>
<script Language=&quot;JavaScript&quot;>
<!--
function checkForm() {
  if(document.form1.first.value == &quot;&quot;) {
    window.alert(&quot;Please provide a first name.&quot;)
    return false
  }
  if(document.form1.last.value == &quot;&quot;) {
    window.alert(&quot;Please provide a last name.&quot;)
    return false
  }
  if(window.confirm(&quot;Are you sure you want to submit ??&quot;)) {
    return true
  } else {
    return false
  }
}
//-->
</script>
</head>
<body>
<form name=&quot;form1&quot; action=&quot;test2.asp&quot; onSubmit=&quot;return checkForm();&quot;>
First Name: <input type=&quot;text&quot; name=&quot;first&quot;><br>
Last Name: <input type=&quot;last&quot; name=&quot;last&quot;><br>
<input type=&quot;submit&quot; value=&quot;Submit&quot;>
</form>
</body>
</html>

Here's the 2nd page.

Page 2 - test2.asp
Code:
<%@ Language=VBScript %>
<% Option Explicit %>
<% Response.Buffer = True %>
<html>
<head>
</head>
<body>
This is the information you submitted.<br>
<b>First Name: </b><%=Request.Form(&quot;first&quot;)%><br>
<b>Last Name: </b><%=Request.Form(&quot;last&quot;)%>
</body>
</html>

Now, just load both of those pages into the same folder on the web server. Study both pages carefully. You'll notice that both pages have the same three lines at the top. You want all of your ASP pages to have these three lines at the top. You'll also notice that the commands on those three lines are surrounded by <% and %> tags. That means that the server is going to try to execute those commands. If any text is not between the <% and %> tags, then the server is going to completely ignore it and just send it down the pipe to the browser. Which is what you will want for about 80% of your contents. Now test1.asp does not have any other server side script other than the first three lines which are really instructions to the server on how to treat the page. The rest of the page is all HTML with a block of JavaScript in between the <head></head> tags. 95% of the time your JavaScript will be between those tags.

In the test1.asp page, there is a basic form. Enter your first name and last name and a submit button. If you study the opening form tag you'll see that we're submitting the information to test2.asp. But notice the onSubmit event handler. This tells the browser to run the JavaScript function checkForm() before submitting and don't submit the form unless that function returns a true response. Now study that function in the block of JavaScript code. You'll see that we're making sure that both text fields have something in them and if not, you get an alert followed by a return false which tells the form not to submit. If the first two conditions test correct, I've given an example of a confirm message box. This will do one thing or another depending on if the user clicks OK or cancel. If the user clicks OK, then the function returns true back to the form. If cancel is pressed, the function returns false back to the form. At any rate, the form will only submit if the function returns true.

Now, look at test2.asp. This page does not have any JavaScript at all. It only has two commands that are surrounded by the <% %> tags. That means that these commands are going to be processed on the server BEFORE the server even sends the document to the page. Go ahead and submit a first and last name. When you get the second page displaying what you've submitted, View the Source. You'll notice that you won't see the Server Side code in the source. All you get is the first and last name. That's because the <%= tag is the equivalant of <%Response.Write method so ASP is actually writing those values to the page before they even leave the server and head for the browser.

Go ahead and play around with it and let me know if you have any other questions. Of course, I can't teach you all about ASP and JavaScript here, but I can point you to three really great books, all available at Amazon.

JavaScript Bible By: Danny Goodman
Mastering ASP By: A. Russell Jones
ASP Developer's Guide By: Greg Buczek

ToddWW
 
Thank you, I'll chew on this. I should be able to put it all together from this.
 
Todd,

Thanks greatly for this info. I'm buying the JavaScript and ASP books now. I'm under the impression that the benefit of using these DTC's in VI is so that you don't have to spend all day coding. With what you're showing me here it looks as if I will have to do hard code my own input boxes and form objects using JavaScript and HTML and do quite a bit of coding to make up for all of the auto-generated code that is created by using the DTC's. So are you implying that in order to have message boxes pop up for my users in their browser I have no choice but to use client-side scripting? Thanks.
 
Oh definitely. The only way to generate a message box or alert box for the visitor is through client side script. And really, the only proper way to evaluate a web page so you know what, when and where to generate that message box is also done with client side script.

Don't worry about the hard coding. Both of those books will have you off and running and writing web pages with client and server side script in a heartbeat. It'll just take a little time and a little bit of a learning curve.

One thing that I learned quickly though was that I read through the ASP book before I even bought the JavaScript book. Thinking back, I did it the opposite. Definitely read the JavaScript book first because the ASP book will only touch on server side validation which is slow and extremely inefficient (in most cases).

ToddWW
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top