if statement syntax?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roy Adams

    #1

    if statement syntax?

    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="JA VASCRIPT" CODEPAGE="1252" %>

    <%

    var ProductID =1;
    var colorForm = String(Request. QueryString("co lor"));//get the form
    field and put into var


    if(colorForm != "undefined" || colorForm != " " ){

    var text = "" ;
    colorForm = colorForm.repla ce(/'/g, "''");
    colorForm_array = colorForm.split (",");//split at ","


    for( i=0 ; i < colorForm_array .length ; i ++){// loop through the array
    if (colorForm_arra y[i] > " " ) {// if there is something in the array

    conn = Server.CreateOb ject('ADODB.Com mand');//make the sql connection
    object and open it here
    conn.ActiveConn ection = "dsn=Blenz; ";
    conn.CommandTex t = ("insert into color (color,ProductI D) values ('" +
    colorForm_array[i] + "','" + ProductID + "')" ) //insert into table
    field/s
    conn.Execute();//do the job
    conn.ActiveConn ection.Close();//close

    }
    }
    Response.Redire ct("coloursOn.a sp?ProductID=" +ProductID) ;
    }

    else
    {
    Response.Redire ct("insert_done .asp?ProductID= " +ProductID)


    }
  • Richard Cornford

    #2
    Re: if statement syntax?

    Roy Adams wrote:
    <snip>[color=blue]
    > if(colorForm != "undefined" || colorForm != " " ){[/color]
    <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.


    Comment

    • Lee

      #3
      Re: if statement syntax?

      Roy Adams said:[color=blue]
      >
      >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[/color]
      [color=blue]
      >if(colorForm != "undefined" || colorForm != " " ){[/color]

      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.

      Comment

      • Roy

        #4
        Re: if statement syntax?

        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!

        Comment

        Working...