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.
Results 1 to 8 of 8
  1. #1
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    190
    Thanks
    50
    Thanked 0 Times in 0 Posts

    Need guidance: web form with subform... best approach

    Hi All,

    I am working on a page with a webform and a subform. I have two related tables where my main form will populate the one side of the relationship and the subform will be for the many side.

    I am thinking about using an HTML table to hold my subform fields with javascript to dynamically add rows for additional records. I am having trouble with the design of the form processing script, however as since the index on the parent record does not exist at the time of processing I can't think of a way to populate the link field for the child records.

    The only thing I can think of is to have my page's onload event create a "blank" record (generating the index and storing it in a hidden field) and then essentially make the proceesing script be an update rather than a create. This of course would involve creating some sort of delete function if the user leaves the page without submiting the form.

    I am not sure this is the best approach but it is all I can come up with. I am open to any ideas or guidance from the community on either my subform idea or processing script idea and I thank you in advance.

    Kind regards,

    Ken

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,519
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    I'm lost.

    First of all, what is a "subform"?? You can't have more than one <Form> on a page if you expect use the <form> to submit all data to server-side processing.

    I am having trouble with the design of the form processing script, however as since the index on the parent record does not exist at the time of processing I can't think of a way to populate the link field for the child records.
    What "form processing script"? JavaScript or server-side code? Why do you need any index for JS purposes? And for server-side, you just get the index as you add to the main table and then use it for adding items to the details table.
    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
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If you are going to use JavaScript with a form then you need to get the form working without JavaScript first. The JavaScript would just be there to save having to reload the page so many times for those with JavaScript enabled.

    Sounds like you need to display so many of the repeating entries to start with and have add and delete buttons that will reload the page with the existing entered form content that changes the number of those repeating elements that are displayed. You can then attach JavaScript to those buttons to dynamically add or delete fields without reloading the form if the JavaScript is able to run. You need to distinguish which button was used to submit the form in order to distinguish between the buttons to add or delete fields and the one to process the form.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    MaDmiX (03-03-2012)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,519
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    There is another way to do this that will avoid depending on JavaScript, as Felgall is espousing.

    Just create the page with, say, space for 20 or so detail items.

    Then, at page load time, have JavaScript hide all but the first one. And have a button that uses JavaScript to reveal a new detail line each time it is clicked.

    If JavaScript is disabled, then the page will come up showing all 20 lines. But so what? Your server side code can then simply ignore any lines where the user enters no information.

    You would want to do that, anyway, even if JavaScript is enabled, because somebody might push the button too many times and then leave some lines blank.

    You still have the advantage that the page only needs to be submitted once (instead of once per each new item, if you do it the way Felgall suggests). All the JavaScript is doing in this case is making the page look more attractive. But it all stays perfectly usable with JS is disabled. As Felgall says, this is a huge plus and, for a true internet situation, should really be required.
    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.

  • #5
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    190
    Thanks
    50
    Thanked 0 Times in 0 Posts
    Thanks guys for all your good advice. Old Pedant, I see that there really is no such thing as a "subform" for web applications (this is an Access thing and makes the entry of records on the many side of a one to many relationship very simple).

    I guess that by setting up a table to hold the repeating fields it is essentially the same thing. One question, though... how would I set up the page to have the "detail items" repeating the same set of fields without getting a duplicate field id error? I would think that if I tried to add a field with the same id as another field on my form I would get an error.

    As it is, I have a table with one row containing the fields for the detail items and now need to repeat those fields for each record of detail items.

    Thanks,

    Ken

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by MaDmiX View Post
    One question, though... how would I set up the page to have the "detail items" repeating the same set of fields without getting a duplicate field id error?
    Add a number to the end so as to make the ids unique. If there are 20 repetitions of the field then use 1 through 20 on the end of the id. If you are using a loop server side to generate the form fields then you should already have a loop counter you can use.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,519
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Since you are used to using Access, I'll assume your server-side code will be ASP.

    We should move this to the ASP forum, but here's a simple example:
    Code:
    <form action="saveData.asp" method="post">
    Name: <input name="customerName">
    
    Items:
    <table>
    <tr>
        <th>item#</th><th>product id</th><th>quantity</th>
    </tr>
    <tr>
        <td>1</td><td><input name="product1"/></td><td><input name="quantity1"/></td>
    </tr>
    <tr>
        <td>2</td><td><input name="product3"/></td><td><input name="quantity1"/></td>
    </tr>
    <tr>
        <td>3</td><td><input name="product3"/></td><td><input name="quantity1"/></td>
    </tr>
    </table>
    <input type="submt" />
    </form>
    And we'll assume you have this for your DB:
    Code:
    CREATE TABLE customers (
        custid INT AUTONUMBER PRIMARY KEY,
        custname VARCHAR(100)
    )
    
    CREATE TABLE orders (
        orderid INT AUTONUMBER PRIMARY KEY,
        custid INT FOREIGN KEY REFERENCES customers(custid),
        orderdate DATETIME DEFAULT NOW()
    )
    
    CREATE TABLE orderitems (
        itemid INT AUTONUMBER PRIMARY KEY,
        orderid INT FOREIGN KEY REFERENCES orders(orderid),
        productid INT FOREIGN KEY REFERENCES products(productid),
        quantity INT 
    )
    
    [products table not shown]
    And so your ASP code might look like this:
    Code:
    <%
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.Open "...your connection string..."
    
    custname = Trim(Request("customername"))
    Set custrs = Server.CreateObject("ADODB.Recordset")
    SQL = "SELECT * FROM customers WHERE customername = '" & custname & "'"
    custrs.Open SQL, conn, 3, 3 ' static cursor, open for write
    
    If custrs.EOF Then 
        custrs.AddNew
        custrs("customername") = custname
        custrs.Update
    End if
    custid = custrs("custid")
    custrs.Close
    
    SQL = "INSERT INTO orders(custid)"
    conn.Execute SQL
    Set ors = conn.Execute( "SELECT @@IDENTITY") ' this is how you get autonumber of just-created record
    orderid = ors(0)
    ors.Close
    
    For itemnum = 1 TO 3 ' or to however many rows are in form
        prodid = 0 : qty = 0
        On Error Resume Next
            prodid = CLNG(Trim(Request.Form("productid" & itemnum))
            qty = CLNG(Trim(Request.Form("quantity" & itemnum))
        On Error GoTo 0
        If prodid > 0 AND qty > 0 THen
            SQL = "INSERT INTO orderitems(orderid,prodid,quantity) " _
                   & " VALUES(" & orderid & "," & prodid & "," & qty & ")"
            howmany = 0
            On Error Resume Next
                conn.Execute SQL, howmany 
            On Error GoTo 0
            IF howmany <> 1 Then
                Response.Write "Invalid product id for item #" & itemnum & "<br/>"
            End If
        End If
    Next
    conn.Close
    %>
    There are a few shortcuts in there, but not any that matter much.

    See? Not really hard, at all.
    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.

  • Users who have thanked Old Pedant for this post:

    MaDmiX (03-03-2012)

  • #8
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    190
    Thanks
    50
    Thanked 0 Times in 0 Posts
    Wow! Very cool. I am actually using PHP for my server side script (trying to break the MS teather) but the ASP logic is easy enough to follow and much like the code I am used to. Are there any advantages to ASP over PHP?

    Thank you guys sooo much for your help.

    Best regards,

    Ken


  •  

    Posting Permissions

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