简体   繁体   English

添加到购物车按钮应提示登录屏幕

[英]add to cart button should prompt log in screen

am doing sample shopping cart application , my requirement is when the ananymous user about to click on "add to cart" button then it should prompt for 'login.php' if it is success then only user can view the cart. 我正在做示例购物车应用程序,我的要求是,当匿名用户要单击“添加到购物车”按钮时,如果成功,则应该提示输入“ login.php”,然后只有用户才能查看购物车。 Below is the add to cart button and iam using this sort of ajax call to check credentials. 下面是“添加到购物车”按钮,IAM使用这种Ajax调用来检查凭据。 Please help thank you 请帮忙谢谢

echo "<input id=add type='button' onClick=\"window.open('cart.php?action=add&id=" . $id . "','_self');\" value='Add to Cart' />";  ====> "add to cart button"

Ajax call 阿贾克斯电话

$("#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;
    });

So as per SR's suggestions you should try something like this 因此,根据SR的建议,您应该尝试这样的操作

remove this part from the button 从按钮中删除此部分

onClick=\"window.open('cart.php?action=add&id=" . $id . "','_self');\

And here in the jquery code do these changes....or something synonymous, as per your requirements... 然后在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;
    });

hope this helps... 希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM