Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Ajax load *.tpl file path

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
oo0o
Smarty n00b


Joined: 25 Oct 2016
Posts: 3

PostPosted: Tue Oct 25, 2016 11:06 pm    Post subject: Ajax load *.tpl file path Reply with quote

Hi, I really can't find solution. I have JS code
Code:
function addToCart(itemId) {
    $.ajax({
      type: 'POST',
      url: "/cart/addtocart/" + itemId + '/',
      dataType: 'json',
      success: function (data) {
        if (data['success']) {
          $('#cartCntItems').html(data['cntItems']);
          $('#from-ajax').load('http://sitename.local/cart/');
        }
      }
    });
  };

  $(document).on('click', '.addToCart', function (e) {
      e.preventDefault();
      addToCart(this.id.split('_')[1]);
  });


On this line
Quote:
$('#from-ajax').load('http://sitename.local/cart/');

I make cart.tpl with new data from database and insert into html.

How i can make it another way, cause I think this way is not correct?

If you need some another files, controller or models, I can post it here.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Oct 26, 2016 12:55 am    Post subject: Reply with quote

Get your question straight. It is either a JavaScript question, in which case it is an off-topic on this forum, or it is a Smarty (or PHP) question, in which case, please make your question more clear.
Back to top
View user's profile Send private message
oo0o
Smarty n00b


Joined: 25 Oct 2016
Posts: 3

PostPosted: Wed Oct 26, 2016 7:56 am    Post subject: Reply with quote

Oh sorry. Think the question is how to load with
ajax after
Code:
echo $smarty->fetch("*.tpl")
Back to top
View user's profile Send private message
oo0o
Smarty n00b


Joined: 25 Oct 2016
Posts: 3

PostPosted: Wed Oct 26, 2016 8:45 am    Post subject: Reply with quote

Okay, after this night I solve this problem. Now I describe all, what I have, maybe It helps some person.



I have header.tpl where include some code to make

Code:

<div id="cartModal" class="modal">
  <div class="modal-content">
    <span class="closeCart">X</span>

    <div class="panel panel-default">
      <!-- Default panel contents -->
      <div class="panel-heading text-center">Cart</div>
      <div id="from-ajax">
      </div>
    </div>

  </div>
</div>


This is a modal block that appears on click cart - item. And where I want to load correct data from database.


I have CartController, that load necessary data and fetch it

Code:
 $itemsIds = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
   
    $rsCategories = getAllMainCatsWithChildren();
    $rsKits8 = getKitsByCat(4);
    $rsKits16 = getKitsByCat(5);
    $rsSortsHoney = getLastSortsHoney();
    $rsSortsHoneyKits8 = getAllKits8WithSorts();
    $rsSortsHoneyKits16 = getAllKits16WithSorts();
    $rsCartKits = getKitsFromArray($itemsIds);
    $rsCount = getCountKitsFromArray($itemsIds);
   
    $smarty->assign('pageTitle', 'Корзина');
    $smarty->assign('rsCategories', $rsCategories);
    $smarty->assign('rsKits8', $rsKits8);
    $smarty->assign('rsKits16', $rsKits16);
    $smarty->assign('rsSortsHoney', $rsSortsHoney);
    $smarty->assign('rsSortsHoneyKits8', $rsSortsHoneyKits8);
    $smarty->assign('rsSortsHoneyKits16', $rsSortsHoneyKits16);
    $smarty->assign('rsCartKits', $rsCartKits);
    $smarty->assign('rsCount', $rsCount);
   
    $output = $smarty->fetch("cart.tpl");
   
    echo $output;


cart.tpl work with database and get necessary data.

After we make this

Code:
$output = $smarty->fetch("cart.tpl");
   
    echo $output;


We can go to JS and write this

Code:

  function updateCart() {
    $.ajax({
      type: 'POST',
      url: "/cart/",
      dataType: 'text',
      success: function(data) {
        $('#from-ajax').html(data);
      }
    })
  }


dataType is text and we insert data to div#fromAjax in header.tpl

Then you can call it in JS something like this

Code:

   $(window).click(function(event) {
    if (event.target == $("#cartModal")[0]){
      $("#cartModal").css({ display: "none"});
      updateCart();
    };
  });


If anybody has better solution, please write me. Thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP