?
Solved

Serverside JavaScript

Posted on 2003-11-20
17
Medium Priority
?
959 Views
Last Modified: 2008-02-01
I have an html code which loads my Codes.js script.
I made a script which parse badwords on a this guestbook.

What I wanna do is to be able to save every messages from my visitors.
Is there anyway I could do that on Serverside JavaScript and how do I do it?

//------------------------------------------------------------------

<html>
<head>
<title>::: JGuestBook :::</title>
</head><body>
<script language="javascript" src="Codes.js"></script>
<center><form name=frmx onLoad="Encript.js">
<br><br><br>
<font face="arial" size="3" color="black">
<b>Thank you for visiting !</b></font>
<br><font face="arial" size="2"><br>
Pls. Take a moment to sign this guessbook<br>
Always hoping for your next visit thanks.<br><br>
Name : <input type=field name="fld1" cols="30"><br><br>
<textarea name="txt1" cols="30" rows="8">
</textarea><br>Previous Comments :<br>
<textarea readonly name="txt2" cols="30" rows="8"></textarea><br><br>
<input type="button" name="cmdbtn1" onclick="testMe()" value="Sign the Guestbook">
<input type="button" name="cmdbtn2" onclick="clearMe()" value="Clear Me">
</form></center>
</body>
</html>

--------------------------------------------------------------//

0
Comment
Question by:Cube_X
  • 9
  • 4
14 Comments
 
LVL 11

Expert Comment

by:Zontar
ID: 9792874
What is your server running? JavaScript is supported on the server in ASP, JSP, and SunONE. Note that when I say "JavaScript" I mean the core JavaScript LANGUAGE -- objects relating to the browser are not supported, and a set of serverside objects is supported instead. For instance, in ASP* you can do this:

<%@LANGUAGE="JSCRIPT"%>
<%
  var today = new Date();

  Response.Write("<p>Today is " + today.toLocaleString() + ".</p>");

  today.setDate(today.getDate() + 1);

  Response.Write("<p>Tomorrow is " + today.toLocaleString() + ".</p>");
%>

The Date object is the same -- that's core JS. The method used to echo it to the page is different; there is no Document object in ASP.

Next question: how do you wish to save the messages? In a text file? In a database? Do you also wish to display the messages again, or do you just want to save them for your own use?

Also, this:

<form name=frmx onLoad="Encript.js">

is incorrect. I'm not sure what you're trying to do here but form elements do not have an onload handler, and event handlers take script code as the attribute value, not a filename. What is in the Encript.js file and what are you trying to do with it?

- - - - -
*NOTE: Before anybody thinks to "correct" me, please run this code in a page with an .asp extension on your IIS server. It WILL in fact run; both VBScript and JScript are supported by default in ASP.
0
 

Author Comment

by:Cube_X
ID: 9792931
Zontar;

How do I call that in my html can you change my html codes above, thanks.

>> <form name=frmx onLoad="Encript.js"> = is incorrect, true I forgot to delete it !


0
 
LVL 11

Expert Comment

by:Zontar
ID: 9793020
Please answer my question: What type of server is your site hosted on, and what is available on it? ASP is generally only available on Windows/IIS servers. If you give me the URL, I can probably find out what sort of server it is, and what it's running.

ASP code must be surrounded by <% ... %> delimiters and can only be run in a page with a .asp file extension.

The example I gave has merely the output

Today is 11/21/2003 12:30:21.

Tomorrow is 11/22/2003 12:30:21.

It does not relate to your form -- it's just an example of writing content to the page using ASP with JScript (JavaScript).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Cube_X
ID: 9793164
What type of server is your site hosted on ? It supports asp.

Can you try to work w/ my html so that I could save the comments
from my visitors, thanks.
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9793337
> Next question: how do you wish to save the messages? In a text file? In a database?
> Do you also wish to display the messages again, or do you just want to save them for your own use?

I'm not trying to diddle you around, matey. I wouldn't ask these things if I didn't need to know.
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9793359
Never mind... I'll just throw you together a classic-type thingo when I have some free time a bit later this afternoon, okay?
0
 

Expert Comment

by:Rosewell
ID: 9793465
> Next question: how do you wish to save the messages? In a text file? In a database?
   I want to save it on my second form which has the name of "txt2"

> Do you also wish to display the messages again, or do you just want to save them for your own use?
   Yes I want to display the message again so that if the guest want to go back they would see their message.
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9794311
<%@LANGUAGE="JSCRIPT"%>
<%
  var forReading = 1;
  var forAppending = 8;
  var useDefaultCharset = 0;

  var line, bits, author, date, message, dateFormatted, hours, minutes, amPm;
  var tab = String.fromCharCode(9);

  var fso, ts;

  if(Request.form("cmdbtn1") == "Sign the Guestbook")
  {
    fso = Server.createObject("Scripting.FileSystemObject");
    ts = fso.openTextFile( Server.mapPath("messages.txt"), forAppending, useDefaultCharset);

    author = Request.form("fld1");
    date = new Date().getTime();
    message = escape( Request.form("txt1") );

    ts.writeLine(author + "\t" + date + "\t" + message);

    ts.close();
    fso = null;
  }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Primitives Gästebuch mit ASP und JScript</title>

<style type="">
  body {font:1em Arial,Helvetica,sans-serif; color:#000; background-color:#FFF;}
  h1  {font-size:1.3em;}
</style>
<!--
<script type="text/javascript" src="Codes.js"></script>
-->
</head>
<body>
<div align="center">
<h1>Thank you for visiting !</h1>
<p>Always hoping for your next visit, thanks.<p>
<form name="frmx" action="<% Response.write( Request.serverVariables("SCRIPT_NAME") ); %>" method="POST">
<p>Please take a moment to sign this guestbook.</p>
<p>Name : <input type="text" name="fld1" size="30"></p>
<p><textarea name="txt1" cols="30" rows="8"></textarea></p>
<p><input type="submit" name="cmdbtn1" value="Sign the Guestbook">&nbsp;
<input type="reset" name="cmdbtn2" value="Clear Me"></p>
</form></div>
<h2 align="center">Previous Comments:</h2>
<%
  var output = "";

  fso = Server.createObject("Scripting.FileSystemObject");
  ts = fso.openTextFile( Server.mapPath("messages.txt"), forReading, useDefaultCharset);

  while(!ts.atEndOfStream)
  {
    line = ts.readLine();
    if(line != "" && line.indexOf(tab) != -1)
    {
      bits = line.split(tab);

      author = bits[0];

      date = new Date(bits[1] - 0);
      dateFormatted = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();

      hours = date.getHours();
      if(hours < 10)
        hours = "0" + hours;

      minutes = date.getMinutes();
      if(minutes < 10)
        minutes = "0" + minutes;

      dateFormatted += " " + hours + ":" + minutes;

      message = unescape(bits[2]);

      output += "<tr><td colspan=\"3\"><hr noshade></td></tr>\n";
      output += "<tr><td align=\"center\">" + author + "</td>";
      output += "<td align=\"center\">" + dateFormatted + " " + "</td>";
      output += "<td>" + message + "</td></tr>\n";
    }
  }

  ts.close();
  fso = null;

  if(output != "")
  {
%>
  <table width="75%" align="center" cellpadding="5" cellspacing="5" border="0">
    <tr>
      <th>Name:</th>
      <th>Date:</th>
      <th>Message:</th>
    </tr>
<%
  Response.write(output);
%>
  </table>
<%
  }
  else
  {
%>
  <p>No messages yet... wouldn't you like to be the first to post one?</p>
<%
  }
%>
</body>
</html>

You'll need an empty file named messages.txt in the same directory as this one to start out. The mssages will be saved in messages.txt.
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9794339
Mod,

Yes, I quoted myself earlier. I live in Australia and DMCA doesn't apply here.
0
 

Author Comment

by:Cube_X
ID: 9794846
Dear Mod;

Rosewell Account is from my brother I told him to type my comment but I did not know that he
Changed the name, sorry if it happend this way but anyways its to seperate accounts.
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9794866
Pasha,

No worries -- just a long, hot day here.

Cube_X,

I need to see a copy of your Code.js file so I know where to hook in the clientside script. Ta.
0
 

Author Comment

by:Cube_X
ID: 9794883
Here's my Code.js :

 function testMe() {

   var x1 = document.frmx.fld1.value
   var x2 = document.frmx.txt1.value
   var x3 = new Array(5)

   x3[0] = /b.{0,5}i.{0,5}t.{0,5}c.{0,5}h/gi;
   x3[1] = /f.{0,5}u.{0,5}c.{0,5}k/gi;
   x3[2] = /s.{0,5}h.{0,5}i.{0,5}t/gi;
   x3[3] = /g.{0,5}a.{0,5}g.{0,5}o/gi;
   x3[4] = /a.{0,5}s.{0,5}s./gi;
 
   msg1 = "No name not allowed ! "
   msg2 = "No comments not allowed ! "
   msg3 = "Bad words not allowed ! "

   var d = new Date()
   var m = d.getMonth()
   var y = d.getDay()
   var f = d.getFullYear()
   var h = d.getHours()
   var n = d.getMinutes()
   var s = d.getSeconds()

    if(x1 == "")
       {
        alert(msg1 + "Sorry . . .");
       }


    else if(x2 == "")
            {
             alert(msg2 + "Sorry . . .");
            }

    for(i=0; i<5; i++) {

    if(x2.match(x3[i]))  
       {
        alert(msg3 + "Sorry . . .");

        ShakeBrowser();

        document.frmx.txt1.value = ""
        document.frmx.fld1.value = ""
        x2 = ""
       }  

       else if(x1.match(x3[i]))  
               {          
                alert(msg3 + "Sorry . . .");

                ShakeBrowser();

                document.frmx.txt1.value = ""
                document.frmx.fld1.value = ""
                x2 = "" 
               }
    }

    if(x1 != "" && x2 != "")  
       {
        document.frmx.txt2.value += ""+m+"/"+y+"/"+f+" "+h+":"+n+":"+s+"\n"+x1+" > "+x2+"\n\n"
       }
 }

 function clearMe() {

    document.frmx.fld1.value = ""
    document.frmx.txt1.value = ""
    document.frmx.txt2.value = ""
     
 }

 function ShakeBrowser()
 {
      var rector= 24;
      if (window.moveBy)
      {
            //shake the browser's screen according to the rector scale!
            for (var i=0;i,i<30;i++)
            {
                  window.moveBy(0,rector);
                  window.moveBy(rector,0);
                  window.moveBy(0,-rector);
                  window.moveBy(-rector,0);
            }
      }
 }
0
 
LVL 11

Accepted Solution

by:
Zontar earned 120 total points
ID: 9795204
Okay, you don't need a separate clear function, using a reset button or the form's reset() method takes care of that automatically.

You also don't need to worry about the date -- I ttok care of that on the server.

Again, you'll need to upload an blank textfile named "messages.txt" to the server -- make surethat it's in the same directory as this one. Don't forget to name *this* page with a .asp extension.

Here ya go, bucko... Just add water, makes its own gravy.

<%@LANGUAGE="JSCRIPT"%>
<%
  var forReading = 1;
  var forAppending = 8;
  var useDefaultCharset = 0;

  var line, bits, author, date, message, dateFormatted, hours, minutes, amPm;
  var tab = String.fromCharCode(9), errmsg = "";
 
 
  var x3 = [
            /b.{0,5}i.{0,5}t.{0,5}c.{0,5}h/gi,
            /f.{0,5}u.{0,5}c.{0,5}k/gi,
            /s.{0,5}h.{0,5}i.{0,5}t/gi,
            /g.{0,5}a.{0,5}g.{0,5}o/gi,
            /a.{0,5}s.{0,5}s./gi,
            /m.{0,5}o.{0,5}n.{0,5}g.{0,5}c.{0,5}h.{0,5}o.{0,5}p.{0,5}/gi
           ];

  var fso, ts;

  if(Request.form("cmdbtn1") == "Sign the Guestbook")
  {

    author = Request.form("fld1");
    date = new Date().getTime();
    message = Request.form("txt1");
   
    if(!(/\S/).test(author) || !(/\S/).test(message))
      errmsg ="<p>Please enter your name and a message to post in the guestbook.</p>";
    else
    {
      for(var i = 0; i < x3.length; i++)
      {
        if(x3[i].test(author) || x3[i].test(message))
        {
          errmsg = "<p>Keep a civil tongue in your head there, matey.</p>";
          break;
        }
      }
    }
   
   
    if(errmsg == "")
    {
      fso = Server.createObject("Scripting.FileSystemObject");
      ts = fso.openTextFile( Server.mapPath("messages.txt"), forAppending, useDefaultCharset);
 
      ts.writeLine(author + "\t" + date + "\t" + escape(message));
 
      ts.close();
      fso = null;
   
      errmsg = "<p>Thanks for posting in our guestbook.</p>";
    }
  }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Primitives Gästebuch mit ASP und JScript</title>

<style type="">
  body {font:1em Arial,Helvetica,sans-serif; color:#000; background-color:#FFF;}
  h1  {font-size:1.3em;}
</style>
<script type="text/javascript">
<!--
function testMe(form)
{
  var returnValue = true;
  var x1 = form.fld1.value;
  var x2 = form.txt1.value;
  var x3 = [
            /b.{0,5}i.{0,5}t.{0,5}c.{0,5}h/gi,
            /f.{0,5}u.{0,5}c.{0,5}k/gi,
            /s.{0,5}h.{0,5}i.{0,5}t/gi,
            /g.{0,5}a.{0,5}g.{0,5}o/gi,
            /a.{0,5}s.{0,5}s./gi,
            /m.{0,5}o.{0,5}n.{0,5}g.{0,5}c.{0,5}h.{0,5}o.{0,5}p.{0,5}/gi
           ];

  msg1 = "No name not allowed ! ";
  msg2 = "No comments not allowed ! ";
  msg3 = "Bad words not allowed ! ";

  if(x1 == "")
  {
    alert(msg1 + "Sorry . . .");
    returnValue = false;
  }
  else if(x2 == "")
  {
    alert(msg2 + "Sorry . . .");
    returnValue = false;
  }

  for(i = 0; i < x3.length; i++)
  {
    if( x1.match(x3[i]) || x2.match(x3[i]) )
    {
      alert(msg3 + "Sorry . . .");

      ShakeBrowser();

      form.reset();
      returnValue = false;
      break;
    }
  }

  return returnValue;
}

function ShakeBrowser()
{
  var rector= 24;
  if(window.moveBy)
  {
    //shake the browser's screen according to the rector scale!
    for(var i=0;i,i<30;i++)
    {
      window.moveBy(0,rector);
      window.moveBy(rector,0);
      window.moveBy(0,-rector);
      window.moveBy(-rector,0);
    }
  }
}
//-->
</script>
</head>
<body>
<div align="center">
<%
  Response.write(errmsg);
%>
<h1>Thank you for visiting !</h1>
<p>Always hoping for your next visit, thanks.<p>
<form name="frmx" action="<% Response.write( Request.serverVariables("SCRIPT_NAME") ); %>" method="POST" onsubmit="return testMe(this);">
<p>Please take a moment to sign this guestbook.</p>
<p>Name : <input type="text" name="fld1" size="30"></p>
<p><textarea name="txt1" cols="30" rows="8"></textarea></p>
<p><input type="submit" name="cmdbtn1" value="Sign the Guestbook">&nbsp;
<input type="reset" name="cmdbtn2" value="Clear Me"></p>
</form></div>
<h2 align="center">Previous Comments:</h2>
<%
  var output = "";

  fso = Server.createObject("Scripting.FileSystemObject");
  ts = fso.openTextFile( Server.mapPath("messages.txt"), forReading, useDefaultCharset);

  while(!ts.atEndOfStream)
  {
    line = ts.readLine();
    if(line != "" && line.indexOf(tab) != -1)
    {
      bits = line.split(tab);

      author = bits[0];

      date = new Date(bits[1] - 0);
      dateFormatted = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();

      hours = date.getHours();
      if(hours < 10)
        hours = "0" + hours;

      minutes = date.getMinutes();
      if(minutes < 10)
        minutes = "0" + minutes;

      dateFormatted += " " + hours + ":" + minutes;

      message = unescape(bits[2]);

      output += "<tr><td colspan=\"3\"><hr noshade></td></tr>\n";
      output += "<tr><td align=\"center\">" + author + "</td>";
      output += "<td align=\"center\">" + dateFormatted + " " + "</td>";
      output += "<td>" + message + "</td></tr>\n";
    }
  }

  ts.close();
  fso = null;

  if(output != "")
  {
%>
  <table width="75%" align="center" cellpadding="5" cellspacing="5" border="0">
    <tr>
      <th>Name:</th>
      <th>Date:</th>
      <th>Message:</th>
    </tr>
<%
  Response.write(output);
%>
  </table>
<%
  }
  else
  {
%>
  <p>No messages yet... wouldn't you like to be the first to post one? ;^)</p>
<%
  }
%>
</body>
</html>
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9814027
Oh my... Well, thanks for the points, Pasha.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

839 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