如何将动态添加的行的值放入数据库?

时间:2012-02-07 08:08:57

标签: php javascript html row add

我有一个表格,其中包含可以通过单击“添加”按钮动态添加行的部分。像往常一样,为什么我来这里是在将添加的行中的这些值添加到数据库中时遇到问题。我的意思是,动态添加的行上的值不会保存到数据库中,只会添加第一行的值。

这是剧本:

编辑:

<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();
?>

2 个答案:

答案 0 :(得分:0)

我会使用jquery并为行中的每个表单元素提供一个唯一的id然后调用

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

然后在addRowToTable(id)函数

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

将此发送到jquery $ .post

答案 1 :(得分: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();
?>