By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,462 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,462 IT Pros & Developers. It's quick & easy.

if statement syntax?

P: n/a
Hello everyone
I'm doing a multiple insert from ten text fields.
all named color
when I submit the from with the text fields
it goes to an asp page with the script to do the job
and the go to another page, I've got that bit going but what if the user
leaves the fields empty an hits submit?
ok a simple enough thing of putting an else in the script, this isn't
working!! it always does the first thing
I've tried so many different variations.
it seems so simple but i just can't get it
here's the full script

<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>

<%

var ProductID =1;
var colorForm = String(Request.QueryString("color"));//get the form
field and put into var
if(colorForm != "undefined" || colorForm != " " ){

var text = "" ;
colorForm = colorForm.replace(/'/g, "''");
colorForm_array = colorForm.split(",");//split at ","
for( i=0 ; i < colorForm_array.length ; i ++){// loop through the array
if (colorForm_array[i] > " " ) {// if there is something in the array

conn = Server.CreateObject('ADODB.Command');//make the sql connection
object and open it here
conn.ActiveConnection = "dsn=Blenz;";
conn.CommandText = ("insert into color (color,ProductID) values ('" +
colorForm_array[i] + "','" + ProductID + "')" ) //insert into table
field/s
conn.Execute();//do the job
conn.ActiveConnection.Close();//close

}
}
Response.Redirect("coloursOn.asp?ProductID=" +ProductID) ;
}

else
{
Response.Redirect("insert_done.asp?ProductID=" +ProductID)
}
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Roy Adams wrote:
<snip>
if(colorForm != "undefined" || colorForm != " " ){

<snip>

You logic is addressing three possibilities: 1. The - colorForm - string
is "undefined", 2. the string is a space character, or 3. the string is
something else.

In reverse order: 3. If the string is something else then the -
colorForm != "undefined" - expression evaluates as true and the body of
the if statement is executed.

2. If the string is a space character - colorForm != "undefined" - also
evaluates as true and the body of the if statement is executed.

1. If the string is "undefined" then - colorForm != "undefined" -
evaluates as false, _but_ you have used a logical OR operator (||) so
the right hand side of that operation is evaluated because the left hand
side is false. So if the string is "undefined" then - colorForm != "
" - is evaluated and that expression returns true, the body of the if
statement is executed.

You don't see the - else - branch being executed because the if
statement is true for all input. You should be using a logical AND
operator (&&) between the two expressions (and learning something about
programming before attempting it as boolean logic is completely
fundamental to the practice).

Richard.
Jul 23 '05 #2

P: n/a
Lee
Roy Adams said:

Hello everyone
I'm doing a multiple insert from ten text fields.
all named color
when I submit the from with the text fields
it goes to an asp page with the script to do the job
and the go to another page, I've got that bit going but what if the user
leaves the fields empty an hits submit?
ok a simple enough thing of putting an else in the script, this isn't
working!! it always does the first thing
I've tried so many different variations.
it seems so simple but i just can't get it
here's the full script if(colorForm != "undefined" || colorForm != " " ){


That reads: if colorForm is not "undefined" or colorForm is not " ".

Unless colorForm is both "undefined" and " " at the same time,
that expression will always be true.

You probably meant "&&" instead of "||".

Note that your second test is testing to see if the value is a single
space, which is not the same as testing to see if it's empty. That
would be "", with no space in between.

Jul 23 '05 #3

P: n/a
Roy
Thanks for your help
Richard but all that's happening now is that all thats being executed is
the else part of the script
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.