Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
getting the value of column inside a table using ajax in php codeigniter

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

enter image description here

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">

Below is my controller's and view's code,

  function checkstock(){
          $post = json_decode($_POST['result']);
          $idp = $post[1];
          $stockid = $post[0];
          $exists = $this->product->checkstock($idp,$exists);
          echo $exists;
  }

  function checkstock($idp,$stockid)
   {
        $this->db->select($stockid);
        $this->db->where('id', $idp);
        $this->db->from('product');
        $query = $this->db->get();
        $result = $query->result();
        return $result;
   }

  

However this doesn't echo the count in the column, can anyone please tell me how to fix this, thanks in advance.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

FROM

function checkstock($idp,$stockid)
{
    $this->db->select($stockid);
    $this->db->where('id', $idp);
    $this->db->from('product');
    $query = $this->db->get();
    
    $result = $query->result();
    return $result;
}

TO

function checkstock($idp,$stockid)
{
    $this->db->select($stockid);
    $this->db->where('id', $idp);
    $this->db->from('product');
    $query = $this->db->get();

    $result = $query->row();
    return $result->{$stockid};
}

Then you can get data into reponse variable and just console it.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda