0

I am having troubles with this code, I am trying to do ajax on codeigniter, I have a form with the username to select . If I click on an user I don't get the other variables from the database. The SQL query works fine. The code is divided into view/controller/model. The model code seems to work fine, the problem should be into the view and/or the controller.

I visualize correctly the front end form, but when I click on the names I get no output from the queries, there is no explicit error.

view code:

<!doctype html>
<html>
 <head>
   <title>How to send AJAX request in Codeigniter</title>
 </head>
 <body>

  Select Username : <select id='sel_user'>
   <option value='abc'>abc</option>
   <option value='ad'>ad</option>
   <option value='admin'>admin</option>
   <option value='sunil'>sunil</option>
  </select>

  <!-- User details -->
  <div >
   Username : <span id='suname'></span><br/>
   Name : <span id='sname'></span><br/>
   Email : <span id='semail'></span><br/>
  </div>

  <!-- Script -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script type='text/javascript'>
  $(document).ready(function(){

   $('#sel_user').change(function(){
    var username = $(this).val();
    $.ajax({
     url:'<?=base_url()?>index.php/user_ajax/userDetails',
     method: 'post',
     data: {username: username},
     dataType: 'json',
     success: function(response){
      var len = response.length;

      if(len > 0){
       // Read values
       var uname = response[0].username;
       var name = response[0].password;
       var email = response[0].email;

       $('#suname').text(uname);
       $('#sname').text(name);
       $('#semail').text(email);

      }else{
       $('#suname').text('abc');
       $('#sname').text('cde');
       $('#semail').text('');
      }

     }
   });
  });
 });
 </script>
 </body>
</html>

controller code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class user_ajax extends CI_Controller {

 public function index(){

  // load base_url
  $this->load->helper('url');

  // load view
  $this->load->view('user_view_ajax');
 }

 public function userDetails(){
  // POST data
  $postData = $this->input->post();
  # echo $postData;
  //load model
  $this->load->model('model_ajax');

  // get data
  $data = $this->model_ajax->getUserDetails($postData);

  echo json_encode($data);
 }

}

model code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class model_ajax extends CI_Model {

 function getUserDetails($postData){

  $response = array();

  if($postData['username'] ){
    /*
   // Select record
   $this->db->select('*');
   $this->db->where('username', $postData['username']);
   $q = $this->db->get('users');
   $response = $q->result_array();
   */
    // Create connection
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "anubi";
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    #$sql = $conn->query("SELECT username, password, email from users WHERE username=".$postData['username']);
    #$response=$sql->result_array();
    $sql = $conn->query("SELECT username, password, email from users WHERE username=".$postData['username']);
    $array = array();
     while ($row = $sql->fetch_assoc()) {
        $array[] = $row;

     }
     $response=$array;
  }

  return $response;
 }

}
  • what type of error are you facing? add more details about your issue – Bilal Ahmed Aug 28 '18 at 11:37
  • $postData = $this->input->post('username'); – Mulet Enguix Aug 28 '18 at 14:31
  • I tried the modification of Mulet, still not working, @Bilal there is no error unfortunately, I see the page correctly but the ajax form doesn't work when I click on the names – Rocco Proscia Aug 28 '18 at 15:54
  • You need to do some debugging to track down the problem. Look at the Network tab in the Developer Tools of your browser. Look at the HTTP request. Is it formatted as you expect? Once you know that, you can eliminate either the JS or the PHP as being the problem. – Quentin Aug 28 '18 at 15:59
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Aug 28 '18 at 15:59

2 Answers2

0

why you are using core php mysql connect functionality ? just put db details in database.php and use CodeIgniter Query builder for mysql queries.

Please Check this link --

https://www.codeigniter.com/userguide3/database/query_builder.html#selecting-data

Sorav Garg
  • 1,116
  • 1
  • 9
  • 26
-1

data: {"username": username} do this type

  • There is nothing about the property name *username* which would require it to be quoted in an object literal. – Quentin Aug 28 '18 at 15:56