编辑jQuery的PHP添加多个文本框,并通过AJAX给他们使用序列化形式到PHP
现在我可以在网络选项卡上看到错误:
注意:未定义指数:medication_id用C :\ wamp64 ... \ addMedInfo.php 上线8
我创建了一个表格,我一次指定药物名称和失效日期,然后添加所有具有相同名称和相同失效日期的药物,但当然要使用不同的条形码,如下面的代码片段所示:
$(document).ready(function()
{
$("button.clone").on("click", clone);
$("button.remove").on("click", remove);
$("#sub").on('submit', function()
{
$.ajax({
url: '../php/addMedInfo.php',
type: 'post',
data: send_data.serialize(),
dataType: 'TEXT',
success:function(resp)
{
},
error:function(resp)
{
console.log(resp);
}
})
})
});
var regex = /^(.+?)(\d+)$/i;
function clone() {
var cloneIndex = $(".clonedInput").length;
$(".rounded").find("#clonedInput1").clone().insertAfter(".clonedInput:last").attr("id", "clonedInput" + (cloneIndex+1));
}
function remove() {
$(".rounded").find(".clonedInput:last").remove();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row justify-content-center">
<div class="col-sm-12 ">
<form name="send_data">
<div class="col-sm-6">
<label for="medication_id">Medication</label>
<fieldset class="form-group">
<select class="form-control select" name="medication_id" id="medication_id">
<option value="select">Select</option>
<?php foreach($getExecGetMedications as $res) { ?>
<option value="<?php echo $res['med_id'] ?>"><?php echo $res['med_name'] ?></option>
<?php } ?>
</select>
</fieldset>
<!-- End class="col-sm-6" -->
</div>
<div class="col-sm-6">
<label for="expiry_date">Expiry Date</label>
<fieldset class="form-group">
<input type="date" class="form-control" name="expiry_date" id="expiry_date">
</fieldset>
<!-- End class="col-sm-6" -->
</div>
</div>
</div>
<div class="col-sm-6 rounded" style="background-color: #D3D3D3">
<div class="row clonedInput" id="clonedInput1">
<div class="col-sm-3">
<label for="barcode">barcode</label>
<fieldset class="form-group">
<input type="text" class="form-control" name="barcode" id="barcode">
</fieldset>
<!-- End class="col-sm-6" -->
</div>
<div class="col-sm-3">
<label for="medication_quantity">Nbr of tablets</label>
<fieldset class="form-group">
<input type="number" class="form-control" name="medication_quantity" id="medication_quantity">
</fieldset>
<!-- End class="col-sm-6" -->
</div>
<div class="col-sm-3">
<label for="medication_pill">Nbr of Pills</label>
<fieldset class="form-group">
<input type="number" class="form-control" name="medication_pill" id="medication_pill">
</fieldset>
<!-- End class="col-sm-6" -->
</div>
<!-- End class="col-sm-6" -->
</form>
</div>
<div class="actions pull-right">
<button class="btn btn-danger clone">Add More</button>
<button class="btn btn-danger remove">Remove</button>
</div>
<!-- End class="col-sm-4" -->
</div>
<button class="btn btn-danger" type="Submit" id="sub">Submit</button>
</form>
其实我可以添加尽可能多的,因为我需要的文本框,所以如果我有5种药物具有相同的名称和到期日期,我可以添加4个格,然后用添加条码片剂和药片的数量,并点击sumbit按钮将它们发送到我的数据库:
<?php
error_reporting(E_ALL);
ini_set('display_error', 1);
require_once('../php/connection.php');
$clinic_id = $_SESSION['clinic_id'];
$medication_id = $_POST['medication_id'];
$expiry_date = $_POST['expiry_date'];
$barcode = $_POST['barcode'];
$medication_quantity = $_POST['medication_quantity'];
$medication_pill = $_POST['medication_pill'];
$addMed = "INSERT INTO med_pharmacy(med_id, med_barcode, med_received, med_expiry,
med_tablet, med_pill, clinic_id)
VALUES(:med_id, :med_barcode, :med_received, :med_expiry, :med_tablet, :med_pill, :clinic_id)";
$execAddMed = $conn->prepare($addMed);
$execAddMed->bindValue(':med_id', $medication_id);
$execAddMed->bindValue(':med_barcode', $barcode);
$execAddMed->bindValue(':med_received', now('Y-m-d H:i:s'));
$execAddMed->bindValue(':med_expiry', $expiry_date);
$execAddMed->bindValue(':med_tablet', $medication_quantity);
$execAddMed->bindValue(':med_pill', $medication_pill);
$execAddMed->bindValue(':clinic_id', $clinic_id);
$execAddMed->execute();
?>
下面是截图:
现在的问题是,当我点击提交按钮,没有任何反应,没有添加到数据库中,并在控制台上,并在网络选项卡中显示没有错误的开发工具。
你的提交按钮必须在里面'
正如你可以看到它的形式是内部的,但无论如何,我改变了它的位置更靠近顶部,但它没有工作,以及 – droidnation
我看到你在代码中有2个'',删除顶部的那个 – Patrick2607