#1
  1. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Feb 2007
    Posts
    3
    Rep Power
    0

    ASP Insert and JS form loop


    I have an ASP page that inserts records to a database. In the form I have a dynamic loop where i is provided by a session variable. This works. However, if i=1 the record is inserted in the databasecorrectly but if i >1 then the insertRecord behavior tries to insert the input field i times in the associated table field.
    How can I insert all the input fields, row by row into the database, in sequence, when the submit button is presses?

    Thank you for your help.

    JM

    =================
    [CODE]
    <script language="JavaScript" type="text/JavaScript">
    var totalBoxes = <%= Session("sessNumberBoxes") %>;
    var message = parseInt(totalBoxes );

    for ( var i =1; i <= message; i++ )
    {
    document.write('<tr>');
    document.write('<TD width="80" height="24" valign="top"> </TD>');
    document.write('<TD width="50" valign="top"><input name="txtDetReceiverID" type="text" id="txtDetReceiverID" value="<%= Session("sessReceiverID") %>" size="10" maxlength="10"></TD>');
    document.write('<TD width="30" valign="top"><input name="txtDetPO_LINE_NO" type="text" id="txtDetPO_LINE_NO" value="<%= Session("sessPO_LineNo") %>" size="3" maxlength="3"></TD>');
    document.write('<TD width="70" valign="top"> </TD>');
    document.write('<TD width="50" align="right" valign="top" NOWRAP><div align="right">Box #: </div></TD>');
    document.write('<TD width="24" align="left" valign="top" size="3" maxlength="3"><div align="center"><input name="txtDetBoxNumber" type="text" id="txtDetBoxNumber" value="'+ i +'" size="3" maxlength="3"></div></TD>');
    document.write('<TD width="40" align ="right" valign="top" NOWRAP>Qty: </TD>');
    document.write('<TD width="40" align="left" valign="top"><input name="txtBoxQty" type="text" id="txtBoxQty" size="10" maxlength="10"></TD>');

    document.write('</tr>');
    }
    </script>
  2. #2
  3. Contributing User
    ASP Hero (2000 - 2499 posts)

    Join Date
    Nov 2003
    Location
    UK
    Posts
    2,280
    Rep Power
    313

    ASP Insert and JS form loop


    couple of questions first,
    is there a reason your using javascript to do the loop?
    as you can do the same in asp

    do the session variables contain array's?
    Hope this advise helps.

    If so please show your appreciation by adding reputation points (click gauge image on top right of this post and score).
  4. #3
  5. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Feb 2007
    Posts
    3
    Rep Power
    0

    Question ASP Issue


    Well.
    I am not an ASP expert. I used JavaScript I could use ASP in the form. I just do not know how I could proceed.
    In the page previous to my form, I enter a number of boxes that is set in as session variable. I send it to the form which create i rows.
    If I use an array, how will it work? What would the ASP code be?
    Even a path to more infor in resolving this will be appreciated.

    Thank you,

    Jean-Marie
  6. #4
  7. No Profile Picture
    Grumpier Old Moderator
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2003
    Posts
    10,143
    Rep Power
    190
    The js you posted is running in the web browser and will have no direct access to change any asp server-side variables. And you didn't post any of the code that is supposed to insert into a database.
    ======
    Doug G
    ======
    I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain
  8. #5
  9. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Feb 2007
    Posts
    3
    Rep Power
    0

    ASP multiple insert issue


    Here is the complete code for the page.

    [CODE]

    <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
    <!--#include file="Connections/RCVR.asp" -->
    <%
    // *** Edit Operations: declare variables

    // set the form action variable
    var MM_editAction = Request.ServerVariables("SCRIPT_NAME");
    if (Request.QueryString) {
    MM_editAction += "?" + Server.HTMLEncode(Request.QueryString);
    }

    // boolean to abort record edit
    var MM_abortEdit = false;

    // query string to execute
    var MM_editQuery = "";
    %>
    <%
    // *** Insert Record: set variables

    if (String(Request("MM_insert")) == "frmSetLabels") {

    Session("sessReceiverID")=String(Request.Form("txt ReceiverID"));
    Session("sessPO_LineNo")=String(Request.Form("txtP O_LineNo"));
    Session("sessNumberBoxes")=String(Request.Form("tx tNumberBoxes"));

    var MM_editConnection = MM_RCVR_STRING;
    var MM_editTable = "dbo.DYNA_BOX_DETAILS";
    var MM_editRedirectUrl = "insertDetail.asp";
    var MM_fieldsStr = "txtDetReceiverID|value";
    var MM_columnsStr = "RECEIVER_ID|',none,''";

    // create the MM_fields and MM_columns arrays
    var MM_fields = MM_fieldsStr.split("|");
    var MM_columns = MM_columnsStr.split("|");

    // set the form values
    for (var i=0; i+1 < MM_fields.length; i+=2) {
    MM_fields[i+1] = String(Request.Form(MM_fields[i]));
    }

    // append the query string to the redirect URL
    if (MM_editRedirectUrl && Request.QueryString && Request.QueryString.Count > 0) {
    MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + Request.QueryString;
    }
    }
    %>
    <%
    // *** Insert Record: construct a sql insert statement and execute it

    if (String(Request("MM_insert")) != "undefined") {

    // create the sql insert statement
    var MM_tableValues = "", MM_dbValues = "";
    for (var i=0; i+1 < MM_fields.length; i+=2) {
    var formVal = MM_fields[i+1];
    var MM_typesArray = MM_columns[i+1].split(",");
    var delim = (MM_typesArray[0] != "none") ? MM_typesArray[0] : "";
    var altVal = (MM_typesArray[1] != "none") ? MM_typesArray[1] : "";
    var emptyVal = (MM_typesArray[2] != "none") ? MM_typesArray[2] : "";
    if (formVal == "" || formVal == "undefined") {
    formVal = emptyVal;
    } else {
    if (altVal != "") {
    formVal = altVal;
    } else if (delim == "'") { // escape quotes
    formVal = "'" + formVal.replace(/'/g,"''") + "'";
    } else {
    formVal = delim + formVal + delim;
    }
    }
    MM_tableValues += ((i != 0) ? "," : "") + MM_columns[i];
    MM_dbValues += ((i != 0) ? "," : "") + formVal;
    }
    MM_editQuery = "insert into " + MM_editTable + " (" + MM_tableValues + ") values (" + MM_dbValues + ")";

    if (!MM_abortEdit) {
    // execute the insert
    var MM_editCmd = Server.CreateObject('ADODB.Command');
    MM_editCmd.ActiveConnection = MM_editConnection;
    MM_editCmd.CommandText = MM_editQuery;
    MM_editCmd.Execute();
    MM_editCmd.ActiveConnection.Close();

    if (MM_editRedirectUrl) {
    Response.Redirect(MM_editRedirectUrl);
    }
    }

    }
    %>
    <%
    var rsBoxDetails = Server.CreateObject("ADODB.Recordset");
    rsBoxDetails.ActiveConnection = MM_RCVR_STRING;
    rsBoxDetails.Source = "SELECT * FROM dbo.DYNA_BOX_DETAILS";
    rsBoxDetails.CursorType = 0;
    rsBoxDetails.CursorLocation = 2;
    rsBoxDetails.LockType = 1;
    rsBoxDetails.Open();
    var rsBoxDetails_numRows = 0;
    %>
    <% var MM_paramName = ""; %>
    <%
    // *** Go To Record and Move To Record: create strings for maintaining URL and Form parameters

    // create the list of parameters which should not be maintained
    var MM_removeList = "&index=";
    if (MM_paramName != "") MM_removeList += "&" + MM_paramName.toLowerCase() + "=";
    var MM_keepURL="",MM_keepForm="",MM_keepBoth="",MM_kee pNone="";

    // add the URL parameters to the MM_keepURL string
    for (var items=new Enumerator(Request.QueryString); !items.atEnd(); items.moveNext()) {
    var nextItem = "&" + items.item().toLowerCase() + "=";
    if (MM_removeList.indexOf(nextItem) == -1) {
    MM_keepURL += "&" + items.item() + "=" + Server.URLencode(Request.QueryString(items.item()) );
    }
    }

    // add the Form variables to the MM_keepForm string
    for (var items=new Enumerator(Request.Form); !items.atEnd(); items.moveNext()) {
    var nextItem = "&" + items.item().toLowerCase() + "=";
    if (MM_removeList.indexOf(nextItem) == -1) {
    MM_keepForm += "&" + items.item() + "=" + Server.URLencode(Request.Form(items.item()));
    }
    }

    // create the Form + URL string and remove the intial '&' from each of the strings
    MM_keepBoth = MM_keepURL + MM_keepForm;
    if (MM_keepBoth.length > 0) MM_keepBoth = MM_keepBoth.substring(1);
    if (MM_keepURL.length > 0) MM_keepURL = MM_keepURL.substring(1);
    if (MM_keepForm.length > 0) MM_keepForm = MM_keepForm.substring(1);
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "(URL address blocked: See forum rules)">


    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>

    </head>

    <body>
    <center>
    <table width="610" border="2" cellpadding="0" cellspacing="0">
    <!--DWLayoutTable-->
    <tr>
    <td width="7" height="9"></td>
    <td width="584"></td>
    <td width="9"></td>
    </tr>
    <tr>
    <td height="346"></td>
    <td align="center" valign="middle"><table width="100%" border="0" cellpadding="0" cellspacing="0">
    <!--DWLayoutTable-->
    <tr>
    <td height="42" colspan="2" align="center" valign="middle"><div align="center">
    <p>&nbsp;</p>
    <p><strong>RECEIVING LABELS DETAILS </strong></p>
    <p>&nbsp;</p>
    </div></td>
    </tr>
    <tr>
    <td width="584" height="295" valign="top">
    <TABLE>
    <!--DWLayoutTable-->
    <TR>
    <TD width="573" height="158" valign="top"><form ACTION="<%=MM_editAction%>" METHOD="POST" name="frmSetLabels" id="frmSetLabels">
    <table>
    <!--DWLayoutTable-->

    <TR>
    <TD width="85" height="24" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</TD>
    <TD width="99" valign="top">Receiver ID : </TD>
    <TD colspan="2" valign="top"><input name="txtReceiverID" type="text" id="txtReceiverID" value="<%= Session("sessReceiverID") %>"+"/"+ size="10" maxlength="10"> </TD>
    <TD width="21" align="right" valign="top" nowrap><div align="right">Line:&nbsp;&nbsp;</div></TD>
    <TD colspan="2" align="left" valign="top"><div align="left">
    <input name="txtPO_LINE_NO" type="text" id="txtPO_LINE_NO" value="<%= Session("sessPO_LineNo") %>" size="6" maxlength="5">
    </div></TD>
    <TD width="22" align="right" valign="top" nowrap>
    <div align="right">
    <input name="txtTotal_Boxes" type="text" id="txtTOTAL_BOXES" value="<%= Session("sessNumberBoxes") %>" size="3" maxlength="3">
    </div></TD>
    <TD width="92" align="left" valign="top"><div align="left">Boxes</div></TD>
    </TR>
    <TR>
    <TD height="24" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</TD>
    <TD colspan="8" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</TD>
    </TR>
    <TR>
    <TD height="24" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</TD>
    <TD colspan="2" align="right" valign="top"><div align="right">&nbsp;&nbsp; </div></TD>
    <TD colspan="5" align="left" valign="top"><div align="left">
    </div></TD>
    <TD valign="top"><!--DWLayoutEmptyCell-->&nbsp;</TD>
    </TR>
    <tr widht="584">
    <td height="6" colspan="9" valign="top">
    <table bgcolor="#FFFFFF">
    <!--DWLayoutTable-->


    <script language="JavaScript" type="text/JavaScript">
    var totalBoxes = <%= Session("sessNumberBoxes") %>;
    var message = parseInt(totalBoxes );

    for ( var i =1; i <= message; i++ )
    {
    document.write('<tr>');
    document.write('<TD width="80" height="24" valign="top">&nbsp;</TD>');
    document.write('<TD width="50" valign="top"><input name="txtDetReceiverID" type="text" id="txtDetReceiverID" value="<%= Session("sessReceiverID") %>" size="10" maxlength="10"></TD>');
    document.write('<TD width="30" valign="top"><input name="txtDetPO_LINE_NO" type="text" id="txtDetPO_LINE_NO" value="<%= Session("sessPO_LineNo") %>" size="3" maxlength="3"></TD>');
    document.write('<TD width="70" valign="top">&nbsp;</TD>');
    document.write('<TD width="50" align="right" valign="top" NOWRAP><div align="right">Box #:&nbsp;&nbsp; </div></TD>');
    document.write('<TD width="24" align="left" valign="top" size="3" maxlength="3"><div align="center"><input name="txtDetBoxNumber" type="text" id="txtDetBoxNumber" value="'+ i +'" size="3" maxlength="3"></div></TD>');
    document.write('<TD width="40" align ="right" valign="top" NOWRAP>Qty:&nbsp;&nbsp;</TD>');
    document.write('<TD width="40" align="left" valign="top"><input name="txtBoxQty" type="text" id="txtBoxQty" size="10" maxlength="10"></TD>');

    document.write('</tr>');
    }
    </script>
    </table></td>
    </tr>


    <TR>
    <TD height="24" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</TD>
    <TD colspan="2" align="right" valign="top"><div align="right"> </div></TD>
    <TD colspan="3" align="left" valign="top"><div align="left"></div></TD>
    <TD width="34" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</TD>
    <TD valign="top"><!--DWLayoutEmptyCell-->&nbsp;</TD>
    <TD valign="top"><!--DWLayoutEmptyCell-->&nbsp;</TD>
    </TR>
    <TR>
    <TD height="24" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</TD>
    <TD colspan="2" align="right" valign="top"><div align="right">&nbsp;</div></TD>
    <TD colspan="5" align="left" valign="top"><div align="left">
    </div></TD>
    <TD valign="top"><!--DWLayoutEmptyCell-->&nbsp;</TD>
    </TR>
    <tr>
    <td height="26"><!--DWLayoutEmptyCell-->&nbsp;</td>
    <td colspan="2" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
    <td colspan="5" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
    <td valign="top"><div align="right">
    <input type="submit" name="Submit" value="Insert Record">
    </div></td>
    </tr>
    <tr>
    <td height="5"></td>
    <td></td>
    <td width="49"></td>
    <td width="3"></td>
    <td></td>
    <td width="6"></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>




    </table>





    <input type="hidden" name="MM_insert" value="frmSetLabels">
    </form></TD>
    </TR>
    </TABLE>




    </td>


    </tr>
    <tr>
    <td height="9"></td>
    <td></td>
    </tr>
    </table></td>
    <td></td>
    </tr>
    <tr>
    <td height="13"></td>
    <td></td>
    <td></td>
    </tr>
    </table>
    </center>
    </body>
    </html>
    <%
    rsBoxDetails.Close();
    %>
    Last edited by ecoviva; February 16th, 2007 at 03:58 PM. Reason: add [CODE] tag
  10. #6
  11. No Profile Picture
    Contributing User
    ASP Mastermind (5000+ posts)

    Join Date
    Aug 2005
    Location
    North East, UK
    Posts
    6,191
    Rep Power
    140
    Using inbuilt dreamweaver scripts will not allow you to insert multiple records.

    You will have you write your own code to loop through the submitted records.


    There are different ways to do this.
    One of those is to suffix your element names with the loop counter.
    When you submit the form you can grab the data and split the name to get the loop counter.

    This will make sure that you insert the correct data.

    Here is an example of that type of form
    http://computer-helpforum.com/asp/up..._textfield.asp

    Comments on this post

    • ecoviva agrees : This code will help me greatly...

Similar Threads

  1. Debugging of a stored Procedure
    By zwieback89 in forum Microsoft SQL Server
    Replies: 0
    Last Post: January 11th, 2007, 01:01 PM
  2. Replies: 10
    Last Post: December 5th, 2006, 08:35 AM
  3. Replies: 2
    Last Post: May 5th, 2006, 09:58 AM
  4. Insert Date / Time into SQL Database via ASP Form Request.
    By JoshCBR28 in forum ASP Development
    Replies: 2
    Last Post: October 28th, 2005, 11:40 AM
  5. Replies: 1
    Last Post: April 2nd, 2004, 04:51 PM

IMN logo majestic logo threadwatch logo seochat tools logo