我正在做样品购物车应用程序,我的要求是当ananymous用户要点击“添加到购物车”按钮然后它应该提示'login.php'如果它成功那么只有用户可以查看购物车。下面是添加到购物车按钮和iam使用这种ajax调用来检查凭据。请帮忙谢谢
echo "<input id=add type='button' onClick=\"window.open('cart.php?action=add&id=" . $id . "','_self');\" value='Add to Cart' />"; ====> "add to cart button"
$("#login").click(function() {
var action = $("#form1").attr('action');
var form_data = {
username: $("#username").val(),
password: $("#password").val(),
is_ajax: 1
};
$.ajax({
type: "POST",
url: "login.php",
data: form_data,
success: function(response)
{
if(response == 'success')
$("#form1").slideUp('slow', function() {
$("#message").html("<p class='success'>You have logged in successfully!</p>");
});
else
$("#message").html("<p class='error'>Invalid username and/or password.</p>");
}
});
return false;
});
答案 0 :(得分:0)
根据SR的建议,你应该尝试这样的事情
从按钮中删除此部分
onClick=\"window.open('cart.php?action=add&id=" . $id . "','_self');\
在jquery代码中根据您的要求执行这些更改....或同义词...
$("#add").click(function(e) {
e.preventDefault() // will stop the form from submitting...
var SKU_data = {sku : 12345, quantity:3} //some data related to the product to be sent to some script to add to cart
var url_to_check_user_session = 'checkAuthentication.php' ; //which can be the same as action
$.get(url_to_check_user_session, {username: $("#username").val()}, function(data){
if(data=='authenticated'){
$.ajax({ //code to check the login authentication
type: "POST",
url: "addToCart.php",
data: SKU_data,
success: function(response)
{
if(response == 'success')
$("#message").html("<p class='error'>Product added to your cart.</p>");
else
$("#message").html("<p class='error'>Ohh!! something went wrong while adding the product to the cart.</p>");
}
});}
else{ // now do the real thing
$("#form1").slideDown();
$("#login").click(function() {
$.ajax({
type: "POST",
url: "login.php",
data: form_data,
success: function(response)
{
if(response == 'success')
$("#form1").slideUp('slow', function() {
$("#message").html("<p class='success'>You have logged in successfully!</p>");
});
else
$("#message").html("<p class='error'>Invalid username and/or password.</p>");
}
});
})
}
})
return false;
});
希望这会有所帮助...