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

131
Visualizações
ajax in codeigniter showing only false message even when the value exist in database

I am trying to check if value exist in database in my CodeIgniter website using AJAAX. I have written the following code:

<input id="username" name="pincode" type="text" class="form-control" placeholder="Enter Pincode">
<input id="prodid" name="prodid" value="<?php echo $prodid;?>" type="hidden">

<button type="button" onclick="check_if_exists();" class="btn btn-primary">Check</button>

function check_if_exists() {

  var username = $("#username").val();
  var prodid = $("#prodid").val();

  $.ajax({
    type: "post",
    url: "<?php echo base_url(); ?>index.php/homecontroller/filename_exists",
    data: {
      username: username,
      prodid: prodid
    },
    success: function(response) {
      if (response == true) {
        $('#msg').html('<span style="color: green;">' + msg + "</span>");
      } else {
        $('#msg').html('<span style="color:red;">Delivery Not Available at ' + username + '</span>');
      }
    }
  });
}

              function filename_exists()
{
    $username = $this->input->post('pincode');
    $prodid = $this->input->post('prodid');
    $exists = $this->product->filename_exists($prodid);
   
 
    if ($exists) {
        return true;
    } else {
        return false;

    }
}

  function filename_exists($prodid)
                  {
  $this->db->select('*');
  $this->db->where('id', $prodid);
  $this->db->from('product');

  $query = $this->db->get();
  $result = $query->result_array();
  return $result;
                  }

I am only getting the message that the value doesn't exist even if the value is there in database.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  1. You are using AJAX not the form submission method, so in back-end post() method won't work
  2. To transfer a value from server in php, one method is echo, but return is wrong here. Please rewrite your code like this View

function check_if_exists() {

  var username = $("#username").val();
  var prodid = $("#prodid").val();
  var transfer = [username,prodid];
  $.ajax({
    type: "post",
    url: "<?php echo base_url(); ?>index.php/homecontroller/filename_exists",
    data: {result: JSON.stringify(transfer)},
    success: function(response) {
      if (response) {
        $('#msg').html('<span style="color: green;">' + msg + "</span>");
      } else {
        $('#msg').html('<span style="color:red;">Delivery Not Available at ' + username + '</span>');
      }
    }
  });
}

Controller

function filename_exists()
{
    $post = json_decode($_POST['result']);
    $username = $post[0];
    $prodid = $post[1];
    $exists = $this->product->filename_exists($prodid);
    echo $exists;
}

Modal

function filename_exists($prodid)
{
  $this->db->select('id');
  $this->db->where('id', $prodid);
  $this->db->from('product');

  $query = $this->db->get();
  if($query->num_rows()) return true;
}

enter code here
about 4 years ago · Juan Pablo Isaza 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