好的编码大师,我已经在这几个星期工作了,我几乎所有的事情都想通了。该项目是一个废弃的购物车恢复脚本。我有表单设置和Javascript存储所有输入值,因为它们被输入到localstorage。在页面刷新时,值将被发送到PHP处理文件并正确写入MySQL数据库。
但是,Ajax帖子应该在页面关闭或导航时发生,而不仅仅是页面刷新。我使用页面刷新测试的唯一原因是它会触发一个unload事件,我可以测试服务器的响应以及我的本地变量和localstorage数据的设置。
Ajax帖子设置为同步,并且似乎通过页面刷新触发的卸载事件正常工作。页面刷新和窗口关闭之间有什么不同吗?代码如下,如果您需要表单和PHP代码,我也会发布它。
为了澄清,脚本底部是我们正在查看的内容。通过调用submitform函数的Jquery绑定到窗口的beforeunload事件,注释掉的是也调用submitform函数的窗口的onunload处理程序。这两种方法看起来都是一样的,并且在页面刷新时正确调用该函数而不是关闭页面。它们也不适用于远离页面的导航。
$( 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());