3

I've recently looked on this site and was able to alter my code to dynamically add row to a table in an html form. My issue is the next step : submitting the input fields' values by clicking the submit button. The issue is that the fields are created when the user clicks the "add" button, but the form doesn't send those values to the database (I'm using php). I have used this reference in order to make this function work, but perhaps I'm still missing an important syntax to send the array info to the field:

 <style>
    .inputThis {
        border: none;}
        .textareaThis {
            border: 0px; width: 100%; height: 25px;}
            .pvaW {
                
                width:46%;
                }
    </style> 
  
      <script language="JavaScript">
        function addRow(tableID) {
        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "text";
        element1.name="Item4[]";
       element1.className="inputThis";
      cell1.appendChild(element1);

        var cell2 = row.insertCell(1);
       var element2 = document.createElement("input");
       element2.type = "text";
        element2.name = "HowMany4[]";
         element2.className="inputThis";
        cell2.appendChild(element2);
        

 var cell3 = row.insertCell(2);
       var element3 = document.createElement("textarea");
        element3.type = "text";
        element3.name = "WhereHowUsed4[]";
         element3.className="textareaThis";
        cell3.appendChild(element3);
        
        
          var cell4 = row.insertCell(3);
       var element4 = document.createElement("input");
        element4.type = "text";
        element4.name = "Cost4[]";
         element4.className="inputThis";
        cell4.appendChild(element4);
        
        
          var cell5 = row.insertCell(4);
       var element5 = document.createElement("textarea");
        element5.type = "text";
        element5.name = "ConCur4[]";
         element5.className="textareaThis";
        cell5.appendChild(element5);
         
        
              var cell6 = row.insertCell(5);
       var element6 = document.createElement("textarea");
        element6.type = "text";
        element6.name = "Benefit4[]";
         element6.className="textareaThis";
        cell6.appendChild(element6);
        
        
              var cell7 = row.insertCell(6);
       var element7 = document.createElement("textarea");
        element7.type = "text";
        element7.name = "ItemComments4[]";
         element7.className="textareaThis";
        cell7.appendChild(element7);
        
    }
    </script>
 </head>
  <body>
     <form action="post.php" method="post" name="form1">
 <table id="tab" cellpadding="0" cellspacing="0" width="100%" class="table-striped">
<tbody>
<tr>
<td><input name="Item4[]" type="text" style="border: 0px;" /></td>
<td><input name="HowMany4[]" type="text" style="border: 0px;"/></td>
<td><textarea name="WhereHowUsed4[]" type="text" style="border: 0px; width: 100%; height: 25px;"></textarea></td>
<td><input name="Cost4[]"  type="text" style="border: 0px;"/></td>
<td><textarea name="ConCur4[]"  type="text" style="border: 0px; width: 100%; height: 25px;"></textarea></td>
 <td><textarea name="Benefit4[]" type="text" style="border: 0px; width: 100%; height: 25px;"></textarea></td>
 <td><textarea name="ItemComments4[]" type="text" style="border: 0px; width: 100%; height: 25px;"></textarea></td></tr></tbody></table>  

<div class="push-right" style="margin-top: 5px;">
<input type="button"  class="btn btn-success" id="add" value="+" name="Add" onclick="addRow('tab')"/>
</div> 
 <button class="btn btn-success" type="submit"> Submit</button>                            
                                    </form> 
 </body>

//php posting syntax from the post.php page
//////////////////
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="description" content="My Company">
<meta name="keywords" content="post">
<meta name="expires" content="never" />
<title>My Site</title>

</head>
<body>

Adding into the database....


 <?php
 $SPProxyServices = "http://--IP--:0000"; 
error_reporting(E_ALL);
ini_set('display_errors', 1);
//General
$dbfieldName1 = "Item4"; $this1= $_POST['Item4'];
$Post41 = implode("~ ", $this1);
$dbfieldName2 = "Cost4"; $this2= $_POST['Cost4'];
$Post42 = implode("~ ", $this2);
$dbfieldName3 = "HowMany4"; $this3= $_POST['HowMany4'];
$Post43 = implode("~ ", $this3);
$dbfieldName4 = "Benefit4"; $this4= $_POST['Benefit4'];
$Post44 = implode("~ ", $this4);
$dbfieldName5 = "ConCur4"; $this5= $_POST['ConCur4'];
$Post45 = implode("~ ", $this5);
$dbfieldName6 = "WhereHowUsed4"; $this6= $_POST['WhereHowUsed4'];
$Post46 = implode("~ ", $this6);


$client = new SoapClient($SPProxyServices."/");
$params->SPWebURL = "/";
$params->ListName = "/";
$params->BatchStr = "<Batch OnError=\"Continue\" ListVersion=\"1\" >
<Method ID=\"1\" Cmd=\"New\">
<Field Name='$dbfieldName1'>".$Post41."</Field>
<Field Name='$dbfieldName2'>".$Post42."</Field>
<Field Name='$dbfieldName3'>".$Post43."</Field>
<Field Name='$dbfieldName4'>".$Post44."</Field>
<Field Name='$dbfieldName5'>".$Post45."</Field>
<Field Name='$dbfieldName6'>".$Post46."</Field>
 </Method></Batch>";

 $result = $client->UpdateListItems($params);
 $simpleresult = $result; //->ListsGetListItemsResult; 

?>

5
  • I've updated the code to show everything. Thank you for any help you can provide -- I'm also new to javascript, so I hope I've added enough information. Commented Oct 18, 2016 at 13:41
  • Can you please post the header information that your are sending to the server on submit Commented Oct 18, 2016 at 15:45
  • I've updated the code. This is a content management site that stores all the data passed through the form, and I have granted the form contribute-level permissions to the list/library. So there are no usernames or passwords stored in the page. Commented Oct 18, 2016 at 16:33
  • right, looking at the dynamically created fields in the header information, the data isn't even there. It's as if the code is ignoring those fields altogether --- the hard coded fields are in the header info, but the dynamically created fields are not. Commented Oct 20, 2016 at 15:38
  • a good friend of mine solved this --- I had an extra "div" tag in my very long form. Threw the whole thing off.... on to the next adventure... Commented Oct 20, 2016 at 18:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.