Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda