[eluser]Kraig[/eluser]
I am trying to use Ajax/Jquery to perform a check. However, the php file with the database query is not working. Below is my code. I know that the code works because when I change the url to a link outside of CI's index it works just fine.
The removed part in the code is the script tag in html.
HTML File:
Code:
<button type="button" name="getdata" id="getdata">Get Data.</button>
<div id="result_table">
</div>
[removed]
$('#getdata').click(function(){
//-------------------------------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-------------------------------------------------------------------------------------------
$.ajax({
url: '<?php echo base_url().'jqueryDB/jquery';?>', //the script to call to get data
type:'POST',
data: "", //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
dataType: 'json', //data format
success: function(output_string){
$("#result_table").append(output_string);
}
});
});
[removed]
PHP Controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class JqueryDB extends CI_Controller {
function __construct(){
parent::__construct();
}
function index()
{
$is_logged_in = $this->loggedin->loggedin();
if(!$is_logged_in)
{
$data['main_content'] = 'home';
$this->load->view('includes/template2', $data);
}
else
{
$data['main_content'] = 'jqueryDB';
$this->load->view('includes/template2', $data);
}
}
public function jquery(){
//--------------------------------------------------------------------------
// 1) Query database for data
//--------------------------------------------------------------------------
$userID = $this->session->userdata('id');
$query1 = $this->db->query("SELECT * FROM test ");
if($query1->num_rows() > 0){
$output_string = "";
$output_string .= "<table border='1'>\n";
foreach ($query1->result() as $row){
$output_string .= "<tr>\n";
$output_string .= "<td>".$row['id']."</td>\n";
$output_string .= "<td>".$row['name']."</td>\n";
$output_string .= "</tr>\n";
}
$output_string .= "</table>\n";
}
else{
$output_string = "There are no results";
}
echo json_encode($output_string);
}
}
If I change the jquery function in the PHP file to this: it displays "There are no results" when the button is clicked...which is what it should do, so not sure what the problem is
Code:
public function jquery(){
$output_string = "There are no results";
echo json_encode($output_string);
}