2012-02-07 97 views
0

我有一個表格,其中包含一個可以通過單擊添加按鈕動態添加行的部分。和往常一樣,爲什麼我要來這裏將添加的行添加到數據庫中時遇到問題。我的意思是,動態添加行的值不會被保存到數據庫中,只會添加第一行的值。如何將動態添加的行的值存入數據庫?

這是腳本:

編輯:

<script language="Javascript"> 
var i=1; 
function addRowToTable() 
{ 


i++; 
    m.r.value = i; 
    var tbl = document.getElementById('table'); 
    var lastRow = tbl.rows.length; 
    var iteration = lastRow; 
    var row = tbl.insertRow(lastRow); 

    var cellLeft = row.insertCell(0); 
    var textNode = document.createTextNode(iteration); 
    cellLeft.appendChild(textNode); 

    var cellRightSel1 = row.insertCell(1); 
    var sel = document.createElement('select'); 
    sel.name = 'name' + iteration; 
    sel.setAttribute("onchange", "choosec(this);");  
    var item = new Option("",""); 
    sel.options[sel.length] = item; 
    <? 
    while($data = mysql_fetch_array($result)){          
    ?> 
    var item = new Option("<?=$data["Name"];?>","<?=$data["ID"];?>"); 
    sel.options[sel.length] = item; 
    <? } ?> 
    cellRightSel1.appendChild(sel); 

    var cellRightSel2 = row.insertCell(2); 
    var sel = document.createElement('select'); 
    sel.name = 'class' + iteration; 
    sel.setAttribute("onchange", "choosepoint(this);");  
    var item = new Option ("",""); 
    sel.options[sel.length] = item; 
    <? 
    while($data = mysql_fetch_array($result_sub)){          
    ?> 
    var item = new Option("<?=$data["Class"];?>","<?=$data["ID"];?>"); 
    sel.options[sel.length] = item; 
    <? } ?> 
    cellRightSel2.appendChild(sel); 

    var cellRight = row.insertCell(3); 
    var div = document.createElement('div'); 
    div.id = 'point' + iteration; 
    cellRight.appendChild(div); 
} 
    </script> 

<form id="m" name="m" method="POST" action="submit.php"> 
<input type="hidden" name="menu_id" value="<?=$menu_id;?>"> 
<input type="hidden" name="r" id="r"> 
<table align="center" class="table"> 
    <tr> 
    <tr> 
     <td class="tablesubtitle">Adjustment</td> 
     <td class="tablesubtitle"><input type="text" name="adjustment" size="2"></td> 
     </td> 
    </tr> 
    <tr> 
     <td class="tablesubtitle">Component</td> 
     <td class="tablesubtitle"> 
     <table id="table"> 
    <tr> 
     <td>No</td> 
     <td>Name</td> 
     <td>Class</td> 
     <td>Point</td> 
     <td>Action</td> 
    </tr> 
    <tr> 
      <td>1</td> 
     <td><select name="name"> 
      <option value="#">&nbsp;</option> 
      <?php 
        opendb(); 
       $query = "SELECT * FROM student"; 
       $result = mysql_query($query); 
       while($data = mysql_fetch_array($result)){ 
        $ID = $data['ID']; 
        $Name = $data['Name']; 
        echo "<option value='$ID'>$Name</option>"; 
       } 
      ?> 
      </select> 
     </td> 
     <td><select name='class' onchange="choosepoint(this);"> 
      <option value="#">&nbsp;</option> 
      <?php 
        opendb(); 
       $query = "SELECT * FROM student2"; 
       $result = mysql_query($query); 
       while($data = mysql_fetch_array($result)){ 
         $ID1 = $data['ID']; 
        $class = $data['Class']; 
        echo "<option value='$ID'>$class</option>"; 
       } 
      ?> 
      </select> 
     </td> 
     <td> 
       <div id="point" name="point"></div> 
      </td> 
      <td><input type="button" value=" + " onClick="addRowToTable();"> | <input type="button" value=" - " onClick="removeRowFromTable();"></td> 
    </tr> 
</table>  
     </td> 
     <tr> 
     <td></td> 
     <td align="center"><input type="submit" value="Submit"></td> 
     </tr> 
    </tr> 
</table> 
</form> 

Submit.php

<?php 
include ("c/config.php"); 
include ("c/db.php"); 

opendb(); 
$query1 = "select * from student where ID='$name'"; 
$result1 = querydb($query1); 
$data = mysql_fetch_array($result1); 
    $adjustment = $data['adjustment']; 

$num = $_POST['r']; 
//echo $num; 
for($i=0; $i<=$num; $i++){ 
if(isset($_REQUEST['name$i'])){ 
    $name = $_REQUEST['name$i']; 
} 
if(isset($_REQUEST['class$i'])){ 
    $class = $_REQUEST['class$i']; 
} 
if(isset($_REQUEST['point$i'])){ 
    $point = $_REQUEST['point$i']; 
} 

$query2 = "INSERT INTO student3 
      VALUES('$adjustment','$name','$class','$point')"; 
$result2 = querydb($query2); 
} 
closedb(); 
?> 
+0

。在你的代碼中沒有插入或更新語句。另外這裏的代碼太多了。減少代碼量,只發布給你帶來麻煩的部分。 – Dan 2012-02-07 08:18:10

+0

呃,我忘記了,我很抱歉。 – candies 2012-02-07 08:33:57

回答

0

我發現了存在的問題。 這是我最新的代碼:

var i=0; 
function addRowToTable() 
{ 
i++; 
    m.r.value = i; 
    var tbl = document.getElementById('table'); 
    var lastRow = tbl.rows.length; 
    var iteration = lastRow - 1; 
    var row = tbl.insertRow(lastRow); 

    var cellLeft = row.insertCell(0); 
    var textNode = document.createTextNode(iteration); 
    cellLeft.appendChild(textNode); 

    var cellRightSel1 = row.insertCell(1); 
    var sel = document.createElement('select'); 
    sel.name = 'name' + iteration; 
    sel.setAttribute("onchange", "choosec(this);");  
    var item = new Option("",""); 
    sel.options[sel.length] = item; 
    <? 
    while($data = mysql_fetch_array($result)){          
    ?> 
    var item = new Option("<?=$data["Name"];?>","<?=$data["ID"];?>"); 
    sel.options[sel.length] = item; 
    <? } ?> 
    cellRightSel1.appendChild(sel); 

    var cellRightSel2 = row.insertCell(2); 
    var sel = document.createElement('select'); 
    sel.name = 'class' + iteration; 
    sel.setAttribute("onchange", "choosepoint(this);");  
    var item = new Option ("",""); 
    sel.options[sel.length] = item; 
    <? 
    while($data = mysql_fetch_array($result_sub)){          
    ?> 
    var item = new Option("<?=$data["Class"];?>","<?=$data["ID"];?>"); 
    sel.options[sel.length] = item; 
    <? } ?> 
    cellRightSel2.appendChild(sel); 

    var cellRight = row.insertCell(3); 
    var div = document.createElement('div'); 
    div.id = 'point' + iteration; 
    cellRight.appendChild(div); 
} 
    </script> 

<form id="m" name="m" method="POST" action="submit.php"> 
<input type="hidden" name="menu_id" value="<?=$menu_id;?>"> 
<input type="hidden" name="r" id="r"> 
<table align="center" class="table"> 
    <tr> 
    <tr> 
     <td class="tablesubtitle">Adjustment</td> 
     <td class="tablesubtitle"><input type="text" name="adjustment" size="2"></td> 
     </td> 
    </tr> 
    <tr> 
     <td class="tablesubtitle">Component</td> 
     <td class="tablesubtitle"> 
     <table id="table"> 
    <tr> 
     <td>No</td> 
     <td>Name</td> 
     <td>Class</td> 
     <td>Point</td> 
     <td>Action</td> 
    </tr> 
    <tr> 
      <td>1</td> 
     <td><select name="name"> 
      <option value="#">&nbsp;</option> 
      <?php 
        opendb(); 
       $query = "SELECT * FROM student"; 
       $result = mysql_query($query); 
       while($data = mysql_fetch_array($result)){ 
        $ID = $data['ID']; 
        $Name = $data['Name']; 
        echo "<option value='$ID'>$Name</option>"; 
       } 
      ?> 
      </select> 
     </td> 
     <td><select name='class' onchange="choosepoint(this);"> 
      <option value="#">&nbsp;</option> 
      <?php 
        opendb(); 
       $query = "SELECT * FROM student2"; 
       $result = mysql_query($query); 
       while($data = mysql_fetch_array($result)){ 
         $ID1 = $data['ID']; 
        $class = $data['Class']; 
        echo "<option value='$ID'>$class</option>"; 
       } 
      ?> 
      </select> 
     </td> 
     <td> 
       <div id="point" name="point"></div> 
      </td> 
      <td><input type="button" value=" + " onClick="addRowToTable();"> | <input type="button" value=" - " onClick="removeRowFromTable();"></td> 
    </tr> 
</table>  
     </td> 
     <tr> 
     <td></td> 
     <td align="center"><input type="submit" value="Submit"></td> 
     </tr> 
    </tr> 
</table> 
</form> 

<?php 
include ("c/config.php"); 
include ("c/db.php"); 

opendb(); 
$query1 = "select * from student where ID='$name'"; 
$result1 = querydb($query1); 
$data = mysql_fetch_array($result1); 
    $adjustment = $data['adjustment']; 

$num = $_POST['r']; 
//echo $num; 
for($i=0;$i<=$num;$i++){ 
if(isset($_REQUEST['name$i'])){$name = $_REQUEST['name$i'];} 
if(isset($_REQUEST['class$i'])){$class = $_REQUEST['class$i'];} 
if(isset($_REQUEST['point$i'])){$point = $_REQUEST['point$i'];} 

$query2 = "INSERT INTO student3 
      VALUES('$adjustment','$name','$class','$point')"; 
$result2 = querydb($query2); 
} 
closedb(); 
?> 
0

我會使用jQuery和給該行中每個表單元素一個唯一的ID,然後調用

addRowToTable('234') //or whatever the id is 

the n的addRowToTable(ID)功能

$('form_element'+id).val(); 

發送給jQuery的$。員額

+0

謝謝邁克爾的建議,但我不明白Jquery,雖然它看起來更容易,我會學習它。 無論如何,我找到了答案。 謝謝。 – candies 2012-02-07 20:13:02

相關問題