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

    Thanks in advance!


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

    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
    //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
    newCell.innerHTML = textbox1;//insertion of the 'text'
    variable in first cell
    var newCell = newRow.insertCell(1);//second cell in the
    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
    } catch (ex) {
    alert(ex); //if exception occurs

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

    if(row_no<=99)//if row is less than 3 then the button will again
    appear to add row
    <form id="orderForm" name="orderForm" method="post"
    <table width="700" border="0" cellpadding="0" cellspacing="0"
    <tr id="header">
    <td width="190" valign="top" bgcolor="#666666"><strong>Pub
    <td width="190" valign="top"
    <td width="190" valign="top"
    bgcolor="#666666"><strong>Additional Comments</strong></td>
    <td width="130" bgcolor="#666666">&nbsp;</td>
    <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>
    <tr id="footer">
    <td colspan="4" valign="top"><input type="button" value="Add
    another item" onClick="addRow('order','item')" ID="add"></td>
    <td colspan="4" valign="top"><input type="submit" name="Submit"
    id="Submit" value="Submit" /></td>

    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

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

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

  2. NoChat wrote:
    ? 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>"

    --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
    1. Advertisements

  3. NoChat wrote on Thu, 20 Dec 2007 12:37:53 -0800 (PST):
    This just takes the contents of the Request items and puts them into an
    array variable that has a single item.


    myvar = Array("test")

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

    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.


    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
    End If
    Daniel Crichton, Dec 21, 2007
  4. NoChat

    NoChat Guest

    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
  5. 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[]"),","")
    Bob Barrows [MVP], Dec 21, 2007
  6. NoChat

    NoChat Guest


    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
  7. 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:
    Bob Barrows [MVP], Dec 27, 2007
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.