0

There is a left block on my web-page - assume it shows a shopping basket. When user clicks the "add to basket" button on any product page, I want to reload my left block (shopping basket) so the new product that has been added by the customer can be seen on the left side of the page without reloading the page.

When customer clicks "add to basket" button, it already puts the product into the basket, that's not the problem but I cannot make the left block refresh. The code is like:

<script type="text/javascript">
function js_add_to_basket(){
    /* vars from form */
    var book_id = jQuery.trim($('#book_id').attr('value'));

    $.ajax({
        type:'POST',
        url : '<?=base_url().index_page()?>/basket/add_to_basket/'+new Date().getTime(),
        data : $('#add_to_basket').serialize(),
        success: function(q){
            $('#add_to_basket').html(q);
            $("#left_basket").load("*path*\inc_left.php #left_basket");
        }
    }) // end of ajax request
} // end of js_basket
</script>

left_basket is the id of the whole div of the left block. I think the path of the file inc_left.php is coming wrong. Whatever I wrote it there, it didn't work. The file is in the path of "views/inc/inc_left.php".

Any idea?

6
  • Can you add the code for views/inc/inc_left.php Commented Jan 11, 2012 at 13:50
  • @Bruce, is it relevant? inc_left works properly, I just want to refresh it. Commented Jan 11, 2012 at 13:53
  • 1
    your inc_left.php is accessible from js? I guess you need a controller method to render the content for left panel, then js can access it with url like <?=base_url().index_page()?>/basket/content Commented Jan 11, 2012 at 13:57
  • 1
    @gzg - see my anwser, your jquery will not refresh it but only load a portion of it into container. The more code you can provide for a question is usually more helpful. Commented Jan 11, 2012 at 13:57
  • @Bao, you might be right about it. But when I use base_url in the js code, it gives me fatal error: undefined function base_url(). Commented Jan 11, 2012 at 14:33

3 Answers 3

1

You need a controller method to render the content for left panel, then js can access it with url such as <?=base_url().index_page()?>/basket/content

Sign up to request clarification or add additional context in comments.

Comments

1

Just a guess but do you have a #left_basket in the view views/inc/inc_left.php?

As you are loading the only the #left_basket conent of views/inc/inc_left.php into #left_basket

If you are wanting to load the full HTML from views/inc/inc_left.php, then use the following code:

$('#left_basket').load('views/inc/inc_left.php');

1 Comment

I guess we're heading into the right answer. This code piece is refreshing the left block, but apparently caller function cannot see the path, because it's refreshing to the main page.
0

You have to enter two jquery ajax functions. 1st for when user click on "add to basket" button this will update the shoping table. The 2nd ajax function for fetch data from shoping table and show it in left side where you want :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.