Welcome Guest, Not a member yet? Register   Sign In
Can't get URI class and jquery Ajax resquest working right.
#1

[eluser]chefnelone[/eluser]
I'm using ajax request (with jquery). Until now everything went fine.

But I have a problem when I use the URI class.
When using segments in the url the ajax request doesn't work.

If I run this code there's no problem

in controller site.php

Code:
function products(){

$data['product_id'] = '10';        
//CALL A MODEL AND FETCH PRODUCT RELATED TO  $data['product_id']
$data['query'] = $this->get_product_model($data['product_id']);
        
$this->load->view('backend/products', $data);        

}    



function update_product(){

$data = ($_POST);    
$data = $this->update_product_model($data);
    
}


in the view views/backend/products.php
Code:
<form class="updateProduct"    action="" method="post">

        <input name="product_name" type="text" size="60"  />
                
        <button id="saveButton" type="submit" >Save</button>

&lt;/form&gt;
and the js which fire the request:
Code:
$(document).ready(function() {

    $('.updateProduct').live('submit',function(eve) {
        eve.preventDefault();

        $.post('../site/update_product' , $(this).serialize() , function(html){});
        
    });
}) ;
I load the page at: http://locallhost/ci/site/products/
EVETHING WORKS FINE

But if I change this:

Code:
$data['product_id'] = '10';
for this:(so that I can pass the id in the url)
Code:
$data['product_id'] = $this->uri->segment(3);

And load the page at: I load the page at: http://locallhost/ci/site/products/10
I doesn't work.

I don't know how to solve it BUT I know the problem is JUST this line.
#2

[eluser]danmontgomery[/eluser]
Possible that you're trying to load the URI class after it's already been loaded? IIRC loading it twice empties the uri_segments. Why not just pass the ID into the function?

Code:
function products($product_id) {
  $data['product_id'] = $product_id;
  $data['query'] = $this->get_product_model($product_id);

  $this->load->view('backend/products', $data);    
}
#3

[eluser]chefnelone[/eluser]
[quote author="noctrum" date="1265068053"]Possible that you're trying to load the URI class after it's already been loaded? IIRC loading it twice empties the uri_segments. Why not just pass the ID into the function?

Code:
function products($product_id) {
  $data['product_id'] = $product_id;
  $data['query'] = $this->get_product_model($product_id);

  $this->load->view('backend/products', $data);    
}
[/quote]

I tried and din't work.

but it was useful to see that it doesn't work even if I take out the line BUT add a segment to the url:

Code:
$data['product_id'] = $this->uri->segment(3);

It seems that it doesn't work if I add a segment to the url like (even if I don't use the uri class):

http://locallhost/ci/site/products/10 //DOESN'T WORK

http://locallhost/ci/site/products // WORKS FINE
#4

[eluser]chefnelone[/eluser]
I solved it.

using this
Code:
$.post('http://locallhost/ci/site/update_product' , $(this).serialize() , function(html){});

instead of:
Code:
$.post('../site/update_product' , $(this).serialize() , function(html){});
#5

[eluser]danmontgomery[/eluser]
Then you have a routing problem... index.php/[controller]/[function]/[parameter] is basic CI functionality that works with no customization
#6

[eluser]chefnelone[/eluser]
[quote author="noctrum" date="1265071867"]Then you have a routing problem... index.php/[controller]/[function]/[parameter] is basic CI functionality that works with no customization[/quote]
how can I check on this?




Theme © iAndrew 2016 - Forum software by © MyBB