If-statement in ASP (simple question)

They have: 133 posts

Joined: Sep 2000

I have a, I think, simple question: How do I verify that a form has been written in?

The form is on 1 page, and is just a "post" then I want to, on the ASP page, to check if the user has written his homepage adress, so that I know if I shall put a picture with a homepage near the text. It is for a guestbook.

If it is at some help, I will show you what I mean, doing it with javascript:

if (document.forms.myform.homepage.value==""){
donnot.write.the.code()
}

I suppose that that can be done easily in ASP

BTW: I am not writing to a database, just to a text file, but that shouldnt matter...

I hope that you can help me, so that I can make the guestbook look better...

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

sure thing.

ASP Form Validation Logic:

<%
HomePage = Request.Form("homepage")

If HomePage = "" Then
picHomePage = false
End If

'Repeat for each desired item
%>
'

print the results:

<%
If picHomePage = true Then
Response.Write "<img src=""homepage.gif"" alt=""Go to their HomePage"">"
Else
Response.Write "&nbsp;"
End If
%>
'

NOTICE the double double quotes? In order to escape a quote in ASP, you double it up. So to print a " you write "" and to get a ' you write ''
I printed a space if there was no image just to demonstrate an 'else' statement, but you can leave that out.

Hope that helps, good luck.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 133 posts

Joined: Sep 2000

I have finally got it up and running, thanks to you! You know, when you write it all you self, you forget a " and then the code wont work... I used a couple of ours, banking my head against the wall, to figure that one out Smiling

I was thinking, that you might be able to help me with this one too:

As you know, it is a questbook, I am working on, and now I want the newest text to be in the top. I know that you can with databases do something like move.next, but how do you do that when I am using a txt filé? It would look way better if the wuestbook had the newest posts in the top, and so on...
I think that it can be done, by doing something like reading the total txt file into the memory, put the new post in the top, and then add the rest underneath. If you can do that, the newest would allways be at the top!
If that isnt posible, maybe you can make the script ready the txt file from the button instead, so that it will write it the right way!?

I hopw that you can ansver this question as well, and I thank for the help I have gotten so far!

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi hotcut,

It is possible, but it depends on how your guestbook is written to begin with. That is, do you have control over how it is written? And, if so, how are you going about it? As one block of text, or as fields with html? If the latter, and you have either a date/timestamp or record number field, you can probably put the entries into an array and then sort on one of those fields before you write out the page.

Vinny

Where the world once stood
the blades of grass cut me still

They have: 133 posts

Joined: Sep 2000

I am in total controll of what is written in the txt file, I am modyfying all the text using javascript, on the page where I have the form, so there will only come the information in the file, that I want!

Below is the code that I am using to write to the txt file:

<?php
Dim filesys
, peoplefile
Set filesys
= CreateObject("Scripting.FileSystemObject")
Set peoplefile = filesys.OpenTextFile("c:\asp\gbpeople.txt", 8, true)
peoplefile.WriteLine Request.Form("name")
peoplefile.WriteLine Request.Form("comment")
peoplefile.WriteLine Request.Form("email")
peoplefile.Writeline Request.Form("homepage")
peoplefile.Writeline Request.Form("icq")
peoplefile.Writeline Request.Form("time")
peoplefile.Close
?>

all of these is written in the txtfile, and when I want to write it on another page, I am reading like thís:

Dim filesys, peoplefile, navn, kommentar, email, emailbillede, tidspunkt, hjemmeside, hp, hjemmesidebillede, icq, icqbillede
Set filesys = CreateObject("Scripting.FileSystemObject")
Set peoplefile = filesys.OpenTextFile("c:\asp\gbpeople.txt", 1)
do while not peoplefile.AtEndOfStream
name = peoplefile.ReadLine
comment = peoplefile.ReadLine
email = peoplefile.ReadLine
homepage = peoplefile.ReadLine
icq = peoplefile.ReadLine
tiime = peoplefile.ReadLine

Then I use the Response.Write to write out all the variables to the document. The problem is that thesés codes writes out the txt file on the page, with the newest one in the top...

Just to show you visually how it is being written in the txt file, I will give you an example below, wich includes two entries:

Casper Bang
This is thefirst entry in the page, I have made all new lines be ""
[email protected]
http://www.casper-bang.com
24774058
6/10-2000 kl.10.20
Casper
This is the second entry, all controlled by the javascript?
[email protected]
http://www.casper-bang.com
24774058
6/10-2000 kl.10.22

The when I write it out on the page it will do it in the "wrong" order, it will write it like this:

Casper
This is the second entry, all controlled by the javascript?
[email protected]
http://www.casper-bang.com
24774058
6/10-2000 kl.10.22
his is thefirst entry in the page, I have made all new lines be ""
[email protected]
http://www.casper-bang.com
24774058
6/10-2000 kl.10.20

Offcourse, I have modyfied it, so that it will look better, but principial, that is how it does!

I suppose that that is all the information you wanted...

I hope that you can help me.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

sorry I didn't reply sooner, I haven't had time to visit the forum sinse my last post here.

Just out of curiosity... Why aren't you using a real database?

Can you give me more info? does it completely reverse the order of the entries? or just put the current one at the bottom (with the rest of the entries in the correct order)?

I'm a bit rusty with using flat file databases with ASP. So if you could provide some code, it would help.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 133 posts

Joined: Sep 2000

OK, you need more information... First of all, my intire script is made, whith danish as the language, so the code I will give, have only danish variables! Anyway, that shouldnt be a big problem...

The page where you enter your information can be found at:
http://www14.brinkster.com/cbang/GB.htm
Youcan weiv the source there, it is normal HTML there!

Then I have 2 asp files! I will give you the INTIRE code for both of them, so please dont distribute it Smiling I have used a lot of hours making that!

The fisrt source is called: gbproc.asp that is the one that writes the code to thetxt file! The code is below:

<?php
Option Explicit
?>

Gæstebog svar
<script language="javascript">
function videresend() {
window.location="gblist.asp"
}
</script>

Navn:

<?php
=Request.Form("navn")
?>

Kommentar:
<?php
=Request.Form("kommentar")
?>

E-mail:
<?php
=Request.Form("email")
?>

Hjemmeside:
<?php
=Request.Form("hjemmeside")
?>

Evt. icqnummer:
<?php
=Request.Form("icq")
?>

tidspunkt:
<?php
=Request.Form("tidspunkt")
?>

<?php
Dim filesys
, peoplefile
Set filesys
= CreateObject("Scripting.FileSystemObject")
Set peoplefile = filesys.OpenTextFile(server.mappath("/cbang/db/gbpeople.txt"), 8, true)
peoplefile.WriteLine Request.Form("navn")
peoplefile.WriteLine Request.Form("kommentar")
peoplefile.WriteLine Request.Form("email")
peoplefile.Writeline Request.Form("hjemmeside")
peoplefile.Writeline Request.Form("icq")
peoplefile.Writeline Request.Form("mobilnummer")
peoplefile.Writeline Request.Form("tidspunkt")
peoplefile.Close
?>

Så er der ingen vej tilbage, den besked bliver indskrevet!
Dine info er gemt.
Tak fordi du skrev i min gæstebog. Du vil nu blive sendt tilbage til gæstebogen om 3 sekunder...

Hvis du ikke vil vente længere, klik da her...

The we have the file called: gblist.asp that is the one that is working with the content of the txt file. The code is below:

<?php
Option Explicit
?>

Casper's gæstebog

- skriv nyt indlæg

<script language="javascript">
function icqpopup(icqnummer) {
var icqnummer=icqnummer
var sted = "icq/icq.htm?icqnummer="+icqnummer+""
popup = window.open(sted)
}
</script>

<?php
Dim filesys
, peoplefile, navn, kommentar, email, emailbillede, tidspunkt, hjemmeside, hp, hjemmesidebillede, icq, icqbillede, mobilnummer
Set filesys
= CreateObject("Scripting.FileSystemObject")
Set peoplefile = filesys.OpenTextFile(server.mappath("/cbang/db/gbpeople.txt"),, 1)
do while
not peoplefile.AtEndOfStream 
  navn
= peoplefile.ReadLine
  kommentar
= peoplefile.ReadLine
  email
= peoplefile.ReadLine
  hjemmeside
= peoplefile.ReadLine
  icq
= peoplefile.ReadLine
  mobilnummer
= peoplefile.Readline
  tidspunkt
= peoplefile.ReadLine

If hjemmeside = "" Then
hjemmesidebillede
= false
else hjemmesidebillede = true
End
If

If
icq = "" Then
icqbillede
= false
else icqbillede = true
End
If

If
email = "" Then
emailbillede
= false
else emailbillede = true
End
If

If
emailbillede = true then
  Response
.Write "<a href='mailto:" & email & "'> <img src='email.gif' border=0></A>&nbsp; &nbsp;"
End If

If
hjemmesidebillede = true Then
Response
.Write "<a href='" & hjemmeside & "'><img src='hjemmeside.gif' border=0></a>&nbsp; &nbsp;"
End If

If
icqbillede = true Then
Response
.Write "<a href='javascript:void(0)'><img src='http://wwp.icq.com/scripts/online.dll?icq=" & icq & "&img=2' onClick='icqpopup(" & icq & ")' border=0></a>"
End If

 
Response.Write "<br></a><font color='Blue' size='+2'><b>" & navn & "<br></font><font size='+2'> skriver den " & tidspunkt & ":</font> </b><br><br><i><font size='+1'>" & kommentar & "</font><br><br><hr></i>"
loop
peoplefile
.Close
?>

That was all the code I have used for making the script... exept the contents of the "icq" folder, you wont need the code for that... That isnt ASP!

Ohh, yea, if you want to write in the guestbook, you can do it freely! If you just writ some "kigkhgfcj" txt, I will just delete it... just write what you want, I dont mind Smiling

Just remember, when you submit ("skriv"), you will have to write something in the "navn" field and in the "kommentar" field! If you dont, it will popup with an alert!

The reason why I aint using a real datbase is,that It would be too hard to start with, and that I like working with TXT files!

I was thinking, maybe it will be posible to just write out the text in the other order, instead of writing it to the txt file in another order?! I dont care how it is done, I will just be happy if it can be done!!!

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I don't know if I can be of much help right now. But heres some ideas.

If you don't want to play memory games, and know the syntax for renaming files. You should do the 'thing' while writing the data to the file.

The syntax is probably wrong, but heres some psuedocode.

<%
Dim filesys, peoplefile
Set filesys = CreateObject("Scripting.FileSystemObject")
Set oldfile = filesys.OpenTextFile(server.mappath("/cbang/db/gbpeople.txt"))

' Print the new stuff
newfile.WriteLine Request.Form("navn")
newfile.WriteLine Request.Form("kommentar")
newfile.WriteLine Request.Form("email")
newfile.Writeline Request.Form("hjemmeside")
newfile.Writeline Request.Form("icq")
newfile.Writeline Request.Form("mobilnummer")
newfile.Writeline Request.Form("tidspunkt")

' Print the old stuff
Set newfile = filesys.OpenTextFile(server.mappath("/cbang/db/gbpeople2.txt"),ForWriting,1)

do while not peoplefile.AtEndOfStream
newfile.WriteLine oldfile.ReadLine 'navn
newfile.WriteLine oldfile.ReadLine 'kommentar
newfile.WriteLine oldfile.ReadLine 'email
newfile.Writeline oldfile.ReadLine 'hjemmeside
newfile.Writeline oldfile.ReadLine 'icq
newfile.Writeline oldfile.ReadLine 'mobilnummer
newfile.Writeline oldfile.ReadLine 'tidspunkt
loop

newfile.Close
oldfile.Close

' I don't know how, but now you will need to
'  make "gbpeople2.txt" to be "gbpeople.txt"
%>
'

If you don't mind playing mind games, you can do your 'thing' before or after. I'll assume you'll do it while reading the final txt file. Simply use the loop to put each line into an array. Reverse the order of the array. Then print the array to the screen. It's important that you know that every 7 lines is a new entry. So, if someone signs your book and leaves a field blank, you need to write a blank line into your txt file [I think your already doing this].

Hope that helps [or at least inspires you].
Good luck!

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 133 posts

Joined: Sep 2000

Actually... I have thought of doing something like this... But I didnt got the knowledge to do so... I hope that I somehow can make it work... But unfortunetly, I have no expirence AT ALL with using arrays! The guestbook script you see abive, is the only ASP script I have made... I gotta start somewhere, you know...

I am not sure if it will be safe, to do like that, becourse if you are unlucky, and two people are signing up at the same time, it will probably ruin the script... But I am now sure with that one...

I am starting to be convinced, that a database will be the best solution... But I do still like working with txt files, you can easily see what you a writing, and how it is all done...

I have never workéd with databases in ASP before, so if you maybe can give me a little hint on how to make it write to a msdatabse instead, But offcourse only if we cant solve the other problem!? I have tried to put it all into a database, but I didnt got all the knowledge to make it work...

I hope that you still have some patience to help me Smiling

Casper

[Edited by hotcut on 10-11-2000 at 10:22 AM]

They have: 133 posts

Joined: Sep 2000

I have again tried to make my script work with a database, and I have sucseeded! Well... at least allmost... It does write to the database, with no problems now, but I still can make it write in the right order...

The code is below:

<?php
dim streng1
, streng2, sti, AccessDataBase, strSQL, rsRecordSet, dbconnection

dim navn
, kommentar, email, hjemmeside, icq, mobilnummer, tidspunkt, hjemmesidebillede, icqbillede, emailbillede
    sti
= "c:\asp\"
    Streng1 = "
PROVIDER=MSDASQL;DRIVER={Microsoft Access Driver (*.mdb)};DBQ="
    Streng2 = "
\gbdatabase.mdb;UID=admin;PWD=;"
    AccessDataBase = Streng1 & sti & Streng2

strSQL = "
SELECT * FROM gbtabel"
Set dbConnection = Server.CreateObject("
ADODB.Connection")
Set rsRecordSet = Server.CreateObject("
ADODB.RecordSet")

dbconnection.Open AccessDataBase
rsRecordSet.Open strSQL, dbConnection


<strong>rsRecordSet.MoveFirst </strong>


while not rsRecordSet.EOF

navn = rsRecordSet("
navn")
kommentar = rsRecordSet("
kommentar")
email = rsRecordSet("
email")
hjemmeside = rsRecordSet("
hjemmeside")
icq = rsRecordSet("
icq")
mobilnummer = rsRecordSet("
mobilnummer")
tidspunkt = rsRecordSet("
tidspunkt")


Response.Write ("
all the things that is needed")
' the code here has been shortened, but only the text that I am writing out have been removed

  <strong> rsRecordSet.MoveNext </strong>
  
wend

rsRecordSet.Close
dbConnection.Close
Set rsRecordSet = Nothing
Set dbConnection = Nothing
?>

The highlighted items is the code, that I believe that should be changed in order to make it go in the right direcktion... I have tried diferent values for them, but I cant seem to do anything, but making a loop, with no end... And now my server is down once in a while, due to me, making too much trafik with that (it is only a PWS server, on my own harddrive)

Anyway I bope that someone, out there, have the ansver to all my questions Smiling

You might wonder, how I learned so quick how to use databases, I used a toturial on: http://www.askasp.com/articles.asp?ArtID=1 where it helped me a little in how to do it... But it wqas one of the worst toturials, I have ever seen; They didnt even tell me how to define the variables!!! Well... A bit og guessing, and a little luck brought me so far Laughing out loud

Casper

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

this is simple.

I don't know what database your using. I work with SQL Server 7 at work, so I'll pull my knowledge from that.

In your database table, create another field. Call it "Id". Make it 'auto_number' and 'not null'. When you insert the info into the database, just ignore this field... it will take care of itself.

When you read from the database, your query should look like this:
"SELECT * FROM gbtabel ORDER BY Id DESC"

This will select everything in the table in reverse order (using our auto_number 'Id' field).

Then simply do the usual:
do while not rs.eof
.......code........
rs.MoveNext
loop

Also... you don't need the rs.MoveFirst. After you execute the SQL query, you'll automatically start at the begining. You only will need to use rs.MoveFirst if you need to loop through your recordset twice. So you'd 'loop while not rs.eof' then 'rs.MoveFirst' and again 'loop while not rs.eof'.

Hope that works!

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 133 posts

Joined: Sep 2000

I think that I can, fro´m the information you gave me, make it work just fine!

I will probably need help with more things later on, but so far, it is sufficient!

A greatfull Casper

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.