Reputation: 85
I want to add medicine one time one row inserting if i want to add click button than open next medicine entry field than fillup and insert next row if one time 5 medicine entry than 5 time insert in to database .but here only one time insert data in to database checkbox is not working only accept first value
view page
i have need database
name quantity days take medician
----------------------------------------------
Amit 1 2 morning,evening
-----------------------------------------------
Amitabh 2 3 evening,night
-----------------------------------------------
Amitabh 3 4 afternoon,night
kumar
gupta
inserting curent database
name quantity days take medician
----------------------------------------------
Amit 1 2 morning
-----------------------------------------------
html view page
<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 code
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");
}
});
}
controllers
public function add_prescription()
{
$result = $this->doctor_health_model->add_prescription();
echo $result;
}
models
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;
}
}
Upvotes: 0
Views: 864
Reputation: 177860
Apart from the SQL Injection issues, the problem storing multiple values in one table field etc, your immediate question is answered by:
That is how jQuery works - this will return ONE value, from the FIRST of the elements with the same name
var take_medicine = $("input[name='take_medicine[]']").val();
You need to get each checked checkbox - for example
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>
You could possibly have a simpler life if you use serialize
IF You have <form id="myForm"
- you change the WHOLE AJAX code to
$("#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)
}
})
});
Upvotes: 1