Okay coding gurus, I have been working on this for a few weeks now and I have pretty much everything figured out and functioning. The project is an abandoned cart recovery script. I have the form setup and Javascript that stores all input values as they are typed to localstorage. On page refresh, the values are sent off to the PHP processing file and written into the MySQL database correctly.
However, the Ajax post should occur on page close or navigation away as well, and not just page refresh. The only reason I am testing with page refresh is that it fires off an unload event and I can test the response back from the server as well as what my local variables and localstorage data is set to.
The Ajax post is set to be synchronous, and seems to be working fine through the unload event triggered on a page refresh. Is there something different between a page refresh and a window close? The code is below, if you need the form and PHP code I will post that as well.
For clarification, at the bottom of the script is what we are looking at. There is a beforeunload event bound to the window through Jquery that calls the submitform function, and commented out is a onunload handler for the window that also calls the submitform function. Both methods seem to be working the same, and properly calling the function on a page refresh and not a page close. They also do not work for navigation away from the page.
$( function() {
$( "#myform" ).sisyphus( {
excludeFields: $( "#useShippingRadio" ),
excludeFields: $( "#useBillingRadio" ),
excludeFields: $( "#billing-first-name" ),
excludeFields: $( "#billing-last-name" ),
excludeFields: $( "#billing-company" ),
excludeFields: $( "#billing-address1" ),
excludeFields: $( "#billing-address2" ),
excludeFields: $( "#billing-city" ),
excludeFields: $( "#billing-first-name" ),
excludeFields: $( "#billing-first-name" ),
excludeFields: $( "#billing-first-name" ),
excludeFields: $( "#billing-first-name" ),
excludeFields: $( "#paymentSelection_0" ),
excludeFields: $( "#ccSelectedRadio" ),
excludeFields: $( "#card-type" ),
excludeFields: $( "#card-number" ),
excludeFields: $( "#card-exp-month" ),
excludeFields: $( "#card-exp-year" ),
excludeFields: $( "#card-cvv" ),
excludeFields: $( "#paymentSelection_1" ),
excludeFields: $( "#ppSelectedRadio" ),
});
});
function submitform(){
$.ajax({
type: "POST",
url: 'process.php',
dataType: 'json',
data: {
myform:[{
fname: localStorage.getItem('myformundefinedshippingAddressDS.shipping_ROW0_first_name'),
lname: localStorage.getItem('myformundefinedshippingAddressDS.shipping_ROW0_last_name'),
company: localStorage.getItem('myformundefinedshippingAddressDS.shipping_ROW0_company'),
address: localStorage.getItem('myformundefinedshippingAddressDS.shipping_ROW0_address1'),
city: localStorage.getItem('myformundefinedshippingAddressDS.shipping_ROW0_city'),
state: localStorage.getItem('myformundefinedshippingAddressDS.shipping_ROW0_state'),
zip: localStorage.getItem('myformundefinedshippingAddressDS.shipping_ROW0_zip'),
phone: localStorage.getItem('myformundefinedshippingAddressDS.shipping_ROW0_phone'),
country: localStorage.getItem('myformundefinedshippingAddressDS.shipping_ROW0_country'),
cart_coupon: localStorage.getItem('myformundefinedgcPaymentDS.gcpayment_ROW0_redemptionCode'),
email: localStorage.getItem('myformundefinedmiscDS.shopperEmailAddress'),
orderSubTotal: orderSubTotal,
orderTotal: orderTotal,
numOfItems: numOfItems,
items: items,
ids: ids,
codes: codes,
qtys: qtys,
price: price,
orderTax: orderTax,
orderShipping: orderShipping,
appliedPromoIdList: appliedPromoIdList,
coupon: coupon,
storeId: storeId,
activeShipPromotionCount: activeShipPromotionCount,
itemImages: itemImages
}]
},
async: false,
});
};
$(window).bind('beforeunload', submitform());
//window.onunload(submitform());