if statement syntax?

R

Roy Adams

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 > " " ) {// 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 + "','" + 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)


}
 
L

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.
 
R

Richard Cornford

Roy Adams wrote:
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.
 
R

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top