I was just successful in getting AJAX to get data from the database and have it render to a div that shows upon a button being clicked. But I get this error upon it reading the first line containing php in the view.
Here is my view file html
<div id="association-list-wrapper" class="list-wrapper">
<?php echo form_input('filter', 'Filter by key, name, or location', array('class' => 'text-input filter', 'data-label-value' => 'Filter by key, name, or location')) ?>
<section class="table-wrapper">
<table id="association-list" class="filter-results">
<tbody>
<?php foreach ($associations as $association): ?>
<tr id="<?php echo key($association)?>" class="result-row">
<td class="td col1"><p id=""><?php echo $association['AssociationKey'] ?></p></td>
<td class="td col2"><a href="<?php echo base_url('association/'.$association['AssociationKey']) ?>"><?php echo $association['Name'].' '.$association['Title'] ?></a></td>
<td class="td col3"><p><?php echo $association['City'].', '.$association['State'] ?></p></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</section>
</div>
Here is my JS:
$('li#page1').click(function(e){
e.preventDefault();
$('#pick-list').show();
$('#association-pick-list').show();
$.ajax({
type: 'get',
url: 'http://myapp.loc/association/pick-list',
dataType: 'html',
success: function (html) {
$('#shadowbox').load('http://myapp.loc/application/views/association_management/pick_list.php');
}
});
$('.shadowbox-wrapper').fadeIn(250);
$('.shadowbox-window').fadeIn(250);
});
And here is my controller:
public function pick_list()
{
$this->load->helper('form');
$data['associations'] = $this->association_model->get_all_associations();
$this->load->view('association_management/pick_list', $data);
}
Why won't it render the page? I tested going to the URL that triggers the controller/method and it renders just fine, but when it renders with AJAX all the php breaks.
Error occur due to this
$('#shadowbox').load('http://myapp.loc/application/views/association_management/pick_list.php');
If you load view directly like above then data from Controller are not accessible in view page. So I will suggest you to get data from controller and render on your view.
Your controller
public function pick_list()
{
$this->load->helper('form');
$associations = $this->association_model->get_all_associations();
echo json_encode(['data'=>$associations]);
}
Now render your view with returned data.
Try this,if you are not getting,please ask,i wil help you
Use this instead of form_input() in view page
echo form_open('filter', array('class' => 'text-input filter', 'data-label-value' => 'Filter by key, name, or location'));
In controller ,add this too
$this->load->library('form_validation');
and one mistake in your view form associations is the array name you passed from controller. but you foreach is done $association. use $association
Give id to your form and use that id in your ajax In view
echo form_open('filter', array('class' => 'text-input filter', 'data-label-value' => 'Filter by key, name, or location','id'=>'myform'));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.