2012-07-14 65 views
0

我想將此動態創建的錶行的所有記錄保存到mysql數據庫中,但即時通訊無法獲取動態添加字段的名稱值,我可以在$ _POST方法中使用它。請告訴我如何將JavaScript var轉換爲Php變量如何使用javaScript獲取動態創建表的名稱屬性值

<?php 
if(isset($_POST['buton_sumit'])) 
    { 
$nav_id =1; 
$item1=$_POST['name1']; 
$item1=$_POST['name2']; 

echo $item1 + $item2; 
    } 
?> 



<!DOCTYPE HTML> 
<HTML> 
<HEAD> 
<TITLE> Add/Remove dynamic rows in HTML table </TITLE> 

</HEAD> 
<BODY> 

    <table id="extra" width="499" > 
    <th width="24">R</th><th width="19">Sr.</th><th width="133">Item-Name</th><th width="144">Quantity</th><th width="145">price</th> 
    </table> 
    <form action="table1.php" method="post" name="input"> 
    <TABLE id="dataTable" width="350px" border="1"> 

     <TR> 
      <TD width="24"><INPUT type="checkbox" name="chk"/></TD> 
      <TD width="19"> 1 </TD> 
      <TD width="133"> <INPUT type="text" name="name1"/> </TD> 
      <TD width="133"> <INPUT type="text" name="qty1"/> </TD> 
      <TD width="133"> <INPUT type="text" name="price1"/> </TD> 
     </TR> 
</TABLE> 


    <INPUT type="button" value="Add Item" onclick="addRow('dataTable')" /> 

    <INPUT type="button" value="Remove Item" onclick="deleteRow('dataTable')" /> 
    <INPUT type="submit" value="End Sale" name="buton_sumit" /> 
    </form> 
<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 = "checkbox"; 
      cell1.appendChild(element1); 

      var cell2 = row.insertCell(1); 
      cell2.innerHTML = rowCount + 1; 

      var cell3 = row.insertCell(2); 
      var element2 = document.createElement("input"); 
      element2.type = "text"; 
      element2.name="name"+ cell2.innerHTML; 
      var element2id="myid"+cell2.innerHTML; 

      element2.setAttribute('id',element2id); 

      cell3.appendChild(element2); 

      var cell4 = row.insertCell(3); 
      var element3 = document.createElement("input"); 
      element3.type = "text"; 
      element3.name="qty"+ cell2.innerHTML; 

      cell4.appendChild(element3); 

      var cell5 = row.insertCell(4); 
      var element4 = document.createElement("input"); 
      element4.type = "text"; 
      element4.name="price"+ cell2.innerHTML; 

      cell5.appendChild(element4); 


     } 

     function deleteRow(tableID) { 
      try { 
      var table = document.getElementById(tableID); 
      var rowCount = table.rows.length; 

      for(var i=0; i<rowCount; i++) { 
       var row = table.rows[i]; 
       var chkbox = row.cells[0].childNodes[0]; 
       if(null != chkbox && true == chkbox.checked) { 
        table.deleteRow(i); 
        rowCount--; 
        i--; 
       } 


      } 
      }catch(e) { 
       alert(e); 
      } 
     } 
javascript:(document.write(document.forms[0].elements[2].name)) 
var myname2=2; 
    </SCRIPT> 

</BODY> 
</HTML> 

回答

0

你在echo上得到的錯誤/輸出是什麼?

還使用隱藏字段將行數'rowCount'發送到服務器。該行數將幫助你定義循環結束點

添加到HTML

<input type="hidden" id="rowCount"/> 

在JS代碼

<script> 
var objInputFieldRowCount = docuement.getElemendbyId("rowCount"); 
var jsRowCount = table.getElementsByTagName("TR").length; 
objInputFieldRowCount.value = jsRowCount; 
</script> 
1

我用同樣的還挺代碼,我的問題是,當我點擊複選框行號2 ...在Php我總是得到結果作爲行號1複選框被選中哪個是錯誤的...

enter code here 
<html><body> 
<?php 

$col = $_POST['Colname']; 
$dtype = $_POST['Datatype']; 
$pk = $_POST['Pk']; 
$dbname= $_POST['Dbname']; 
$space=$_POST['Space']; 
$concount=$_POST['Concount']; 
$qps=$_POST['Qps']; 


echo "$dbname <br/> $space <br/> $concount <br/> $qps <br/> "; 




$result=count($col); 
echo($result); 


while($result!=0) 
{ 
$result-=1; 

echo "<table><tr>"; 
echo "<td>$col[$result]</td>"; 
echo "<td>$dtype[$result]</td>"; 
echo "<td>$pk[$result]</td>"; 
echo "</tr></table>"; 

} 
?> 

</html></body> 
相關問題