I am new to ajax and i am studying . i searched but i could not get a solution . i want to select all the countries from my database and want to echo the result in a table . i got the result but i don't know how to echo it in view . Somebody please help me. thanks in advance.
My controller Ajax.php
function index()
{
$this->load->view('Ajax/Ajax_view');
}
function Get_specific()
{
$name=$this->input->post('name');
$data['country']=$this->Ajax_m->select("countries");
echo json_encode($data);
$this->load->view('Ajax_view',json_encode($data));
}
my Model Ajax_m.php
function select($table){
$query=$this->db->get($table);
return $query->result();
}
My View Ajax_view.php
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#data').hide();
$('#sub').click(function(event){
event.preventDefault();
var name=$('#name').val();
$.ajax({
type: "POST",
url : "<?php echo base_url(); ?>" + "index.php/Ajax/Get_specific/",
datatype : 'html',
data : { name: name },
success:function(res){
$('#data').show();
alert('done')
}
});
});
});
</script>
</head>
<body>
<?php echo form_open('Ajax/insert'); ?>
<input type="text" name="name" id="name" value=""><br>
<input type="submit" name="sub" id="sub"><br>
<?php echo form_close(); ?>
<div id="data">
<table>
<tr>
<th>name</th>
<th>Delete</th>
</tr>
<?php
if(isset($query)):
foreach ($query as $row): ?>
<tr>
<td></td>
<td>Delete</td>
</tr>
<?php endforeach;
endif;?>
</table>
</div>
</body>
$.ajax({
type: "POST",
url : "<?php echo base_url(); ?>" + "index.php/Ajax/Get_specific/",
datatype : 'html',
data : { name: name },
success:function(res){
$('#data').html(res).show();
alert('done')
}
difficult to know exactly what you want here, but all i've done, is in the success of the ajax call, adding res
to the element before showing it.
edit: if you don't want to lose anything already in the element, you could always use .append()
rather than .html()