I am using Codeignator. My HTML output which is correct for me.
Now What I am doing is, the user will enter the name of the medication, no of pills and amount so that based on the amount it will calculate the price and display it. Formula is $single_price=$total_amount/$qty_number;
Above image I added medication name="SIPLA", no of pills=3
and amount=30
. It will calculate it and display the single price=10.00
Everything is working perfectly til now.
Let's talk about the "ADD" button. If any user wants more then one medication then he/she should click on "ADD" button and it will display the same field as above.
I did the same process, I added a medication name="ZOCON 400", no of pills=4
and amount=60
. It calculating and displaying the single price=20.00
which is wrong it should be displayed single price=15.00
.
1) Why I am getting single price=20.00 because it's talking no of pills=3 which is added in the first medication. So it's talking the first one quantity. It should talk no of pill=4
2) The calculation of the single price is also displaying in both fields. first one as well as second one. I need only in the second one.
3) How to submit this data in the database?
Hope you understand my issue.
Code
View
<div class="add_row">
<input type="text" name="medication_name[]" id="medication_name" class="form_control text_cap" placeholder="medication_name">
<span class="value_button" id="decrease" onclick="decreaseValue(1)" value="Decrease Value">-</span>
<input type="number" id="number1" class="qty_number form_control" name="qty_number[]" value="0" class="form_control"/>
<span class="value_button" id="increase" onclick="increaseValue(1)" value="Increase Value">+</span>
<input type="text" class="form_control" name="single_price[]" id="single_price" placeholder="single price" />
<input type="text" class="form_control" name="total_p_price[]" id="total_p_price" placeholder="total price" />
<div class="btn_row add_row_click"> <span> + </span> Add </div>
</div>
Ajax and Js
function increaseValue(n) {
var value = parseInt(document.getElementById('number' + n).value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('number' + n).value = value;
}
function decreaseValue(n) {
var value = parseInt(document.getElementById('number' + n).value, 10);
value = isNaN(value) ? 0 : value;
value < 1 ? value = 1 : '';
value--;
document.getElementById('number' + n).value = value;
}
$(document).ready(function() {
var max_fields = 20; //maximum input boxes allowed
var wrapper = $(".add_row"); //Fields wrapper
var add_button = $(".add_row_click"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div class="custom_fields"><input type="text" name="medication_name[]" id="medication_name'+ x +'" class="form_control text_cap" placeholder="medication_name"><span class="value-button" id="decrease" onclick="decreaseValue('+ x +')" value="Decrease Value">-</span><input type="number" id="number'+ x +'" value="0" name="qty_member[]" /><span class="value-button" id="increase" onclick="increaseValue('+ x +')" value="Increase Value">+</span><br /><input type="text" class="form_control" name="single_price[]" id="single_price'+ x +'" placeholder="single price" /> <input type="text" class="form_control" name="total_p_price[]" id="total_p_price'+ x +'" placeholder="total price" /> <div class="btn_row remove_field"> <span> - </span> Remove </div></div>');
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault();
//$(this).parent('custom_fields').remove();
$(this).closest('.custom_fields').remove();
x--;
})
$("body").on('keyup', 'input[id^=total_p_price]', function() {
//$('input[id^=single_price]').prop('disabled', true);
var total_p_price= $(this).val();
var qty_number = $('input[id^=number]').val();
$.ajax({
type : "POST",
url: baseUrl + "/Customer_control/calculate_total_p_price",
data: {total_p_price: total_p_price,qty_number:qty_number},
cache : false,
success: function(html) {
//alert(html);
$('input[id^=single_price]').val(html);
}
});
});
});
Controller
public function calculate_total_p_price(){
$total_p_price=$this->input->post('total_p_price');
$qty_number=$this->input->post('qty_number');
$single_price=$this->Customer_model->calculate_total_p_price($total_p_price,$qty_number);
echo $single_price;
}
Model
public function calculate_total_p_price($total_p_price,$qty_number){
// print_r($total_p_price);
if(empty($qty_number) || ($qty_number == 0)){
return 0;
}
elseif(empty($total_p_price) || ($total_p_price == 0)){
return 0;
}
elseif(!empty($total_p_price) && (!empty($qty_number) || ($qty_number>0))){
$single_price=$total_p_price/$qty_number;
return number_format((float)$single_price, 2, '.', '');
}
else{return 0;}
}
See Question&Answers more detail:
os