Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Jun 2009
    Location
    Midlands
    Posts
    135

    Unanswered: Form validation inserts blank fields into database-why oh why?

    Hello all,

    I have a HTML form and use ASP for form validation, but for some reason the data that is being written to the database is blank? Just wondering what the possible reasons could be for this?

    Im sending the the action attribute to to a 'validate.asp' page, like so:

    Code:
    <form name="form1" method="post" action="validate.asp" onsubmit="return validate(this);" id="form">
    In the validate.asp page i have the following (im only sending one field by way of testing):

    Code:
     <!-- #include file="incCon.asp" -->
    
    <%
    'Variables
    Dim formName
    
    'Set variable
    formName = request.form("firstname")
     %>
    
     <%
    IF formName <> "" Then
    str = "EXEC spNewStarter " & "'" & formName & "'"
    CON.Execute(str)
        ELSE 
       response.write "Please click back and fill in the form again"
    End IF
      %>
    As i say, it write to the database, but is it blank - the name joe bloggs (for example) doesnt come up, but a blank field does

    Im not missing anything am i?

    Kind regards
    MG

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Chances are formName is null
    Code:
    If IsNull(formName) Or formName = "" Then
      Response.Write "blank"
    Else
      Response.Write "not-so-blank"
    End If
    George
    Home | Blog

  3. #3
    Join Date
    Jun 2009
    Location
    Midlands
    Posts
    135
    Hi gvee, thank you for your reply.

    I copied what you wrote and pasted it into my code, the reply returned "blank".

    Does this mean there is a problem with the way the database has been set up or that there is something wrong with the HTML form it self?

    Regards
    MG

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    The problem is in a couple of places:

    1) Your ASP logic is incorrect. If formName is null then you are executing your sproc, because null <> ""!

    2) Your database allows blank strings to be entered to this field! Add a check constraint to the table to dissallow this behaviour.
    George
    Home | Blog

  5. #5
    Join Date
    Jun 2009
    Location
    Midlands
    Posts
    135
    Hi, thanks for the reply.

    Sorry Im still struggling with ASP. I thought, incorrectly it now seems, that I wrote if 'formName is not equal to blank then execute the sproc?' I am mistaken.

    The database has been set up so as not to does not Allow Nulls. is this what you mean by setting up a constraint?

    Apologies for the lack of knowledge, im still learning. Your help is greatly appreciated.

    Regards
    MG

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I think you're confusing null and a blank string - these are not equivalent.

    consider the following example:
    Code:
    Dim a
    Dim b
    
    a = ""
    We can clearly state that the value of a is a blank string, but what about b?



    As for the database constraint; if you don't want the value of a column to be a blank string then youhave to apply the necessary constraint (i.e. col <> "")
    George
    Home | Blog

  7. #7
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    To extend the ASP example, take a look at this code and see what is returned.
    Code:
    Dim a
    Dim b
    
    a = ""
    
    If a = "" Then
      Response.Write "a is blank string<br />"
    End If
    
    If b = "" Then
      Response.Write "b is blank string<br />"
    End If
    
    If a = b Then
      Response.Write "a equals b<br />"
    End If
    
    'This one might throw you
    If b = b Then
      Response.Write "b equals b<br />"
    End If
    George
    Home | Blog

  8. #8
    Join Date
    Jun 2009
    Location
    Midlands
    Posts
    135
    Hi gvee,

    I think I may be. I understood Null to an unknown, but I think i am checking for "nothing" right?

    No, i don't want a blank string. so I guess I need to set something up in the VBScript, when checking to make sure its not a blank string? And not something that needs to be set up in the Database its' self?

    Thank you for your help
    Regards
    MG


    Quote Originally Posted by gvee View Post
    I think you're confusing null and a blank string - these are not equivalent.

    consider the following example:
    Code:
    Dim a
    Dim b
    
    a = ""
    We can clearly state that the value of a is a blank string, but what about b?



    As for the database constraint; if you don't want the value of a column to be a blank string then youhave to apply the necessary constraint (i.e. col <> "")

  9. #9
    Join Date
    Jun 2009
    Location
    Midlands
    Posts
    135
    You're right , the last one does throw me.

    Im not sure but, is it because 'b' has not been set and will always equal nothing ?

    Also, am i write in thinking 'a' and 'b' both equal nothing?

    Regards
    MG

  10. #10
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Consider this.

    You have 3 boxes, labelled A, B & C.

    You take the lid off box A, put a rubber duck in and shut the lid.
    You take the lid off box B, don't put anything in and shut the lid.
    You do not open box C.

    You know the object is in box A, this is your known value, e.g. a string "rubber duck"
    You know that box B has nothing in. This too is a known value, e.g. ""
    You do not know if box C contains anything at all - it could be full or it could be empty. This value is not known. This is your null


    Now imagine we add 3 new boxes in to the equation which have the same set up as before. We will call these A1, B1 & C1 respectively.


    Now can you answer these questions?

    Does A = A1?
    Does A = B1?
    Does A = C1?
    Does B = A1?
    Does B = B1?
    Does B = C1?
    Does C = A1?
    Does C = B1?
    Does C = C1?
    George
    Home | Blog

  11. #11
    Join Date
    Jun 2009
    Location
    Midlands
    Posts
    135
    Hi, thanks for the little example.

    Ok, at the risk of getting it wrong, i think the following:

    Does A = A1?
    ANS: As we know that something is in both A and A1, this would mean they both equal the same, as does A = B1, B = A1 and B = B1? As they are all known values?

    The reason I answered the above is because im assuming it should be answered based on the known value within box A and B, but not the rubber duck in box A, compared to the nothing in box B is this wrong?

    But, C = A1 and C = B1 cannot equal the same as we dont know if anything is in boxes C and C1?

    As for the last one C = C1, im not sure how best to answer it, it does confuse me but, because we don't know if there is something in the box or not, can this equal as correct as they may both contain something, or both be empty? That said one may have something and the other may not? and therefore can be false too.

    Am i right in what i right or completely wrong?

    Kind regards
    MG

    Quote Originally Posted by gvee View Post
    Consider this.

    You have 3 boxes, labelled A, B & C.

    You take the lid off box A, put a rubber duck in and shut the lid.
    You take the lid off box B, don't put anything in and shut the lid.
    You do not open box C.

    You know the object is in box A, this is your known value, e.g. a string "rubber duck"
    You know that box B has nothing in. This too is a known value, e.g. ""
    You do not know if box C contains anything at all - it could be full or it could be empty. This value is not known. This is your null


    Now imagine we add 3 new boxes in to the equation which have the same set up as before. We will call these A1, B1 & C1 respectively.


    Now can you answer these questions?

    Does A = A1?
    Does A = B1?
    Does A = C1?
    Does B = A1?
    Does B = B1?
    Does B = C1?
    Does C = A1?
    Does C = B1?
    Does C = C1?

  12. #12
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Quote Originally Posted by gvee
    Does A = A1?
    Does a rubber duck equal a rubber duck?
    Yes
    Quote Originally Posted by gvee
    Does A = B1?
    Does a box containing a rubber duck equal an empty box?
    No
    Quote Originally Posted by gvee
    Does A = C1?
    Does a box containing a rubber duck equal a box with unknown contents?
    Well, the box could contain a rubber duck, but it could also contain a tin of spam, or it could be empty, so the answer is:
    unknown
    Quote Originally Posted by gvee
    Does B = A1?
    Does an empty box equal a box containing a rubber duck?
    No
    Quote Originally Posted by gvee
    Does B = B1?
    Does an empty box equal an empty box?
    Yes
    Quote Originally Posted by gvee
    Does B = C1?
    Does an empty box equal a box with unknown contents?
    Well it could, or it couldn't, we don't know!
    unknown
    Quote Originally Posted by gvee
    Does C = A1?
    Does a box with unknown contents equal a box containing a rubber duck?
    Well it could, or it couldn't, we don't know!
    unknown
    Quote Originally Posted by gvee
    Does C = B1?
    Does a box with unknown contents equal an empty box?
    Well it could, or it couldn't, we don't know!
    unknown
    Quote Originally Posted by gvee
    Does C = C1?
    Does a box with unknown contents equal a box with unknown contents?
    Well they could both contain the same thing or have completely different things in them, so once again the answer is...
    unknown
    George
    Home | Blog

  13. #13
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    What I've tried to do here is to highlight to you what a null is (an unknown value, not equal to anything) and how it differs from an blank string.


    This is why we do not get a true or false value when comparing things to null; we get another unknown result!


    Luckily, we can ask this question: Is this value unknown? I.e. is this value a null?

    So if you look back at my first post in this thread, I have used a lovely function called IsNull() that performs this check.


    I know we've taken a pretty windy route, but I hope it has explained to you why your intial code wasn't working as you'd expected.

    As always, come back with any questions you have
    George
    Home | Blog

  14. #14
    Join Date
    Jun 2009
    Location
    Midlands
    Posts
    135
    Hi gvee, thats brilliant thank you. It makes more sense now. It almost feels like paper, stone, scissors - with the 'unknown' above all else.

    I think i learn better when things are broken down like that.

    Thank you for taking the time to explain that, much appreciated dude.

    Also, i have realised why my oringinal code was not working. It was due to the placement of the VBScript that executed the SQL, but it has now been sorted, and a new error has come up:

    Microsoft OLE DB Provider for ODBC Drivers error '80040e07'

    [Microsoft][ODBC SQL Server Driver][SQL Server]Error converting data type varchar to int.

    /aform/V3/default.asp, line 360

    tyring to solve this now.

    Thank you once again.

    Regards
    MG

    Quote Originally Posted by gvee View Post
    What I've tried to do here is to highlight to you what a null is (an unknown value, not equal to anything) and how it differs from an blank string.


    This is why we do not get a true or false value when comparing things to null; we get another unknown result!


    Luckily, we can ask this question: Is this value unknown? I.e. is this value a null?

    So if you look back at my first post in this thread, I have used a lovely function called IsNull() that performs this check.


    I know we've taken a pretty windy route, but I hope it has explained to you why your intial code wasn't working as you'd expected.

    As always, come back with any questions you have

  15. #15
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    The error is being thrown by SQL where it expects an integer and you're passing it some text.

    Remember: '1' is still text (note the quotes!)
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •