Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    UK
    Posts
    302
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Multi Submission

    Hi all, I need a little help.

    I have one form that has the same fields on it twice (I do intended to add more). I am wanting to send the form to a DB. Now, the issue I need help with how do I split the data up into the rows in DB. I know i can use a the following code to pick up the form fields.

    Code:
    For x = 1 To Request.Form.count()
    	Response.Write(Request.Form.key(x) & _		
            "(" & x & ")" & " = ")
            Response.Write(Request.Form.item(x) & _
    	"<br>")
    	Next
    But this has issues alone. Can this be adjusted to allow multi submissions to a DB. Or would an Array be needed?

    The HTML for the form looks like this

    Code:
    <form name="inbound_form" action="save.asp?Action=AddNewStock" method="post">
    <fieldset>
    <legend>:: Add - Inbound Invertory Items</legend>
    <table width="1226">
    <tr>
      <td width="302" class="theader">Provider</td>
      <td width="302" class="theader">Number Type</td>
      <td width="302" class="theader">Number Carrier</td>
      <td width="302" class="theader">Number Function</td>
    </tr>
    <tr>
      <td><input class="textfield" name="Provider" type="text" id="Provider" /></td>
      <td><input class="textfield" name="NumberType" type="text" id="NumberType" /></td>
      <td><input class="textfield" name="NumberCarrier" type="text" id="NumberCarrier" /></td>
      <td><input class="textfield" name="NumberFunction" type="text" id="NumberFunction" /></td>
      </tr>
    <tr>
      <td><input class="textfield" name="Provider" type="text" id="Provider" /></td>
      <td><input class="textfield" name="NumberType" type="text" id="NumberType" /></td>
      <td><input class="textfield" name="NumberCarrier" type="text" id="NumberCarrier" /></td>
      <td><input class="textfield" name="NumberFunction" type="text" id="NumberFunction" /></td>
      </tr>
    <td><input type="submit" name="hidden" id="Save" value="Save &amp; Close" /></td>
      </tr>
    </table>
    </fieldset>
    </form>
    Thanks.
    Kind regards,
    Mike Hughes

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    First of all, your HTML is illegal. ID's *MUST* be UNIQUE on a page. You are repeating the ids as well as the names.

    But there is *ZERO* reason to have IDs when you have names. So just get rid of them.

    *********

    The better way would be to number the rows, thus:
    Code:
    <tr>
      <td><input class="textfield" name="Provider1" type="text" /></td>
      <td><input class="textfield" name="NumberType1" type="text" /></td>
      <td><input class="textfield" name="NumberCarrier1" type="text"  /></td>
      <td><input class="textfield" name="NumberFunction1" type="text" /></td>
    </tr>
    <tr>
      <td><input class="textfield" name="Provider2" type="text" /></td>
      <td><input class="textfield" name="NumberType2" type="text" /></td>
      <td><input class="textfield" name="NumberCarrier2" type="text"  /></td>
      <td><input class="textfield" name="NumberFunction2" type="text" /></td>
    </tr>
    Now that ASP code is easy:
    Code:
    For row = 1 To 999999
        provider = Request("Provider" & row)
        If IsNull(provider) OR IsEmpty(provider) Then Exit For
        numtype = Request("NumberType" & row)
        carrier = Request("NumberCarrier" & row)
        func = Request("NumberFunction" & row)
        ... now do something with those values ...
    Next
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    If there is some reason you can't do that (e.g., you aren't creating the <form>; some other process is), then:
    Code:
    For row = 1 To Request.Form("Provider").count
        provider = Request.Form("Provider")(row)
        numtype = Request.Form("NumberType")(row)
        carrier = Request.Form("NumberCarrier")(row)
        func = Request.Form("NumberFunction")(row)
        ... now do something with those values ...
    Next
    Note that this DOES NOT WORK if any of the form fields are checkboxes or radio buttons. So you *need* to use the numbering scheme, above, if those are involved.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    UK
    Posts
    302
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Thanks for the help. I am still a little confused. I do understand about giving the HTML unique field names.

    But what I am trying to achieve here is to sumbit the "filled out" vaules from my form and save to DB, so How can I do this?

    Am I way off on the below?

    Code:
    For row = 1 To Request.Form("Provider").count
        provider = Request.Form("Provider")(row)
        numtype = Request.Form("NumberType")(row)
        carrier = Request.Form("NumberCarrier")(row)
        func = Request.Form("NumberFunction")(row)
    	 
    Rs4("Provider") = provider
    Rs4("NumberType") = numtype
    Rs4("NumberCarrier") = carrier
    Rs4("NumberFunction") = func
    		
    Next
    Kind regards,
    Mike Hughes

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    You need to show more code.

    Will you *ALWAYS* be inserting into the DB?

    *PROBABLY* you would use something like this:
    Code:
    <%
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.Open "...your connection string..."
    
    Set rsNew = Server.CreateObject("ADODB.Recordset")
    rsNew.Open "yourtablename", conn, 3, 3
    
    For row = 1 To Request.Form("Provider").count
        provider = Request.Form("Provider")(row)
        numtype = Request.Form("NumberType")(row)
        carrier = Request.Form("NumberCarrier")(row)
        func = Request.Form("NumberFunction")(row)
    
        ' you might want to have code here to validate that all those values are okay
    
        rsNew.AddNew ' add a new record
    
        rsNew("Provider") = provider
        rsNew("NumberType") = numtype
        rsNew("NumberCarrier") = carrier
        rsNew("NumberFunction") = func
        
        rsNew.Update ' don't forget this!
    
    		
    Next
    rsNew.Close
    conn.Close
    %>
    But the exact code depends on what kind of database you are using, what connection string you use, etc., etc.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    UK
    Posts
    302
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Thanks again for the pointers. I do understand more now. Okay, the last part thats got me stuck. The HTML form has multiple field names and all with different values, like below i think I going to do this 10 times.

    In this instance it will always go to a DB.

    Code:
    <tr>
      <td><input class="textfield" name="Provider1" type="text" /></td>
      <td><input class="textfield" name="NumberType1" type="text" /></td>
      <td><input class="textfield" name="NumberCarrier1" type="text"  /></td>
      <td><input class="textfield" name="NumberFunction1" type="text" /></td>
    </tr>
    <tr>
      <td><input class="textfield" name="Provider2" type="text" /></td>
      <td><input class="textfield" name="NumberType2" type="text" /></td>
      <td><input class="textfield" name="NumberCarrier2" type="text"  /></td>
      <td><input class="textfield" name="NumberFunction2" type="text" /></td>
    </tr>
    <tr>
      <td><input class="textfield" name="Provider3" type="text" /></td>
      <td><input class="textfield" name="NumberType3" type="text" /></td>
      <td><input class="textfield" name="NumberCarrier3" type="text"  /></td>
      <td><input class="textfield" name="NumberFunction3" type="text" /></td>
    </tr>
    So when using the Rs (FieldName) command it doesn't like this because it cant see the correct name in the DB. So, how can this part be over come? And lastly, I don't want to be submitting null values to the DB, so I guess I need to stop that also.

    If you could give me a few more pointers and if the code is submiting individual records to the DB and should be on my way.

    Thanks in advance, Mike.
    Kind regards,
    Mike Hughes

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    So when using the Rs (FieldName) command it doesn't like this because it cant see the correct name in the DB.
    Ummm...you control that yourself:

    Code:
        rsNew("Provider") = provider
        rsNew("NumberType") = numtype
        rsNew("NumberCarrier") = carrier
        rsNew("NumberFunction") = func
    Notice that the DB field names there DO NOT have the numbers after them. On purpose.

    If those aren't the right DB field names, change them to the right ones.

    As for null or invalid data: I did comment on that. I wrote
    Code:
        ' you might want to have code here to validate that all those values are okay
    But since I don't know WHAT values are "okay" for your usage, I can't very well write that code.

    If all you care about is that the fields are non-blank, then you could do:
    Code:
    For row = 1 To 9999
    
        provider = Trim(Request.Form("Provider" & row))
        If IsNull(provider) OR IsEmpty(provider) Then Exit For
    
        numtype = Trim(Request.Form("NumberType" & row))
        carrier = Trim(Request.Form("NumberCarrier" & row))
        func = Trim(Request.Form("NumberFunction" & row))
    
        ' check that field values are okay
        okay = True
        If provider = "" Then okay = False
        If numtype = "" Then okay = False
        If carrier = "" Then okay = False
        If func = "" Then okay = False
     
        If Not okay Then
            Response.Write "Skipping row " & row & " because of invalid data<br/>"
        Else
    
            rsNew.AddNew ' add a new record
    
            rsNew("Provider") = provider
            rsNew("NumberType") = numtype
            rsNew("NumberCarrier") = carrier
            rsNew("NumberFunction") = func
        
            rsNew.Update ' don't forget this!
    
        End If
    
    Next
    I would *HOPE* you can to better validation than that, but I also hope that will get you started.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Location
    UK
    Posts
    302
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Thanks for your help! I am still stuck. Would you mind helping a little more?

    This were I am

    Code:
        'Dealcare the variables 
    			Dim Conn4, Rs4, NumberProvider, NumberType, NextworkCarrier, NumberStatus, NumberFunction
    			
    			' Open objects
    			Set Rs4 = Server.CreateObject("ADODB.Recordset") 
    			Set Conn4 = Server.CreateObject("ADODB.Connection")
    			Conn4.Open  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("db.mdb")
    			sql4 = "SELECT * FROM InventoryStockInbound;"
    			
    			'SQl Debug (Not really needed in this statment)
    			'Response.Write(sql4)
    			
    			Rs4.CursorType = 2 
    			Rs4.LockType = 3 
    			
    			'Open the recordset with sql query
    			Rs4.Open sql4, Conn4
    			 
    			' 
    			For row = 1 To 16
    			NumberProvider = Trim(Request.Form("NumberProvider" & Row)) 
    			If IsNull(NumberProvider) OR IsEmpty(NumberProvider) Then Exit For
    						
    			NumberProvider = Left(Request.Form("NumberProvider" & Row),14)  
    			NumberType = Left(Request.Form("NumberType" & Row),10)  
    			NextworkCarrier = Left(Request.Form("NextworkCarrier"& Row),15)  
    			NumberStatus = Left( Request.Form("NumberStatus"& Row),12)  
    			NumberFunction =  Left(Request.Form("NumberFunction"& Row),14)  
    			
    			' Check that field values that are null 
    		    okay = True
    		    If NumberProvider = "" Then okay = False
    			If NumberType = "" Then okay = False
    			If NextworkCarrier = "" Then okay = False
    			If NumberStatus = "" Then okay = False
    			If NumberFunction = "" Then okay = False
    
    			If Not okay Then
    			Response.Write ("Blanks fields on "& row &" <br>") 
    			Else
    			
       			 'ADD fields 
    		    Rs4.AddNew 
    			  
    			Rs4("ShowStock") = True 
    			Rs4("FootPrint_AddedDate") = Now 
    			Rs4("NumberProvider") = NumberProvider
    			Rs4("NumberType") = NumberType
    			Rs4("NextworkCarrier") = NextworkCarrier
    			Rs4("NumberStatus") =  NumberStatus
    			Rs4("NumberFunction") = NumberFunction
    			
    			Rs4.Update 
    	
    			End If
    			Next
    			Rs4.Close
    			Set Rs4= Nothing
    			Set Conn4 = Nothing
    %>
    Kind regards,
    Mike Hughes

  • #9
    Regular Coder
    Join Date
    Jul 2002
    Location
    UK
    Posts
    302
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Hi all,

    Can anyone help? From the help above and SQL I worked out I can now multi submit from to my DB.

    However, I can't find a way to stop NULL vaules being entered. The example given to me to stop blanks is not working.

    Code:
    	' Check that field values that are null 
    		    okay = True
    		    If NumberProvider = "" Then okay = False
    			If NumberType = "" Then okay = False
    			If NextworkCarrier = "" Then okay = False
    			If NumberStatus = "" Then okay = False
    			If NumberFunction = "" Then okay = False
    
    			If Not okay Then
    			Response.Write ("Blanks fields on "& row &" <br>") 
    			Else
    This is latest update of code

    Code:
    	' Open objects 
    			Set Conn4 = Server.CreateObject("ADODB.Connection")
    			Conn4.Open  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("db.mdb")
    
    			'For row = 1 Request.Form("NumberProvider").count
    			For row = 1 To 16
    		    NumberProvider = Left(Request.Form("NumberProvider" & row), 14)
        		If IsNull(NumberProvider) OR IsEmpty(NumberProvider) Then Exit For
    			NumberProvider = Left(Request.Form("NumberProvider" & Row),14)  
    			NumberType = Left(Request.Form("NumberType" & Row),10)  
    			NetworkCarrier = Left(Request.Form("NetworkCarrier"& Row),15)  
    			NumberStatus = Left(Request.Form("NumberStatus"& Row),12)  
    			NumberFunction =  Left(Request.Form("NumberFunction"& Row),14)  
    			
    			FootPrint_AddedDate = Now
    			
    		  
    			sql4 = "INSERT INTO InventoryStockInbound (NumberProvider, NumberType, NetworkCarrier, NumberStatus, NumberFunction, FootPrint_AddedDate, ShowStock)" _
    					&"VALUES ('"&NumberProvider&"','"&NumberType&"','"&NetworkCarrier&"','"&NumberStatus&"','"&NumberFunction&"','"&FootPrint_AddedDate&"', True)"     
    			'SQl4 Debug
    			'Response.Write(sql4)
    			
    			Conn4.Execute sql4
    			Next
    			Conn4 = Close
    Kind regards,
    Mike Hughes

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    First of all, what are all those calls to LEFT() in there for???

    But more importantly, after you have use LEFT() on a field, the result is *NEVER* NULL.

    Anyway, it seems really really suspicious to me that all those numbers in your LEFT calls match the number of characters in the base names:
    Code:
    			NumberProvider = Left(Request.Form("NumberProvider" & Row),14)  
    			NumberType = Left(Request.Form("NumberType" & Row),10)  
    			NetworkCarrier = Left(Request.Form("NetworkCarrier"& Row),15)  
    			NumberStatus = Left(Request.Form("NumberStatus"& Row),12)  
    			NumberFunction =  Left(Request.Form("NumberFunction"& Row),14)
    That is, "NumberType" has 10 characters, "NumberProvider" has 14 characters. Hmmm...No, I'm wrong. "NetworkCarrier" has 14 yet you used 15.

    Anyway...If you are doing the LEFTs because of the *NAMES*, that is guaranteed to be totally bogus. Don't do it.

    But in any case, if you want to check for NULL or EMPTY, you have to do it *BEFORE* you call LEFT or any of the other VBS string functions.

    So you would do
    Code:
        NumberProvider = Request.Form("NumberProvider" & row)
        If IsNull(NumberProvider) OR IsEmpty(NumberProvider) Then Exit For
        NumberProvider = Lert(NumberProvider,14) ' if you really need this
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Regular Coder
    Join Date
    Jul 2002
    Location
    UK
    Posts
    302
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Hi, thanks again for your help.

    The reason why used the left function was to strip off the number from my field on my form. If you recal, my form will have several fields names the same and as you correctly pointed out these need to unique.

    So I worked out a way to strip these off so I can mutli insert. And after some playing about with my SQL I managed to do this. I just need a simple way to stop picking blanks and for those not to be insteted to my db.


    I shall give your new code below a go.

    If there is a better way for me to do a multi insert to db then please point me to it.
    Kind regards,
    Mike Hughes

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    *SIGH*

    The reason why used the left function was to strip off the number from my field on my form.
    Then your reason is 100% *BOGUS*.

    The fact that your field NAME has a NUMBER on the end of it has NOTHING NOTHING NOTHING to do with the *CONTENTS* of the field!

    If somebody types in a value for the field <input name="NumberProvider732"/> that has 23 characters in it (example: "system 27 subsystem 441"), then YOUR CODE doing Left(Request.Form("NumberProvider" & Row),14) (where Row has a value of 732) would trim that answer to "system 27 subs" and TOTALLY DESTROY the answer given by the user!

    THE NAME OF THE FIELD HAS NOTHING TO DO WITH THE CONTENTS OF THE FIELD!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Regular Coder
    Join Date
    Jul 2002
    Location
    UK
    Posts
    302
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Thanks. I see that now its not needed. my code is now update too the folllowing

    Code:
        'Dealcare the variables 
    			'Dim Conn4, Rs4, NumberProvider, NumberType, NextworkCarrier, NumberStatus, NumberFunction
    			
    			' Open objects 
    			Set Conn4 = Server.CreateObject("ADODB.Connection")
    			Conn4.Open  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("db.mdb")
    
    			'For row = 1 Request.Form("NumberProvider").count
    			For row = 1 To 10
    		    NumberProvider = Request.Form("NumberProvider" & Row)
    		    If IsNull(NumberProvider) OR IsEmpty(NumberProvider) Then Exit For
    			NumberProvider = Request.Form("NumberProvider"& Row )
    			NumberType = Request.Form("NumberType" & Row )
    			NetworkCarrier = Request.Form("NetworkCarrier"& Row)  
    			NumberFunction =  Request.Form("NumberFunction"& Row)  
    		  			  
    			sql4 = "INSERT INTO InventoryStockInbound (NumberProvider, NumberType, NetworkCarrier, NumberFunction) VALUES ('"&NumberProvider&"','"&NumberType&"','"&NetworkCarrier&"','"&NumberFunction&"')"     
    			'SQl4 Debug
    			Response.Write(sql4)
    			
    			Conn4.Execute sql4
    			Next
    			Conn4 = Close
    However, its still submiting NULL or EMPTY date to the dabase. Couyld you offer any fuhter help?
    Kind regards,
    Mike Hughes

  • #14
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,776
    Thanks
    41
    Thanked 196 Times in 195 Posts
    I may be chastised for this but.... since you are having soooo many issues with this, why don't you just add a javascript validation to your page, and then in the database make the fields not null. If someone turns of scripting and submits, or if they somehow manage to bypass the validation, they would get an error thrown up.... then handle the error by either custom page maybe? or idk if it is do-able in classic asp, but on postback could you handle the error? either way it would at least move you forward-- I'm not saying ditch trying to solve the problem, just a temporary fix until you learn more or figure it out....

    regardless though, if you are not wanting null values to be allowed in the database, the field should be not null. It's like a last defense should you not correctly validate everything upfront... it's kind of "the point" of being able to make a database field not null

    *just my thoughts though

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    I agree in principle with Alykins. It's something worth doing, no matter what.

    But let's see if we can figure out the reason for the behavior, too.

    First of all, hughesmi, I hope you realize that your code is ONLY testing for a NULL (or empty) NumberProvider. You are making no checks whatsoever of the other fields. So we cand *EXPECT* that you could get null or empty values for them!

    Assuming you know that, then let's add some debug:
    Code:
        For row = 1 To 10
            NumberProvider = Request.Form("NumberProvider" & Row)
            Response.Write "NumberProvider is null? " & IsNull(NumberProvider) _
                           & " or empty? " & IsEmpty(NumberProvider) _
                           & " has value [" & NumberProvider & "]<br/>"
           If IsNull(NumberProvider) OR IsEmpty(NumberProvider) Or Trim(NumberProvider)="" Then Exit For
           ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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