PDA

View Full Version : I know there's something wrong with the syntax...


mangeloni
12-29-2002, 01:46 AM
I am just trying to do a simple if-then-else statement but something is missing...I get a JScript compilation error.

<% if (String(Session("BGMainPhotoC")) != "undefined") then (Response.Write("You have not chosen to have a Caption for your Main Photo")) %>
<% else Session("BGMainPhotoC") end if %>

Basically I want to have a custom message to the user if they have not entered any text, otherwise show the text (based on a session variable) they did enter.

Any help (with all syntax) is appreciated as I am new at writing code.

-MAngeloni

miranda
12-29-2002, 02:43 AM
ok, first off the part != "undefined" means that the string does not contain the string undefined. I take it undefined is the default value. Therefore, this statement would only evaluate to true if indeed the string does not equal undefined. Since I take it you want to give the message if the string does indeed contain undefined or the empty string "" then you should be using ==.

Secondly, since I am used to only using jscript client side and vbscript on my server side apps I have to wonder, since when does jscript use the words then or end if in an if statement. normally jscript would work like this
if (String(Session("BGMainPhotoC")) != "undefined") {
Response.write("You have not chosen to have a Caption for your Main Photo");
}else{(Session("BGMainPhotoC")) ;
}


Your if/then looks more like a vbscript statements syntax

mangeloni
12-29-2002, 07:48 AM
Miranda,

Thanks for the help...I got it sorta working except that even if a session variable exists the user's entry doesn't display -- the Response.write displays in both cases!

I even tried to swap things around, which works only as above as well...

<% if (Session("BGMainPhotoC")) { ;
} else (String(Session("BGMainPhotoC")) != "undefined") ; {
Response.write("You have not chosen to include a Caption for your Main Photo.");
} %>

I tried == as well, but get a syntax error...the only way this works (as it does) is with "undefined" or ""

Hmmmm....

-MAngeloni

whammy
12-29-2002, 04:52 PM
I don't usually use javascript server-side either, but perhaps this will work?:

<%
if (!Session("BGMainPhotoC")) {
Response.write("You have not chosen to include a Caption for your Main Photo.");
}
%>

mangeloni
12-30-2002, 01:21 AM
Well that's a half step better...the response.write only appears when there is no session. Except now when there is a session, nothing appears.

-MA

miranda
12-30-2002, 01:38 AM
the statement doesnt have an else clause add an else clause if you want something to display

<%
if (!Session("BGMainPhotoC")) {
Response.write("You have not chosen to include a Caption for your Main Photo.");
} else{ Response.write ("write something here");
}
%>

mangeloni
12-30-2002, 02:09 AM
I do not want to write a second message, I want to display the session variable...it needs to work like this...

Display the session variable (BGMainPhotoC) else if there is no session variable display the response.write message. Or handle this in the opposite way...no session variable displays the response.write message else display the session variable.

This sounds so simple...I can't figure out why this is not working (I have double checked that the session variable is being stored).

-MA

miranda
12-30-2002, 06:35 AM
Well actually yes, you do need to have an else statement. How else do you think the session variable will display? try this it will work if there is a value in the session variable.
<%
if (!Session("BGMainPhotoC")) {
Response.write("You have not chosen to include a Caption for your Main Photo.");
} else{ Response.write (Session("BGMainPhotoC"));
}
%>

mangeloni
12-30-2002, 08:07 AM
:thumbsup:

Miranda~

Thank you so much...it worked!!! I'm obviously new at actually coding things so I really appreciate your patience and continuous help.

~MVA

whammy
12-31-2002, 12:54 AM
I'm glad that worked for you! :)

P.S. Sorry I didn't explain further (I didn't include the "else" example :(), I thought you were more familiar with programming in javascript, since you're using it server-side. If you're not, I'd suggest sticking with VBScript for ASP server-side usage for the time being. :)

In javascript, the ! means "NOT"... so:

if(!MaryHadALittleLamb) {
// This isn't the Mary we know and love!
}
else {
// She had a little lamb!
}


However, these are basic programming fundamentals you need to learn in order to program applications, at least regarding client-side javascript or any other language with similar C-Based syntax. I would check out this tutorial to start with:

http://hotwired.lycos.com/webmonkey/programming/javascript/tutorials/tutorial1.html

This will teach you the basics of client-side javascript programming, and it seems to me you understand programming concepts logically - however if you're using ASP, you will be better off using VBScript in the meantime for your server-side programming, since references to server-side javascript and ASP are very hard to find. Not to mention, if you frequent this forum regularly, there's absolutely NO way you can avoid learning something from the gurus here like jkd, beetle, etc....

One book that's continually mentioned here (and it's the one I learned from originally, so I know it will get you started) is "Beginning ASP 3.0" by Wrox.com.

For really wonderful quick references for almost ANYTHING, you really must bookmark this site:

http://www.w3schools.com

Hope this helps! :)