Reputation: 321
I have the following generated href link on a product list page
<a title="Add to My Favourites" class="a2f-link" href="http://epicurium.localhost/by-lifestyle-diet/breakfast?sort=20a&products_id=1838&action=add_to_favourites">
<img src="images/icons/favourites.png" alt="Add to My Favourites" title=" Add to My Favourites " width="16" height="16" class="fl_add" />
</a>
The php that generates this href link is
public function add_to_favourites_link($products_id, $product_value)
{
if (!isset($this->icon)) {
$this->icon = (FAVOURITES_LISTING_ICON == 'Font Awesome') ? FA_ADD_TO_FAVOURITES : zen_image(DIR_WS_IMAGES . 'icons/' . BUTTON_ADD_TO_FAVOURITES, BUTTON_ADD_TO_FAVOURITES_ALT, '', '','class="fl_add"');
}
$page_link = zen_href_link($GLOBALS['current_page_base'], zen_get_all_get_params(array('products_id', 'action')) . 'products_id=' . (int)$products_id . '&action=add_to_favourites');
return ($this->favouritesEnabled) ? sprintf('<span class="a2f-wrap"><a title="%4$s" class="a2f-link" href="%2$s">%3$s</a><span class="a2f-value">%1$s</span></span>', $product_value, $page_link, $this->icon, BUTTON_ADD_TO_FAVOURITES_ALT) : '';
}
In another file I have
case 'add_to_favourites':
if (isset($_GET['products_id'])) {
if (zen_has_product_attributes($_GET['products_id'])) {
$messageStack->add_session('product_info', sprintf(CAUTION_PRODUCT_HAS_ATTRIBUTES, zen_get_products_name($_GET['products_id'])), 'caution');
zen_redirect(zen_href_link(zen_get_info_page($_GET['products_id']), zen_get_all_get_params(array('action'))));
} else {
if (!(isset($_SESSION['customer_id']) && zen_not_null($_SESSION['customer_id']))) {
$messageStack->add_session('login', CAUTION_LOGIN_REQUIRED_FOR_FAVOURITES, 'caution');
$_SESSION['navigation']->set_snapshot();
zen_redirect(zen_href_link(FILENAME_LOGIN, '', 'SSL'));
}
$favourites = new favourites();
$favourites->addToFavourites($_GET['products_id'], (FAVOURITES_SHOW_ON_ADD == 'true') ? 'favourites' : 'header');
if (FAVOURITES_SHOW_ON_ADD == 'true') {
zen_redirect(zen_href_link(FILENAME_FAVOURITES, '', 'SSL'));
} else {
zen_redirect(zen_href_link($current_page_base, zen_get_all_get_params(array('action', 'products_id'))));
}
}
}
break;
I want to change this to work via AJAX so I have no page reload, but to be honest, I have no idea where to start modifying an href link into an AJAX call. The only AJAX I've ever attempted was submitting a simple form via a button, where the file the AJAX linked to was specific to that process, not part of a switch.
I've done a search online, and via SO and not found any examples of how this is done.
Any advice or examples welcome!
Upvotes: 1
Views: 226
Reputation: 1026
give id to anchor tag and write script for ajax
<a id="add_fav" href="javascript:void(0)" ..> img src="">...
$(document).on('click','#add_fav',function(){
//write your ajax call here
$.ajax({
url : 'your_controller_method',
type : 'GET', or "POST" select accordingly
data : {'product_id' : 1838,
....
},
dataType:'json',
success : function(data) {
alert('Data: '+data);
},
error : function(request,error){
}
});
})
or you can make javascript function and pass data to function and then set it to ajax data
Upvotes: 1