Using Arrays from Javascript

Discussion in 'ASP General' started by NoChat, Dec 20, 2007.

  1. NoChat

    NoChat Guest

    Hi all --

    I am new to arrays and could use some help. Here is my situation: I
    have a very basic order form that I am allowing users to fill out.
    They can enter multiple products onto this form, so I have created a
    simple Javascript function that allows the user to dynamically add
    another row of text boxes for each item he or she would like to
    order. I am submitting the form to another ASP page which I hope can
    read the information from the form and send an email notification to
    somebody else.

    I have already built the emailing portion of this ASP script, but I've
    been unable to read in the various form items. I'm guessing there is
    something incompatible with either my Javascript or ASP. I have
    included the important code snippets below and could use some
    assistance.

    Thanks in advance!
    --Ty

    JAVASCRIPT & HTML CODE THAT CREATES DYNAMIC FORM OBJECTS
    ________________________________________________________

    <script language="javascript">
    row_no=1;
    function addRow(tbl,row){
    //so that user can only add 99 rows
    if(row_no<=99){

    var textbox1='<input type="text" name="pub[]">';//for text box
    var textbox2='<input type="text" name="qty[]">';//for text box
    var textbox3='<input type="text" name="comments[]">';//for text box
    var remove= '<a href="#" onclick="removeRow(\''+ tbl +'\',\''
    + row_no + '\')"/>Remove It</a>'; //for the text which is used to
    remove the current row by using the function removeRow(..,..)

    //for suitable label to the row
    //if(row_no==1) text="<div class='label' align=right>First
    Textbox:</div>";
    //else if(row_no==2) text="<div class='label'
    align=right>Second Textbox:</div>";
    //else if(row_no==3) text="<div class='label'
    align=right>Third Textbox:</div>";

    var tbl = document.getElementById(tbl);//to identify the table
    in which the row will get insert
    var rowIndex = document.getElementById(row).value;//to
    identify the row after which the row will be inserted
    try {
    var newRow = tbl.insertRow(row_no);//creation of new row
    var newCell = newRow.insertCell(0);//first cell in the
    row
    newCell.innerHTML = textbox1;//insertion of the 'text'
    variable in first cell
    var newCell = newRow.insertCell(1);//second cell in the
    row
    newCell.innerHTML = textbox2;//insertion of the text box
    and the remove text using their variable
    var newCell = newRow.insertCell(2);//third cell in the row
    newCell.innerHTML = textbox3;//insertion of the text box
    and the remove text using their variable
    var newCell = newRow.insertCell(3);//fourt cell in the row
    newCell.innerHTML = remove;//insertion of the remove text
    row_no++;
    } catch (ex) {
    alert(ex); //if exception occurs
    }

    }
    if(row_no>99)//if the row contain 99 textboxes, the add button
    will disapper
    {
    document.getElementById("add").style.display="none";
    }
    }
    function removeRow(tbl,num)
    {
    var table = document.getElementById(tbl);//identification of table
    try {
    row_no--;
    table.deleteRow(num);//deletion of the clicked row
    } catch (ex) {
    alert(ex);
    }

    if(row_no<=99)//if row is less than 3 then the button will again
    appear to add row
    {
    document.getElementById("add").style.display="block";
    }
    }
    </script>
    <form id="orderForm" name="orderForm" method="post"
    action="aspmail.asp">
    <table width="700" border="0" cellpadding="0" cellspacing="0"
    id="order">
    <tr id="header">
    <td width="190" valign="top" bgcolor="#666666"><strong>Pub
    #</strong></td>
    <td width="190" valign="top"
    bgcolor="#666666"><strong>Quantity</strong></td>
    <td width="190" valign="top"
    bgcolor="#666666"><strong>Additional Comments</strong></td>
    <td width="130" bgcolor="#666666">&nbsp;</td>
    </tr>
    <tr id="item">
    <td valign="top"><input type="text" name="pub"></td>
    <td valign="top"><input type="text" name="qty"></td>
    <td valign="top"><input type="text" name="comments"></td>
    <td>&nbsp;</td>
    </tr>
    <tr id="footer">
    <td colspan="4" valign="top"><input type="button" value="Add
    another item" onClick="addRow('order','item')" ID="add"></td>
    </tr>
    <tr>
    <td colspan="4" valign="top"><input type="submit" name="Submit"
    id="Submit" value="Submit" /></td>
    </tr>
    </table>
    </form>
    ________________________________________________________
    ASP CODE I'M USING TO TRY AND READ IN THIS INFORMATION
    ________________________________________________________

    Dim pub, qty, comments

    'order information
    pub = Array(Request.Form("pub"))
    qty = Array(Request.Form("qty"))
    comments = Array(Request.Form("comments"))

    '* Pull in form arrays
    For i=0 to ubound(pub)
    Response.Write "Item #: " & pub(i) & "<br>" & vbCrLf
    next

    For i=0 to ubound(qty)
    Response.Write "Qty #: " & qty(i) & "<br>" & vbCrLf
    next

    For i=0 to ubound(comments)
    Response.Write "Comments: " & comments(i) & "<br>" & vbCrLf
    next
    NoChat, Dec 20, 2007
    #1
    1. Advertising

  2. NoChat wrote:
    ________________________________________________________
    > ASP CODE I'M USING TO TRY AND READ IN THIS INFORMATION
    > ________________________________________________________
    >
    > Dim pub, qty, comments
    >
    > 'order information
    > pub = Array(Request.Form("pub"))


    ? This does not make sense.

    Do this to show us what is coming from your form:
    <%
    for each key in request.form
    response.write key & ": " & request.form(key) & "<BR>"
    next
    %>

    --Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
    Bob Barrows [MVP], Dec 20, 2007
    #2
    1. Advertising

  3. NoChat wrote on Thu, 20 Dec 2007 12:37:53 -0800 (PST):

    > 'order information
    > pub = Array(Request.Form("pub"))
    > qty = Array(Request.Form("qty"))
    > comments = Array(Request.Form("comments"))


    This just takes the contents of the Request items and puts them into an
    array variable that has a single item.

    eg.

    myvar = Array("test")

    creates an Array which has a single value at location 0.


    > '* Pull in form arrays
    > For i=0 to ubound(pub)
    > Response.Write "Item #: " & pub(i) & "<br>" & vbCrLf
    > next


    > For i=0 to ubound(qty)
    > Response.Write "Qty #: " & qty(i) & "<br>" & vbCrLf
    > next


    > For i=0 to ubound(comments)
    > Response.Write "Comments: " & comments(i) & "<br>" & vbCrLf
    > next


    What does the output look like?

    I'm guessing you need to use Split to break up a string into items based on
    a delimiter.

    eg.

    myvar = Split("test1,test2",",")

    will result in an array with 2 items, "test1" at location 0 and "test2" at
    location 1.

    However, you might find that this is already done for you if the data is
    posted as fields with the same name. You can check this using the .Count
    property, eg.

    If Request.Form("pub").Count > 1 Then
    'there are multiple items, which means they were posted all with the
    same field name and have been automatically split up in the Request
    collection
    End If

    --
    Dan
    Daniel Crichton, Dec 21, 2007
    #3
  4. NoChat

    NoChat Guest

    On Dec 20, 4:21 pm, "Bob Barrows [MVP]" <>
    wrote:
    > NoChat wrote:
    >
    > ________________________________________________________
    >
    > > ASP CODE I'M USING TO TRY AND READ IN THIS INFORMATION
    > > ________________________________________________________

    >
    > > Dim pub, qty, comments

    >
    > > 'order information
    > > pub = Array(Request.Form("pub"))

    >
    > ? This does not make sense.
    >
    > Do this to show us what is coming from your form:
    > <%
    > for each key in request.form
    > response.write key & ": " & request.form(key) & "<BR>"
    > next
    > %>
    >
    > --Microsoft MVP - ASP/ASP.NET
    > Please reply to the newsgroup. This email account is my spam trap so I
    > don't check it very often. If you must reply off-line, then remove the
    > "NO SPAM"


    Thanks for all your help guys... Bob, I tried what you said. Here's
    what I got :

    qty[]: qty1, qty2, qty3
    pub[]: pub1, pub2, pub3
    comments[]: com1, com2, com3
    Submit: Submit
    NoChat, Dec 21, 2007
    #4
  5. NoChat wrote:
    >>

    > qty[]: qty1, qty2, qty3
    > pub[]: pub1, pub2, pub3
    > comments[]: com1, com2, com3
    > Submit: Submit


    OK, so now you want to put these results into arrays? Basically, just do
    what Daniel said:
    pub = Split(Request.Form("pub[]"),",")
    qty = Split(Request.Form("qty[]"),","")


    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
    Bob Barrows [MVP], Dec 21, 2007
    #5
  6. NoChat

    NoChat Guest

    On Dec 21, 3:18 pm, "Bob Barrows [MVP]" <>
    wrote:
    > NoChat wrote:
    >
    > > qty[]: qty1, qty2, qty3
    > > pub[]: pub1, pub2, pub3
    > > comments[]: com1, com2, com3
    > > Submit: Submit

    >
    > OK, so now you want to put these results into arrays? Basically, just do
    > what Daniel said:
    > pub = Split(Request.Form("pub[]"),",")
    > qty = Split(Request.Form("qty[]"),","")
    >
    > --
    > Microsoft MVP - ASP/ASP.NET
    > Please reply to the newsgroup. This email account is my spam trap so I
    > don't check it very often. If you must reply off-line, then remove the
    > "NO SPAM"


    Guys,

    Thanks for your help. I've just about got it working the way I need.
    My apologies if my questions were ridiculous -- I don't have much
    experience with arrays.

    One more question: The items being stored in my arrays need to be
    emailed to somebody. I have the email script working properly, so
    that's not the issue. The problem is that I'm storing the body text
    (and formatting it) for that email message in a variable called
    "Bodytext" and I can't seem to incorporate my arrays into that.
    Obviously, I need to build some sort of loop that outputs everything,
    and I can't just do a simple Response.Write because that doesn't work
    for this purpose. Any suggestions for getting these arrays to work
    with that variable?
    NoChat, Dec 27, 2007
    #6
  7. NoChat wrote:
    > Guys,
    >
    > Thanks for your help. I've just about got it working the way I need.
    > My apologies if my questions were ridiculous -- I don't have much
    > experience with arrays.
    >
    > One more question: The items being stored in my arrays need to be
    > emailed to somebody. I have the email script working properly, so
    > that's not the issue. The problem is that I'm storing the body text
    > (and formatting it) for that email message in a variable called
    > "Bodytext" and I can't seem to incorporate my arrays into that.
    > Obviously, I need to build some sort of loop that outputs everything,
    > and I can't just do a simple Response.Write because that doesn't work
    > for this purpose. Any suggestions for getting these arrays to work
    > with that variable?


    I haven't a clue what you're trying to do, but I will hazard a guess
    that the Join method may be what you are after:
    http://msdn2.microsoft.com/en-us/library/yscc53h0.aspx

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Dec 27, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,583
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,248
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    562
  4. Philipp
    Replies:
    21
    Views:
    1,123
    Philipp
    Jan 20, 2009
  5. shashi
    Replies:
    17
    Views:
    412
    Guest
    Apr 13, 2006
Loading...

Share This Page