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