Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
ajax en codeigniter que muestra solo un mensaje falso incluso cuando el valor existe en la base de datos

Estoy tratando de verificar si existe valor en la base de datos en mi sitio web CodeIgniter usando AJAAX. He escrito el siguiente código:

 <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; }

Solo recibo el mensaje de que el valor no existe, incluso si el valor está en la base de datos.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. Está utilizando AJAX, no el método de envío de formularios, por lo que en el método back-end post () no funcionará
  2. Para transferir un valor del servidor en php, un método es echo, pero el retorno es incorrecto aquí. Por favor, reescriba su código como este Ver

 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>'); } } }); }

Controlador

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!