我需要使用serializejqueryajax调用保存具有静态和动态表的表单输入。
该表单有两个动态表和两个用于收据编号和总金额的字段。我需要用保存存款按钮给ajax打电话。需要将mysql数据库中的数据保存为insert。
我试图用savephp文件中的php代码通过post保存一个动态表。我想做的是通过ajax调用完成这个函数。
{
<form id="myform" method="POST">
<div class="panel panel-success">
<div class="panel-heading">
<h4>Savings | Loans Deposits</h4>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<label>Member</label>
<select name="member_id" id="member_id" class="form-control">
<option value="">Select Member</option>
<?php
$query = "SELECT * FROM users_table ORDER BY id ASC";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result))
{
echo '<option value="'.$row["id"].'">'.$row["username"].'</option>';
}
?>
</select>
</div>
</div>
<hr>
<section id="savings">
<hr>
<div class="row">
<div class="col-md-2">
<INPUT class="btn btn-primary" type="button" value="Add Savings Payment" onclick="addRow('dataTable')" />
</div>
<div class="col-md-2">
<INPUT class="btn btn-danger" type="button" value="Delete Selected Payment" onclick="deleteRow('dataTable')" />
</div>
</div>
<hr>
<div class="row">
<div class="col-md-10">
<TABLE class="table table-bordered" id="dataTable">
<TR>
<TD><INPUT class="form-control col-md-2" type="checkbox" name="chk"/></TD>
<TD>
<select name="savings_account_number[]" id="savings_account_number" class="form-control">
<option value="">Select Member</option>
<?php
$query = "SELECT * FROM savings_account_table ORDER BY account_number ASC";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result))
{
echo '<option value="'.$row["account_number"].'">'.$row["account_number"].'</option>';
}
?>
</select>
</TD>
<TD>
<INPUT class="form-control text-right" type="text" name="savings_deposit_amount[]" onkeyup="total()" onkeypress="return isNumber(event)"/>
</TD>
</TR>
</TABLE>
</div>
</div>
<hr>
</section>
<section id="loans">
<hr>
<div class="row">
<div class="col-md-2">
<INPUT class="btn btn-primary" type="button" value="Add Loans Payment" onclick="addRow('loans_table')" />
</div>
<div class="col-md-2">
<INPUT class="btn btn-danger" type="button" value="Delete Selected Payment" onclick="deleteRow('loans_table')" />
</div>
</div>
<hr>
<div class="row">
<div class="col-md-10">
<TABLE class="table table-bordered" id="loans_table" width="350px" border="1">
<TR>
<TD><INPUT class="form-control" type="checkbox" name="chk"/></TD>
<TD>
<label>Select Loans Account</label>
<select name="loans_account_number[]" id="loans_account_number" class="form-control">
<option value="">Select Member</option>
<?php
$query = "SELECT * FROM savings_account_table ORDER BY account_number ASC";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result))
{
echo '<option value="'.$row["account_number"].'">'.$row["account_number"].'</option>';
}
?>
</select>
</TD>
<TD>
<label>Deposit Amount</label>
<INPUT class="form-control text-right" type="text" name="loans_deposit_amount[]"/>
</TD>
</TR>
</TABLE>
</div>
</div>
<hr>
</section>
<div class="row">
<div class="col-md-3">
<label>Receipt Number</label>
<input id="receipt_number" name="receipt_number" value="" class="form-control text-right"/>
</div>
<div class="col-md-3">
<label>Total Amount</label>
<input disabled id="total_amount" name="total_amount" value="0.00" class="form-control text-right"/>
</div>
<div class="col-md-3">
<label>Row Count</label>
<input type="hidden" id="row_count" name="row_count" class="form-control text-right"/>
</div>
<div class="col-md-2">
<label>Save Deposit</label>
<button id="save_deposit" class="btn btn-primary" type="submit">Save Deposit</button>
</div>
</div>
</div>
</div>
</form>
}
我对db的插入如下,它在同一个php文件中
if (isset($_POST['receipt_number'])) {
$user_id = $_SESSION['username'];
$row_count = $_POST['row_count'];
echo $row_count;
$receipt_id = $_POST['receipt_number'];
$savi_account_number = $_POST['savings_account_number'];
$savi_deposit_amount = $_POST['savings_deposit_amount'];
$member_id = $_POST['member_id'];
$create_date = date('Y-m-d H:i:s');
//check for remaining receipt id
$check_item_id = "SELECT receipt_id from savings_transactions_table where receipt_id='$receipt_id' LIMIT 1"; //check if item exist
$resultset = $conn->query($check_item_id);
if ($resultset->num_rows == 0) {
for ($i = 0; $i < $_POST['row_count']; $i++) {
$insert = "INSERT INTO `savings_transactions_table` (`id`, `receipt_id`, `sub_code`, `savings_account`, `user_id`, `amount`, `dr`, `cr`, `actual_balance`, `available_balance`, `created_user`, `created_date`)
VALUES (NULL, '$receipt_id', '1', '" . $_POST['savings_account_number'][$i] . "', '$member_id', '" . $_POST['savings_deposit_amount'][$i] . "', '0', '" . $_POST['savings_deposit_amount'][$i] . "', '0', '0', '$user_id', '$create_date')";
$res = $conn->query($insert);
}
if ($res) {
echo "<script>";
echo "alert('Transactions Posted!')";
echo "</script>";
} else {
if ($conn->error) {
try {
throw new Exception("MySQL error $conn->error <br> Query:<br> $res", $conn->errno);
} catch (Exception $e) {
echo "Error No: " . $e->getCode() . " - " . $e->getMessage() . "<br >";
}
}
}
}
}
1条答案
按热度按时间z3yyvxxp1#
在html末尾添加如下内容:
另外,将处理post的php代码放入另一个名为“ajax insert data.php”的文件中