Solved

Form in asp page on load

Posted on 2001-06-27
17
196 Views
Last Modified: 2008-03-06
Hi.

Is there any way for me to, like, display a message when an asp page is loaded,and then something else when the user selects something from a drop down box??All help appreciated!

Thanks.
0
Comment
Question by:segafreak
  • 8
  • 6
  • 3
17 Comments
 
LVL 7

Expert Comment

by:weesiong
ID: 6233759
segafreak,

I no sure this is what you want

<%
Response.Buffer = True

'your script.....
%>
<script>
alert('i am message')
</script>
<%
Response.Flush

'continue your script.......
%>

Regards,
Wee Siong
0
 

Author Comment

by:segafreak
ID: 6233761
err,i mean writing a message to the page when the page first loads,something like "Please select a value".the message will be replaced by something from the db once the user selects value from the drop-down box and clicks on submit
0
 
LVL 7

Expert Comment

by:weesiong
ID: 6233763
segfreak,

Ok, try this:

<%
'Your db connection
'Get the message you want put in message
%>
<script>
alert('<%=Rs("message")%>')
</script>
.
.
.

Regards,
Wee Siong
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6233764
Try this

<html>

<head>
<script language="JavaScript">
<!--
function fun1()
{
     alert(document.frmMain.selMain[document.frmMain.selMain.selectedIndex].value + " selected");
}
//-->
</script>
</head>

<body onLoad="alert('Page Loaded')">
<form name="frmMain">
<select name="selMain" onChange="fun1()">
<option selected value="first">first
<option value="second">second
</select>
</form>
</body>

</html>

hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6233766
Oops!! Posted my above comment without refreshing this page. It is not you wanted.

hongjun
0
 
LVL 33

Accepted Solution

by:
hongjun earned 15 total points
ID: 6233775
Are you submitting to the same page or to another page? I assume you are using method="post". If you are using "GET" use Request.QueryString instead of Request.Form in the below code.


If another page,
Try this and place it at the top of the code page.

<%@language=VBScript%>

<%
if request.form("your_select_name") = "" then
    response.write "Please select a value<br>Click back to go back"
else
    response.write request.form("your_select_name") & " selected"
end if
%>


If same page,
Try this and place it at the top of the code page.

<%@language=VBScript%>

<%
if request.form("your_select_name") = "" then
    response.write "Please select a value"
else
    response.write request.form("your_select_name") & " selected"
end if
%>

hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6233776
I think you are trying to do something like validation aren't you? Perhaps you can do client side validation using JavaScript.

hongjun
0
 

Author Comment

by:segafreak
ID: 6233784
erm,let me post the whole code...the thing is that i want a sentence to appear on the page(without using alert).that sentence will be replaced by tables pulled from the db once the user selects any non-default value from the drop down box and clicks submit...

<%@ Language=VBScript %>
<% Option Explicit %>
<% Response.Buffer = true %>

<%'enables database connection%>
<!--#include file="DBConnection.asp"-->

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Course Outlines & Information</title>
</head>

<body bgcolor="#337066">

<p align="center">&nbsp;</p>
<p align="center"><font size="5" color="#FFFFFF">Course Outlines &amp; Information</font></p>
<hr size=1 width="50%">
<div align="center">
<%
      Dim SQL_b, rs1, MyConn
     
      OpenConnection MyConn
     Set rs1=Server.CreateObject("ADODB.recordset")
     SQL_b = " select distinct batch from moduleInfo "
     rs1.Open SQL_b,MyConn
     
%>
<form name = "batchName" action="g_courseOutlinesInfo.asp" method="POST">
<select name="batchCode">
<option selected value="selBatch">Select Batch</option>
<%
     Do Until rs1.EOF
%>
<option value="<%=rs1.fields("batch")%>"><%=rs1.fields("batch")%></option>
<%
     rs1.movenext
     Loop
     rs1.Close
     Set rs1 = Nothing
%>
</select>
<br>
<%Response.write "<p>Please select a batch</p>" %>
<p><br>
<a href="javascript:document.batchName.submit();"><img name="image1" border="0" src="images/view.gif"></a>
<%
     Dim batchCode, SQL_b1, rs2, MyConn1,course
     batchCode = Request.form("batchCode")
%>
<%
     If Request.form("batchCode") = "selBatch" then
%>
     <p align = "center">Please select a batch</p>

     <%Else%>

     <p align="center"><b><u><%=batchCode%></b></u></p>

//an eg of the table to replace the initial onload message    
<table width="631">
     
//this line shows up once the table is loaded...i want it to load only when the user selects something and submits...
<tr><td width="305" align="center"><u>Year One Semester One Core Modules</u></td><td width="4"></td><td width="302"></td></tr>
     <%
          OpenConnection MyConn1
          Set rs2=Server.CreateObject("ADODB.recordset")
          SQL_b1 = " select * from moduleInfo where batch = '"&batchCode&"' and year = '1' and semester = '1' "
          rs2.Open SQL_b1,MyConn1
          Do Until rs2.EOF
     %>
     <tr><td width="305" align="center"><%=rs2.fields("moduleCode")%></td><td width="4">:</td><td width="302"><%=rs2.fields("moduleName")%></td></tr>
     <%
          rs2.movenext
          loop
          rs2.Close
          Set rs2 = Nothing
     %>
</table>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:segafreak
ID: 6233791
oops I mean,

//this line shows up once the form is loaded...i want it to load only when the user selects something
and submits...
<tr><td width="305" align="center"><u>Year One Semester One Core Modules</u></td><td width="4"></td><td
width="302"></td></tr>
    <%
         OpenConnection MyConn1
         Set rs2=Server.CreateObject("ADODB.recordset")
         SQL_b1 = " select * from moduleInfo where batch = '"&batchCode&"' and year = '1' and semester
= '1' "
         rs2.Open SQL_b1,MyConn1
         Do Until rs2.EOF
    %>
    <tr><td width="305" align="center"><%=rs2.fields("moduleCode")%></td><td width="4">:</td><td width="302"><%=rs2.fields("moduleName")%></td></tr>
    <%
         rs2.movenext
         loop
         rs2.Close
         Set rs2 = Nothing
    %>
</table>
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6233798
form name = "batchName" action="g_courseOutlinesInfo.asp" method="POST">

Is g_courseOutlinesInfo.asp another page or same page?

hongjun
0
 

Author Comment

by:segafreak
ID: 6233799
same page
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6233825
Try and replace the following statements

Replace
If Request.form("batchCode") = "selBatch" then

With
If Request.form("batchCode") = "selBatch" or Request.Form("barchCode") = "" then


I don't quite understand your this comment
//this line shows up once the form is loaded...i want it to load only when the user selects something

and submits...


Do you mean you want a alert message when user selected something and he submit? If yes then try this

<script language="JavaScript">
<!--
function do_function
{
    if ( document.batchName.batchCode.value != "selBatch" )
    {
        alert(document.batchName.batchCode[document.batchName.batchCode.selectedIndex].value + " selected")
    }
}
//-->
</script>

<form name = "batchName" action="g_courseOutlinesInfo.asp" method="POST" onSubmit="do_function();return true;">



hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6233831
I made a mistake above. Replace with this

<script language="JavaScript">
<!--
function do_function()
{
   if ( document.batchName.batchCode.value != "selBatch" )
   {
       alert(document.batchName.batchCode[document.batchName.batchCode.selectedIndex].value + " selected")
   }
}
//-->
</script>

hongjun
0
 

Author Comment

by:segafreak
ID: 6233832
<%
     If Request.form("batchCode") = "selBatch" then
%>
     <p align = "center">Please select a batch</p>
     
     <%
          ElseIf Request.form("batchCode") = "" then
     %>
     <p align = "center">Please select a batch</p>

     <%Else%>
         //tables

this was what I tried and found to be successful.I modified it off this :

if request.form("your_select_name") = "" then
   response.write "Please select a value"

Thanks man.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6233834
You can actually do a client-side validation to make sure user selects something before it can be submitted. Then you won't even need a server-side validation.

<script language="JavaScript">
<!--
function do_function()
{
   if ( document.batchName.batchCode.value != "selBatch" )
       return true;
   else
       return false;
}
//-->
</script>

<form name = "batchName" action="g_courseOutlinesInfo.asp" method="POST" onSubmit="do_function()">

hongjun
0
 
LVL 7

Expert Comment

by:weesiong
ID: 6233843
Singapore Poly Vs NanYang Poly

Me is what Poly :)
0
 

Author Comment

by:segafreak
ID: 6233858
huh???
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

947 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

19 Experts available now in Live!

Get 1:1 Help Now