In part one of this two part article we took a look at a simple way to create a fully functional browser-based HTML editor. In this article Mitchell shows us how to put it to good use, creating a database driven news system using the WYSIWYG editor, ASP and SQL Server 2000.
Building a WYSIWYG HTML Editor Part 2/2 - Implementing the WYSIWYG HTML editor (contd.) (Page 4 of 6 )
I've also included the following code at the top of the page:
<!-- #INCLUDE file="db.asp" --> <% dim objConn dim objRS set objConn = Server.CreateObject("ADODB.Connection") set objRS = Server.CreateObject("ADODB.Recordset") objConn.Open dbConnString objRS.ActiveConnection = objConn %>
Our newsposts.asp file now contains all of the fields we need for our news posts: title, author, topic(s) and content. It looks like this:
The <form> element that we added to newsposts.asp looks like this: <form onSubmit="return ProcessNews()" name="frmNews" action="newsposts.asp" method="post">
As you can see, the action attribute means that all data from the form will be sent back to the same page, newsposts.asp. We need a way to tell whether or not we should display the news form or actually add the news to our SQL Server database, and the hidden form variable action gives us all of the info we need:
If action is equal to addNews then we add the news to the database. If not, we display the news form…simple. At the top of newsposts.asp, just under the code that connects to our database, we add the following code that separates newsposts.asp into two separate sub-routines: dim action action = Request.Form("action") if action = "addNews" then addNews() else showNewsForm() end if sub showNewsForm() 'HTML Code for news form goes below %> <% end sub sub addNews() dim author dim title dim topics dim newsPost author = Request.Form("author") title = Replace(Request.Form("title"), "'", "''") topics = Request.Form("topics") newsPost = Replace(Request.Form("newsPost"), "'", "''") objConn.Execute "insert into newsPosts(title,topics,newsPost,authorId) values('" & title & "', '" & topics & "','" & newsPost & "'," & author & ")" Response.Write "<b>New news post added OK, here's what you added:<br><br></b>" Response.Write Request.Form("newsPost") objConn.Close set objConn = nothing end sub %>
In the example above I've copy-pasted directly from socket6.com into the WYSIWYG editor, in fact, I could even copy-paste straight from Microsoft Word if I wanted to!