PDA

View Full Version : success message on same page where edit


crystal nano
01-10-2010, 10:04 AM
i have code for update news. i have two pages one is in which i edit the news. and afrer submit it will go to update_record.asp page in which it is written that news is updated. i want after pressing submit success message show on top of eeit news page. the code for edit news is as follows

<%@ Language="VBScript" %>
<% Option Explicit %>

<html>
<head><title>News Management System - Update News</title>
</head>
<body>

<%
'declare you variables
Dim SQL, connection, recordset,msg
Dim sConnString, ID 'receive the id of the record passed through querystring
ID=request.querystring("ID")

'Declare the SQL statement that will query the database
SQL="SELECT * FROM latestNews WHERE ID=" & ID

'Create an ADO connection and recordset object
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("news_data.mdb")

'Open the connection to our database
Connection.Open(sConnString)

'Open the recordset object, execute the SQL statement
'and return the record with the id that was sent
recordset.Open SQL,connection
%>

<div align='center'>
<table width="370" border="0" cellspacing="0" cellpadding="0" align="center">
<tr align="left" valign="top">
<td>
<div align="center">
<p>&nbsp;</p>
<!-- start the html form -->
<form action="update_record.asp" method="post" name="form" id="frm" runat="server">
<table border="2" bordercolor="#CCCCFF" cellspacing="0" cellpadding="4">
<tr>
<td>
News Title</td>
<td>
<input id="txtAlpha" type="text" name="newstitle" value="<%= recordset("newstitle")%>" size="50"
class="cssborder" onkeyup="keyUP(event.keyCode)" onkeydown="return isAlpha(event.keyCode);"
onpaste="return false;" />
</td>
<td>
<span id="lblAlpha" style="color: Red; font-size: 10pt; font-family: Arial; visibility: hidden;">
* Only Alphabets</span>
</td>
</tr>
<tr>
<td valign="top">
News Text</td>
<td>
<textarea cols="50" rows="8" name="newsbody" class="cssborder"><% response.write recordset("newsbody") %></textarea></td>
</tr>
<tr>
<td>
News Date</td>
<td>
<input type="text" name="newsdate" value="<% response.write recordset("newsdate") %>" size="12" class="cssborder"></td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="id" value="<%=id%>">
<input type="hidden" name="ispostback" value="1">
<input type="submit" value="update" class="cssborder" />
<input type="reset" class="cssborder">
</td>
</tr>
</table>
</form>
</div>

<%
'close the connection and recordset objects
recordset.Close
Set recordset=Nothing
connection.Close
Set connection=Nothing
%>
</body>
</html>

the after submit edit news it will go to update_record.asp page the code is:

<%@ Language="VBScript" %>
<% Option Explicit %>

<html>
<head><title>News Management System - Updated News</title>
</head>
<body>

<%
'declare your variables
Dim SQL, connection, recordset, ID
Dim sConnString, newstitle, newsbody, newsdate

newstitle = request.form("newstitle")
newsbody = request.form("newsbody")
newsdate = request.form("newsdate")
ID=request.form("ID") 'receive the hidden form ID

'build the SQL - MAKE SURE YOUR CODE IS ON SAME LINE
SQL = "UPDATE latestNews SET newstitle='" & newstitle & "', newsbody='" & newsbody & "', newsdate='" & newsdate & "' WHERE ID=" & ID

'Create an ADO connection and recordset object
Set connection=Server.CreateObject("ADODB.Connection")

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("news_data.mdb")

'Open the connection to our database
Connection.Open(sConnString)

'Execute the SQL
Connection.execute(SQL)

'Close the resources and free up space
connection.Close
Set connection=Nothing

Response.write "<div align='center'>Record Updated.</div>"
%>
</body>
</html>

wht change in this code so that the success message display on edit news page??

Old Pedant
01-10-2010, 08:23 PM
I think you are making a mistake having two separate pages, if you want the "Record Updated" message to appear at the top of the edit page.

I would just combine the two into a single page.

That's what ASP.NET does, for example, and what most of the JSP frameworks do.

Heck, you are already set up to do that! You even have a hidden "ispostback" form field!!

Why did you stop there???

<%@ Language="VBScript" %>
<% Option Explicit %>

<html>
<head><title>News Management System - Update News</title>
</head>
<body>

<%
'declare your variables
Dim SQL, connection, recordset, ID
Dim sConnString, newstitle, newsbody, newsdate

' You always need connection, so create and open it first:
'Create an ADO connection and recordset object
Set connection=Server.CreateObject("ADODB.Connection")

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("news_data.mdb")

'Open the connection to our database
Connection.Open(sConnString)

'*******************
' Is this a POSTBACK???
' That is, do we need to UPDATE the record in the DB??
'
If Trim(Request.Form("ispostback")) = "1" Then

newstitle = request.form("newstitle")
newsbody = request.form("newsbody")
newsdate = request.form("newsdate")
ID=request.form("ID") 'receive the hidden form ID

'build the SQL - MAKE SURE YOUR CODE IS ON SAME LINE
SQL = "UPDATE latestNews " _
& " SET newstitle='" & newstitle & "', " _
& " newsbody='" & newsbody & "'," _
& " newsdate='" & newsdate & "' " _
& " WHERE ID=" & ID

'Execute the SQL
Connection.execute(SQL)

Response.write "<div align='center'>Record Updated.</div>"

' done handling postback

Else
' If not a postback, then the ID must come from querystring:
ID=request.querystring("ID")
End If

Dim msg

'Declare the SQL statement that will query the database
SQL="SELECT * FROM latestNews WHERE ID=" & ID

' More efficient way to get the recordset:
Set Recordset = connecction.Execute(SQL)
%>

<div align='center'>
<table width="370" border="0" cellspacing="0" cellpadding="0" align="center">
<tr align="left" valign="top">
<td>
<div align="center">
<p>&nbsp;</p>
<!-- start the html form -->
<!-- forms do not HAVE a "runat" property...that's only for ASP.NET -->
<form action="update_record.asp" method="post" name="form" id="frm">

<table border="2" bordercolor="#CCCCFF" cellspacing="0" cellpadding="4">
<tr>
<td>News Title</td>
<td>
<input id="txtAlpha" type="text" name="newstitle" value="<%= recordset("newstitle")%>" size="50"
class="cssborder" onkeyup="keyUP(event.keyCode)" onkeydown="return isAlpha(event.keyCode);"
onpaste="return false;" />
</td>
<td>
<span id="lblAlpha" style="color: Red; font-size: 10pt; font-family: Arial; visibility: hidden;">
* Only Alphabets</span>
</td>
</tr>
<tr>
<td valign="top">
News Text</td>
<td>
<textarea cols="50" rows="8" name="newsbody" class="cssborder"><% response.write recordset("newsbody") %></textarea></td>
</tr>
<tr>
<td>
News Date</td>
<td>
<input type="text" name="newsdate" value="<% response.write recordset("newsdate") %>" size="12" class="cssborder"></td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="id" value="<%=id%>">
<input type="hidden" name="ispostback" value="1">
<input type="submit" value="update" class="cssborder" />
<input type="reset" class="cssborder">
</td>
</tr>
</table>
</form>
</div>

<%
'close the connection and recordset objects
recordset.Close
Set recordset=Nothing
connection.Close
Set connection=Nothing
%>
</body>
</html>

crystal nano
01-21-2010, 01:39 PM
thanx for help. its work fine. again another problem. i have another two pages for delete news. one is dell_all.asp the code is as fallows

<%@ Language="VBScript" %>
<% Option Explicit %>

<html>
<head><title>News Management System - Delete News</title>
</head>
<body>
<!--#include file="admin_navigation.asp" -->
<%
'declare your variables
Dim Connection, Recordset, sConnString
Dim SQL

'Create ADO connection and recordset object
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("news_data.mdb")

'Open the connection to the database
Connection.Open(sConnString)

'declare SQL statement that will query the database
SQL ="SELECT * FROM latestNews"


'Open the recordset object, execute SQL statement & return the records
Recordset.Open SQL , connection

'Check to see if there are records with the End of File (EOF) property

If recordset.Eof Then
Response.write "<div align='center'>Sorry there are no current records.</div>"
Else
'loop through the records until we reach the end
Do while not recordset.Eof
%>
<TABLE border=1 bordercolorlight="green" bordercolordark="#808080" height=20% width=70% align="center">
<tr>
<td><b><%= recordset("newstitle")%></b> Date: <%=recordset("newsdate")%><tr><td><%=recordset("newsbody")%><br>
<a href="delete_record.asp?id=<%= recordset("ID") %>">delete record</a></td></tr>
<%
'move on to the next record
recordset.movenext
Loop
End If

'Now that we have displayed the table data lets close the connection
'and the recordset
recordset.Close
Set recordset = Nothing
connection.Close
Set connection = Nothing
%>
</body>
</html>

and after click on delete link it will go to delete_record.asp page where it display that record deleted. code for second page is

<%@ Language="VBScript" %>
<% Option Explicit %>

<html>
<head><title>News Management System - Delete Records</title>
</head>
<body>
<!--#include file="admin_navigation.asp" -->
<%
'declare your variables
Dim Connection, sConnString
Dim SQL, ID

'get the id of the record sent through the querystring of the hyperlink
ID=request.querystring("ID")

'Create an ADO connection object
Set Connection = Server.CreateObject("ADODB.Connection")

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("news_data.mdb")

'Open the connection to the database
Connection.Open(sConnString)
'declare SQL statement that will query the database
'and which will be deleting the record with the id sent
SQL="DELETE FROM latestNews WHERE ID=" & ID

'execute the SQL statement
Connection.execute(SQL)

response.write "<div align='center'>The record was deleted.</div>"

'close the connection
connection.Close
Set connection = Nothing
%>
</body>
</html>

i want that when record is deleted the success message display on del_all.asp page.

what should i do??

Old Pedant
01-21-2010, 07:45 PM
Same thing I did for the update code.

Put it all on one page.

The "trick" is all in that *one* "IF" statement.


'*******************
' Is this a POSTBACK???
' That is, do we need to UPDATE the record in the DB??
'
If Trim(Request.Form("ispostback")) = "1" Then

Do that, or equivalent, on the delete page.