I am newbie in Codeigniter so I don't know how to do this.
Actually my view page has five rows which I generated through for loop in which each row contain two select boxes and two input boxes. I don't know how to get these values first in jQuery and then pass into controller. Actually I know how to grab values from a simple textboxes but because I generate them in a loop and give them a separate id, so I don't know how to get the values in jQuery and then pass to the controller.
This is my view
<tr>
<th>Category:</th>
<th>Items:</th>
<th>Selling Price:</th>
<th>quantity:</th>
</tr>
<?php for ($i = 0; $i < 5; $i++) {
?>
<tr> //Categories
<td><?php echo form_dropdown('cat_id', $records2, '#',
"id='category_".$i."' onchange='getItems(this.value,".$i.")' ");?>
</td>
<!-- Items -->
<td>form_dropdown('item_id', $records3, '#', "id='items_".$i."'"); ?> </td>
<!-- end of Items -->
<td> <?php echo form_input($price); ?></td>
<td><?php echo form_input($quantity); }?></td>
The item options is based on the categories selection, means if I select some option from categories, then the items against categories show in a second select box.
Here is my Javascript for functionality of top two select boxes
<script type="text/javascript">
function getItems(category_id,index) {
$("#items_" + index + " > option").remove();
$.ajax({
type: "POST",
url: "stockInController/get_Items/"+category_id,
success: function(items)
{
$.each(items,function(item_id,item_name)
{
var opt = $('<option />');
opt.val(item_id);
opt.text(item_name);
$('#items_'+ index).append(opt);
});
}
});
}
</script>
Now the problem is here; I want to get the values from all the rows and then pass into controller.
This is the javascript for catching and then sending values to controller
<script type="text/javascript">
$('#btns').click(function() { // $("#form").serialize()
Here, I am not doing right, first I don't know how to get values of item and categories here and then the other thing is how to be able to get multiple textbox values because there are five rows, and the textvalues in which values are filled are to be sent
var item_id = $('#items').val();
var item_id = $('#items').val();
var price = $('#price').val();
var quantity = $('#quantity').val();
var form_data = {
cat_id: $('#cat_id').val(),
quantity: $('#quantity').val(),
item_id: $('#items').val(),
};
$.ajax({
url: "<?php echo site_url('salesController/addSales'); ?>",
type: 'POST',
data: form_data,
dataType: 'json',
success: function(msg) {
if(msg.res == 1)
{
alert("true");
}
else{
alert("false");
}
}
});
return false;
});
</script>
Here is my controller and I don't know to get multiple values here also.
Here is the code of what I am doing, I know I am wrong, but I don't know how to make it right
$data = array(
'cat_id' => $this->input->post('cat_id'),
'item_id' => $this->input->post('item_id'),
'price' => $this->input->post('sales_date'),
'quantity' => $this->input->post('sales_bill_no'),
);