John Davidson

jquery - getting the value of column inside a table using ajax in php codeigniter

0 comments
Message:


I have a simple shopping website in codeigniter in which I have an sql table like below:



So whenever a user selects a product size, I want to show how many pieces are left, so I took the following approach for this code:




function checkstock() {

var stockid = $(".stockid").val();
var idp = $("#idp").val();
var transfer = [stockid, idp];
$.ajax({
type: "post",
url: "<?php echo base_url(); ?>index.php/homecontroller/checkstock",
data: {
result: JSON.stringify(transfer)
},
success: function(response) {
if (response) {
$('#msg').html('<span style="color: green;">ONLY ' + stockid + "</span>");

}
}
});
}

<input id="idp" name="prodid" value="<?php echo $product->id;?>" type="hidden">
<input class="stockid" type="radio" onclick="checkstock();" name="size" value="xs">
<input class="stockid" type="radio" onclick="checkstock();" name="size" value="s">
<input class="stockid" type="radio" onclick="checkstock();" name="size" value="m">


My Answer:
("#idp").val();__ $.ajax({__ url: "",__ type: "POST",__ data: {stockid: stockid, idp: idp},__ success: function(data) {__ $(".stockleft").html(data);__ }__ });__}

In your PHP controller, you can create a function to get the stock value from the database and return it to the AJAX call:

public function get_stock() {__ $stockid = $this->input->post('stockid');__ $idp = $this->input->post('idp');__ $stock = $this->your_model->get_stock($stockid, $idp);__ echo $stock;__}

In your model, you can create a function to get the stock value from the database:

public function get_stock($stockid, $idp) {__ $this->db->select('stock');__ $this->db->where('stockid', $stockid);__ $this->db->where('idp', $idp);__ $query = $this->db->get('products');__ $result = $query->row();__ return $result->stock;__}

Make sure to replace 'your_model' with the actual model name you are using in your CodeIgniter application.

Rate this post

5 of 5 based on 6239 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us