数据表格提交所有记录

时间:2015-11-18 07:45:13

标签: javascript php mysql ajax datatable

我正在使用Jquery数据表来显示表单中的所有员工详细信息。当我提交表单时,它只发布第一页表单值。但我需要发布所有表单值

这是我的代码

jQuery(document).ready(function(){
	jQuery('#emptable').dataTable();
	
});
<link rel="stylesheet" href="http://cdn.datatables.net/1.10.5/css/jquery.dataTables.min.css" type="text/css"  />
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
<form name="emp" method="post">
<table cellpadding="0" cellspacing="0" border="0" class="stdtable" id="emptable">
    <thead>
      <tr>
        <th class="head0">S.No</th>
        <th class="head1">Name</th>
        <th class="head0">Dep Name</th>
        <th class="head1">Comments</th>
      </tr>
    </thead>
    <tr>
    <td>1<input type='hidden' name='serialno' value='1'></td>
    <td>xyz<input type='hidden' name='name[]' value='xyz'></td>
    <td>DE<input type='hidden' name='dep[]' value='DE'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <tr>
    <td>2<input type='hidden' name='serialno' value='2'></td>
    <td>abc<input type='hidden' name='name[]' value='xyz'></td>
    <td>DED<input type='hidden' name='dep[]' value='DED'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <tr>
    <td>3<input type='hidden' name='serialno' value='3'></td>
    <td>adc<input type='hidden' name='name[]' value='adc'></td>
    <td>EDE<input type='hidden' name='dep[]' value='EDE'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <!-- Like all 'N' records will display here -->
    <input type="submit" name="submit" value="submit">
</table> 
</form>

这是我的表单提交代码

<?php 
include_once("includes/dbConnect.inc.php");
if(isset($_POST['submit'])){
$serialno=$_POST['serialno'];
for($i=0; $i< $serialno;)
{
$name=$_POST['name'][$i];
$dep=$_POST['dep'][$i];
$comments=$_POST['comments'][$i];
$query=mysqli_query($con, "INSERT INTO xyz(......remaining code )");
$i++;
}
if($query){echo "Inserted";}
else{echo("Error description: " . mysqli_error($con));}
}
?>

当我运行此代码并提交表单时,只有第一页值(前10行记录)插入到DB中。但我需要将所有表记录插入到DB中,任何人都可以帮助我,我该如何实现这一目标。

2 个答案:

答案 0 :(得分:1)

试试这个。这是正确的解决方案。

HTML:

<link rel="stylesheet" href="http://cdn.datatables.net/1.10.5/css/jquery.dataTables.min.css" type="text/css"  />
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>

<form id="formID" name="emp" method="post">
<table cellpadding="0" cellspacing="0" border="0" class="stdtable" id="emptable">
    <thead>
      <tr>
        <th class="head0">S.No</th>
        <th class="head1">Name</th>
        <th class="head0">Dep Name</th>
        <th class="head1">Comments</th>
      </tr>
    </thead>
    <tr>
    <td>1<input type='hidden' name='serialno[]' value='1'></td>
    <td>xyz<input type='hidden' name='name[]' value='name1'></td>
    <td>DE<input type='hidden' name='dep[]' value='dep1'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <tr>
    <td>2<input type='hidden' name='serialno[]' value='2'></td>
    <td>abc<input type='hidden' name='name[]' value='name2'></td>
    <td>DED<input type='hidden' name='dep[]' value='dep2'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <tr>
    <td>3<input type='hidden' name='serialno[]' value='3'></td>
    <td>adc<input type='hidden' name='name[]' value='name3'></td>
    <td>EDE<input type='hidden' name='dep[]' value='dep3'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <tr>
    <td>3<input type='hidden' name='serialno[]' value='4'></td>
    <td>adc<input type='hidden' name='name[]' value='name4'></td>
    <td>EDE<input type='hidden' name='dep[]' value='dep4'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <tr>
    <td>3<input type='hidden' name='serialno[]' value='5'></td>
    <td>adc<input type='hidden' name='name[]' value='name5'></td>
    <td>EDE<input type='hidden' name='dep[]' value='dep5'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <tr>
    <td>3<input type='hidden' name='serialno[]' value='6'></td>
    <td>adc<input type='hidden' name='name[]' value='name6'></td>
    <td>EDE<input type='hidden' name='dep[]' value='dep6'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <tr>
    <td>3<input type='hidden' name='serialno[]' value='7'></td>
    <td>adc<input type='hidden' name='name[]' value='name7'></td>
    <td>EDE<input type='hidden' name='dep[]' value='dep7'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr><tr>
    <td>3<input type='hidden' name='serialno[]' value='8'></td>
    <td>adc<input type='hidden' name='name[]' value='name8'></td>
    <td>EDE<input type='hidden' name='dep[]' value='dep8'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <tr>
    <td>3<input type='hidden' name='serialno[]' value='9'></td>
    <td>adc<input type='hidden' name='name[]' value='name9'></td>
    <td>EDE<input type='hidden' name='dep[]' value='dep9'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <tr>
    <td>3<input type='hidden' name='serialno[]' value='10'></td>
    <td>adc<input type='hidden' name='name[]' value='name10'></td>
    <td>EDE<input type='hidden' name='dep[]' value='dep10'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <tr>
    <td>3<input type='hidden' name='serialno[]' value='11'></td>
    <td>adc<input type='hidden' name='name[]' value='name11'></td>
    <td>EDE<input type='hidden' name='dep[]' value='dep11'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <tr>
    <td>3<input type='hidden' name='serialno[]' value='12'></td>
    <td>adc<input type='hidden' name='name[]' value='name12'></td>
    <td>EDE<input type='hidden' name='dep[]' value='dep11'></td>
    <td><textarea name="comments[]"></textarea></td>
    </tr>
    <!-- Like all 'N' records will display here -->
    <input type="submit" name="submit" value="submit">
</table> 
</form>

JavaScript:

<script>
    jQuery(document).ready(function(){
        var oTable = jQuery('#emptable').dataTable();

        $( "#formID" ).submit(function( event ) {           
            $.ajax({
                type: "POST",
                url: "post.php",                
                data: oTable.$('input, textarea').serialize(),
                success: function(response){
                   alert(response);
                }
            });
            event.preventDefault(); // Prevents default form submit
        });    
    });
</script>

PHP(post.php):

<?php 

    echo "<pre>"; print_r($_POST); 

?>

希望这会有所帮助。

答案 1 :(得分:0)

试试这个:

 <?php 
    include_once("includes/dbConnect.inc.php");
    if(isset($_POST['submit'])){
  //  $serialno=$_POST['serialno'];
    for($i=0; $i< 1000)
    {
    $name=$_POST['name'][$i];
    $dep=$_POST['dep'][$i];
    $comments=$_POST['comments'][$i];
    $query=mysqli_query($con, "INSERT INTO xyz(......remaining code )");
    $i++;
    }
    if($query){echo "Inserted";}
    else{echo("Error description: " . mysqli_error($con));}
    }
    ?>