2012-04-12 66 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

相關問題