2012-04-12 64 views
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" 

Ajax调用

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

问题是什么? – 2012-04-12 06:33:16

+1

嗨请解释更多细节,真的没有得到你想要做的..?你的id应该像这样id ='add'。用单引号。 – 2012-04-12 06:37:40

+0

先生..当匿名用户是关于点击“添加到购物车”按钮,然后登录屏幕应该出现,以检查用户是否有效..所以请检查我的“添加到购物车”按钮代码 – user1160126 2012-04-12 06:41:20

回答

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

希望这有助于...

+0

先生非常感谢.......... – user1160126 2012-04-12 07:25:46

+2

我刚刚更新了.....所以使用这个新的代码....只有一些变化,虽然... – 2012-04-12 07:26:39

+0

这个stackoverflow使人们lazzy。谷歌现在在做什么..!对于每一件简单的事情,人们都会在谷歌中搜索它Stackoverflow做得更多。总之它很好。 – 2012-04-12 07:55:13

相关问题