Reputation: 309
I am trying to refresh / reload the page using ajax.
View:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Insert | SP</title>
<script src="<?php echo base_url(); ?>bootstrap/js/jquery-2.1.1.min.js"></script>
Script section
<script>
$(document).ready(function(){
function loadNowPlaying(){
$(this).load('<?php echo base_url().'Store/student' ;?>');
}
setInterval(function(){loadNowPlaying()}, 5000);
});
</script>
Renaming Code
</head>
<body>
<form action="<?php echo base_url();?>Store/add" method="post">
<input type="text" name="name" id="name"><br>
<input type="text" name="mail" id="mail"><br>
<input type="password" name="pass" id="pass"><br>
<input type="submit" name="submit" id="submit" value="Insert">
</form>
<table id="studata">
<?php foreach ($students as $student):?>
<tr>
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>
<tr>
<td><?php echo $student->Name;?></td>
<td><?php echo $student->Email;?></td>
<td><?php echo $student->Password;?></td>
</tr>
<?php endforeach;?>
</table>
</body>
</html>
My Controller code:
public function student()
{
$data["students"] = $this->sp->getstudent();
$this->load->view('student',$data);
}
My Model:
public function getstudent()
{
$this->db->select('*');
$this->db->from('ex_sp');
$query = $this->db->get();
return $query->result();
}
The code is working fine but it is loading my whole page [view] and i want to only load the table or some specific container but using the same view. I check a lot of question but did not find what i want, Please check the question instead of down voting. What is wrong in it? can someone please look at it.
Upvotes: 0
Views: 120
Reputation: 2468
Have a isAjax flag in your controller and view:
In controller:
public function student()
{
$data["isAjax"] = $_REQUEST['isAjax']; //change it to CI Get/Post method you are using.
$data["students"] = $this->sp->getstudent();
$this->load->view('student',$data); //you are sending $data["isAjax"] to view file
}
Now you have to check in view if isAjax flag is set then load only table else load entire page:
In View:
if($isAjax == 1)
// Load data only needed for ajax request
else
// Load all other data
Please note: You will need to send extra param with ajax request as isAjax=1 :
Ajax code Example:
$.ajax({
url: "view_file_ajax_call_url",
method: "POST",
data: { 'isAjax':1, 'example_data': value},
}).done(function(data) {
//operation
}
Upvotes: 1
Reputation: 805
In your script section try to change ''
to ""
<script>
$(document).ready(function(){
function loadNowPlaying(){
$(this).load("<?php echo base_url().'Store/student' ;?>");
}
setInterval(function(){loadNowPlaying()}, 5000);
});
</script>
Upvotes: 0