我正在使用Bootstrap Dynamic表单。但是数据没有插入数据库。 Emp_id是员工ID。 leave_id是员工从其他表中获取的当前离开id。 emp_id和amp;对于每次插入数据,leave_id都是相同的。
请帮我解决这个问题。 提前谢谢。
HTML
<!--Alernative Staff Starts-->
<form id="alernativeForm" name="alernativeForm" role="form" data-leave-id="'.$leaves['id'].'">
<table class="table table-bordered" id="dynamic_field">
<thead>
<tr>
<td><strong>Day</strong></td>
<td><strong>Period</strong></td>
<td><strong>Name of alernate Teacher/Staff</strong></td>
<td><strong>Add More or Delete</strong></td>
</tr>
</thead>
<tbody id="add_name" name="add_name">
<tr>
<td><input type="text" class="form-control day_list" name="day[]" id="day" placeholder="Day"></td>
<td><input type="text" class="form-control period_list" name="period[]" id="period" placeholder="Period"></td>
<td><input type="text" class="form-control teacher_list" name="teacher[]" id="teacher" placeholder="Name of alernate Teacher/Staff"></td>
<td><a class="btn btn-primary" name="add" id="add" role="button">Add More</a></td>
</tr>
</tbody>
</table>
</form>
<!--Alernative Staff Ends-->
我的jQuery&amp; AJAX
/*Dynamic Field Starts*/
var i = 1;
$("#add").click(function(){
i++;
$("#dynamic_field").append('<tr id="row'+i+'"><td><input type="text" class="form-control day_list" name="day[]" id="day" placeholder="Day"></td><td><input type="text" class="form-control period_list" name="period[]" id="period" placeholder="Period"></td><td><input type="text" class="form-control teacher_list" name="teacher[]" id="teacher" placeholder="Name of alernate Teacher/Staff"></td><td><a class="btn btn-danger btn-remove" name="remove" id="'+i+'" role="button">X</a></td></tr>')
});
$(document).on('click', '.btn-remove', function(){
var btn_remove_id = $(this).attr('id');
$('#row'+btn_remove_id+'').remove();
});
$("#alernativeSubmit").click(function(){
var dynamic_field = $("#alernativeForm").serialize();
var leave_id = $('#alernativeForm').attr('data-leave-id');
var emp_id = <?php echo $userRow['emp_id'];?>;
alert($('#alernativeForm').serialize() + "&emp_id=" + emp_id + "&leave_id=" + leave_id)
$.ajax({
url:"core/name.php",
method:"POST",
cache: false,
data: $('#alernativeForm').serialize() + "&emp_id=" + emp_id + "&leave_id=" + leave_id,
success:function(data)
{
alert(data)
$('#alernativeForm')[0].reset();
$('#alernateStaff').modal('hide');
}
});
});
/*Dynamic Field Ends*/
PHP
<?php
session_start();
include_once 'dbconnect.php';
$day_number = count($_POST['day']);
$period_number = count($_POST['period']);
$teacher_number = count($_POST['teacher']);
$emp_id = $_POST['emp_id'];
$leave_id = $_POST['leave_id'];
if($day_number > 0)
{
for($i=0; $i<$day_number; $i++)
{
if(trim($_POST["day"][$i] != '') && trim($_POST["period"][$i] != '') && trim($_POST["teacher"][$i] != '') && trim($_POST["emp_id"] != '') && trim($_POST["leave_id"] != ''))
{
mysql_query("INSERT INTO alernate(`emp_id`,`leave_id`,`day`,`period`,`teacher`) VALUES('".mysql_real_escape_string($_POST["emp_id"])."', '".mysql_real_escape_string($_POST["leave_id"])."', '".mysql_real_escape_string($_POST["day"][$i])."', '".mysql_real_escape_string($_POST["period"][$i])."', '".mysql_real_escape_string($_POST["teacher"][$i])."')");
echo "Data Inserted";
}
}
}
?>