Miva Merchant Development by Scot's Scripts

JQUERY AJAX: Simple Click and Send Data Attributes

Miva Random Code Trivia

Want to start an online store? We work with you from start to finish, from commerce platform to design to SEO.
Experience counts, and we have a lot.

WARNING:

This information is offered "AS IS" for internal reference only. Use at your own risk.
Does AI actually understand your Miva Merchant store? Our smart JSON-LD schema generator makes sure it does. Contact us to get started. (more info)
SEO Audit & Inquiries • News & Updates • SEO for Miva Merchant

JQUERY AJAX: Simple Click and Send Data Attributes

THIS CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED;
THE AUTHOR DISCLAIMS ALL LIABILITY FOR ANY DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR STORE DISRUPTIONS ARISING FROM ITS USE.

Scot Ranney • November 09, 2024


The most simple way in existence to send data to an AJAX page or back end (such as json.mvc via module) using jquery.

Step 1: Assign a CSS ID to the thing that is getting clicked on.

<div id="clickme" data-firstname="Scot" data-city="Bellingham"></div>
<div id="results"></div>

Step 2: We track the click via the CSS ID and set up data vars based on the data attributes.

The AJAX URL can be a page template in the store. We often make an AJAX handler page and give it the code AJAX.

If using a module to get data and such the better way might be to use the json module feature and send the click to json.mvc - this probably handles bot clicks better although it's more complicated and can't be updated without updating the module.

The html(result) is nothing more than whatever the AJAX page or module calls returns. Simple as that.

$("#clickme").on('click', function(){
		var $el = $(this);   
	var firstname =  $el.data('name');
	var city = $el.data('city');
	$.ajax({
		type:'POST', 
			url:'&mvt:ajax_page_uri;',
			data:{'firstname':firstname,'city':city},
			success:function(result){
				$('#results').html(result);
			}
	}); 
});

https://www.scotsscripts.com/mvblog/ajax-simple-click-and-send-data-attributes.html

ajax