0

我正在做示例购物车应用程序,我的要求是当匿名用户要单击“添加到购物车”按钮时,如果成功,它应该提示输入“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;
    });
4

1 回答 1

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;
    });

希望这可以帮助...

于 2012-04-12T07:21:19.100 回答