Welcome Guest, Not a member yet? Register   Sign In
ajax display div
#1

I need to display an ajax view in a div named showthis, I am able to see the generated view within the console, but how can I get
the view to display within the div "showthis" this is what I have so far.

Thanks in-advance for your assistance. 


// Main view setup

foreach($products as $product) : ?>
<div class="acc-item-slot">
<?php echo $product->title; ?>
<?php echo $product->description; ?>
<?php echo $product->sales; ?>
<a href="#" onclick="prev_product(<?php echo $product->campaignID;?>)">Preview</a>
<?php endforeach;
</div>

<div id="showthis"> // I would like to load view below here  
<div id="prodview">  // Example of view prodview.php I would like to load
<?php echo $product->title; ?>
<?php echo $product->description; ?>
<?php echo $product->sales; ?>
</div> 
</div>


//Controller View setup
public function ajax_edit($id)
{
$this->load->model('Product_model');
$data = $this->Product_model->get_by_id($id);
$this->load->view('prodview', $data); 
echo json_encode($data);

}


// Jquery Ajax setup 

function prev_product(id){
$.ajax({
type : "POST",
url : "<?php echo site_url('index.php/products/ajax_edit')?>/"+id,
dataType : "JSON",
success: function(data){ 
$(showthis).load('index.php/products/method', data, true); // tried to load view here but no joy
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error get data from ajax');
}
});
}
Reply
#2

Your missing the # for one and you also need the full url.

Site_url includes index.php

Code:
url : "<?php echo site_url('products/ajax_edit/');?>"+id,

$(#showthis).load("<?php echo site_url('index.php/products/method');?>", data, true); // tried to load view here but no joy

I  usually add this to my html it makes it easier to get the url in jQuery


Code:
<html lang="en">

<head>

   <!-- Pass base_url() and site_url() to JavaScript -->
   <!-- url: siteUrl+id, baseUrl+'method/'+id, etc;
   <script>
       var baseUrl = "<?php echo base_url(); ?>";
       var siteUrl = "<?php echo site_url(); ?>";
   </script>

</head>
 
If you want to use and show index.php then use baseUrl.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thanks for your response, turns out that I just needed to add ("$data=") 

My controller view now reads:

$data = $this->load->view('prodview',$data, TRUE);
$this->output->set_output($data);
Reply




Theme © iAndrew 2016 - Forum software by © MyBB