使用 jquery 和 php codeigniter 插入多行药物
inserting multiple row of medicine using jquery and php codeigniter
如果我想添加点击按钮而不是打开下一个药物输入字段而不是填充并插入下一行如果一次 5 次药物输入而不是 5 次插入数据库,我想一次添加药物插入行。但是这里只有一次将数据插入数据库 复选框不起作用 只接受第一个值
查看页面
我需要数据库
name quantity days take medician
----------------------------------------------
Amit 1 2 morning,evening
-----------------------------------------------
Amitabh 2 3 evening,night
-----------------------------------------------
Amitabh 3 4 afternoon,night
kumar
gupta
正在插入当前数据库
name quantity days take medician
----------------------------------------------
Amit 1 2 morning
-----------------------------------------------
html 查看页面
<tbody>
<?php foreach ($app_booking as $row){ ?>
<input type="hidden" name="doctorname" id="doctorname" value="<?php echo $row["doctor_name"];?>">
<input type="hidden" id="doctorid" name="doctorid" value="<?php echo $row["doctor_id"];?>">
<input type="hidden" id="appid" name="appid" value="<?php echo $row["appointment_id"];?>">
<input type="hidden" id="uid" name="uid" value="<?php echo $row["user_id"];?>">
<?php };?>
<tr>
<td><input class="form-control" type="text" name="name" id="name" required=""></td>
<td><input class="form-control" type="text" id="quantity" name="quantity" required=""></td>
<td><input class="form-control" type="text" name="days" id="days" required=""></td>
<td>
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" id="morning" value="Morning"> Morning
</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" id="afternoon" value="Afternoon"> Afternoon
</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" id="evening" value="Evening"> Evening
</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" value="Night" id="night"> Night
</label>
</div>
</td>
<td>
<a href="#" name="add" id="btn1" value="Add" class="btn bg-success-light"><i class="fas fa-plus-circle"></i> Add Item</a>
</td>
<button type="button" onclick="save_medical_records();" class="btn btn-primary submit-btn">Save</button>
</tr>
</tbody>
ajax代码
function save_medical_records()
{
var doctorname = $("input[name='doctorname']").val();
var doctorid = $("input[name='doctorid']").val();
var appid = $("input[name='appid']").val();
var uid = $("input[name='uid']").val();
var name = $("input[name='name']").val();
var quantity = $("input[name='quantity']").val();
var days = $("input[name='days']").val();
const take_meds = $("[name='take_medicine[]']:checked").map(function() { return this.value}).get()
console.log(take_meds);
$.ajax({
url:"<?php echo base_url() ?>add-prescription",
data:"doctorname="+doctorname+"&doctorid="+doctorid+"&appid="+appid+"&uid="+uid+"&name="+name+"&quantity="+quantity+"&days="+days+"&take_meds="+take_meds,
//data: $("[name=doctorname]").serialize(),$("[name=doctorid]").serialize(),$("[name=appid]").serialize(),$("[name=uid]").serialize(),$("[name=name]").serialize(),$("[name=quantity]").serialize(),$("[name=days]").serialize(),$("[name=take_meds]").serialize(),
type:"post",
success:function(response){
if(response==1)
$("#msg").css("display","block");
}
});
}
控制器
public function add_prescription()
{
$result = $this->doctor_health_model->add_prescription();
echo $result;
}
型号
public function add_prescription()
{
$db2 = $this->load->database('dpr',TRUE);
$medicine = $this->input->post('name');
$uid = $this->input->post('uid');
$appid = $this->input->post('appid');
$doctor_id = $this->input->post('doctorid');
$doctor_name = $this->input->post('doctorname');
$quantity = $this->input->post('quantity');
$days = $this->input->post('days');
$take_medicine = $this->input->post('take_medicine');
$today_date = date("Y-m-d");
$insert =$db2->query('INSERT INTO dpr_save_farmacytest (medicine,user_id,appointment_id,doctor_id,doctor_name,quantity,days,take_medicine_time,created_date)
VALUES ("'.$medicine.'","'.$uid.'","'.$appid.'","'.$doctor_id.'","'.$doctor_name.'","'.$quantity.'","'.$days.'","'.$take_medicine.'","'.$today_date.'")');
//echo $db2->last_query();
return $insert;
}
}
除了 SQL 注入问题,在一个 table 字段中存储多个值的问题等,您的直接问题由以下人员回答:
这就是 jQuery 的工作方式 - 这将 return 一个值,来自第一个具有相同名称的元素
var take_medicine = $("input[name='take_medicine[]']").val();
您需要获取每个选中的复选框 - 例如
const take_meds = $("[name='take_medicine[]']:checked")
.map(function() { return this.value}).get();
console.log(take_meds);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-check form-check-inline">
<label class="form-check-label"><input class="form-check-input checkbox_value" checked type="checkbox" name="take_medicine[]" id="morning" value="Morning"> Morning</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label"><input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" id="afternoon" value="Afternoon"> Afternoon</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label"><input class="form-check-input checkbox_value" checked type="checkbox" name="take_medicine[]" id="evening" value="Evening"> Evening</label>
</div>
如果你使用serialize
,你的生活可能会更简单
如果您有 <form id="myForm"
- 您将整个 AJAX 代码更改为
$("#myForm").on("submit", function(e) {
e.preventDefault();
$.ajax({
url: "<?php echo base_url() ?>add-prescription",
data: $(this).serialize(),
type: "post",
success: function(response) {
if (response == 1) $("#msg").css("display", "block");
},
error: function(jxhr) {
console.log("Error", jxhr)
}
})
});
如果我想添加点击按钮而不是打开下一个药物输入字段而不是填充并插入下一行如果一次 5 次药物输入而不是 5 次插入数据库,我想一次添加药物插入行。但是这里只有一次将数据插入数据库 复选框不起作用 只接受第一个值
查看页面
我需要数据库
name quantity days take medician
----------------------------------------------
Amit 1 2 morning,evening
-----------------------------------------------
Amitabh 2 3 evening,night
-----------------------------------------------
Amitabh 3 4 afternoon,night
kumar
gupta
正在插入当前数据库
name quantity days take medician
----------------------------------------------
Amit 1 2 morning
-----------------------------------------------
html 查看页面
<tbody>
<?php foreach ($app_booking as $row){ ?>
<input type="hidden" name="doctorname" id="doctorname" value="<?php echo $row["doctor_name"];?>">
<input type="hidden" id="doctorid" name="doctorid" value="<?php echo $row["doctor_id"];?>">
<input type="hidden" id="appid" name="appid" value="<?php echo $row["appointment_id"];?>">
<input type="hidden" id="uid" name="uid" value="<?php echo $row["user_id"];?>">
<?php };?>
<tr>
<td><input class="form-control" type="text" name="name" id="name" required=""></td>
<td><input class="form-control" type="text" id="quantity" name="quantity" required=""></td>
<td><input class="form-control" type="text" name="days" id="days" required=""></td>
<td>
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" id="morning" value="Morning"> Morning
</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" id="afternoon" value="Afternoon"> Afternoon
</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" id="evening" value="Evening"> Evening
</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" value="Night" id="night"> Night
</label>
</div>
</td>
<td>
<a href="#" name="add" id="btn1" value="Add" class="btn bg-success-light"><i class="fas fa-plus-circle"></i> Add Item</a>
</td>
<button type="button" onclick="save_medical_records();" class="btn btn-primary submit-btn">Save</button>
</tr>
</tbody>
ajax代码
function save_medical_records()
{
var doctorname = $("input[name='doctorname']").val();
var doctorid = $("input[name='doctorid']").val();
var appid = $("input[name='appid']").val();
var uid = $("input[name='uid']").val();
var name = $("input[name='name']").val();
var quantity = $("input[name='quantity']").val();
var days = $("input[name='days']").val();
const take_meds = $("[name='take_medicine[]']:checked").map(function() { return this.value}).get()
console.log(take_meds);
$.ajax({
url:"<?php echo base_url() ?>add-prescription",
data:"doctorname="+doctorname+"&doctorid="+doctorid+"&appid="+appid+"&uid="+uid+"&name="+name+"&quantity="+quantity+"&days="+days+"&take_meds="+take_meds,
//data: $("[name=doctorname]").serialize(),$("[name=doctorid]").serialize(),$("[name=appid]").serialize(),$("[name=uid]").serialize(),$("[name=name]").serialize(),$("[name=quantity]").serialize(),$("[name=days]").serialize(),$("[name=take_meds]").serialize(),
type:"post",
success:function(response){
if(response==1)
$("#msg").css("display","block");
}
});
}
控制器
public function add_prescription()
{
$result = $this->doctor_health_model->add_prescription();
echo $result;
}
型号
public function add_prescription()
{
$db2 = $this->load->database('dpr',TRUE);
$medicine = $this->input->post('name');
$uid = $this->input->post('uid');
$appid = $this->input->post('appid');
$doctor_id = $this->input->post('doctorid');
$doctor_name = $this->input->post('doctorname');
$quantity = $this->input->post('quantity');
$days = $this->input->post('days');
$take_medicine = $this->input->post('take_medicine');
$today_date = date("Y-m-d");
$insert =$db2->query('INSERT INTO dpr_save_farmacytest (medicine,user_id,appointment_id,doctor_id,doctor_name,quantity,days,take_medicine_time,created_date)
VALUES ("'.$medicine.'","'.$uid.'","'.$appid.'","'.$doctor_id.'","'.$doctor_name.'","'.$quantity.'","'.$days.'","'.$take_medicine.'","'.$today_date.'")');
//echo $db2->last_query();
return $insert;
}
}
除了 SQL 注入问题,在一个 table 字段中存储多个值的问题等,您的直接问题由以下人员回答:
这就是 jQuery 的工作方式 - 这将 return 一个值,来自第一个具有相同名称的元素
var take_medicine = $("input[name='take_medicine[]']").val();
您需要获取每个选中的复选框 - 例如
const take_meds = $("[name='take_medicine[]']:checked")
.map(function() { return this.value}).get();
console.log(take_meds);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-check form-check-inline">
<label class="form-check-label"><input class="form-check-input checkbox_value" checked type="checkbox" name="take_medicine[]" id="morning" value="Morning"> Morning</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label"><input class="form-check-input checkbox_value" type="checkbox" name="take_medicine[]" id="afternoon" value="Afternoon"> Afternoon</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label"><input class="form-check-input checkbox_value" checked type="checkbox" name="take_medicine[]" id="evening" value="Evening"> Evening</label>
</div>
如果你使用serialize
,你的生活可能会更简单如果您有 <form id="myForm"
- 您将整个 AJAX 代码更改为
$("#myForm").on("submit", function(e) {
e.preventDefault();
$.ajax({
url: "<?php echo base_url() ?>add-prescription",
data: $(this).serialize(),
type: "post",
success: function(response) {
if (response == 1) $("#msg").css("display", "block");
},
error: function(jxhr) {
console.log("Error", jxhr)
}
})
});