Shopping Cart Theory - Update cart VS Checkout

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gregerly
    Recognized Expert New Member
    • Sep 2006
    • 192

    Shopping Cart Theory - Update cart VS Checkout

    Hello,

    I once again turn to this community of genius' for some help with a problem. I've got a shopping cart (which is working well so far) that I will be implementing all kinds of AJAX functionality on. However, first I want to make sure it works if javascript is not available. So when your on the shopping cart page, you can update the quantities then click Update Cart, which would update the totals, and refresh the shopping cart page, and then you could checkout. My question is: Without Javascript, how do I know if the user is trying to update the cart, or checkout? The cart in question can be found at:

    Fresno SCORE

    You have to add a workshop to the cart. Then you will see the cart page I'm talking about.

    Thanks in advance!

    Greg
    Last edited by gregerly; Jul 27 '07, 07:32 PM. Reason: Adding URL
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    I haven't visited the site, but so you know, whenever you do anything in JavaScript, the non-JavaScript alternative needs to make sense. With AJAX, you can automatically update things behind the scenes. Otherwise, just have them post the data through a form and handle it server-side just like you would with AJAX.

    Comment

    • gregerly
      Recognized Expert New Member
      • Sep 2006
      • 192

      #3
      Originally posted by volectricity
      I haven't visited the site, but so you know, whenever you do anything in JavaScript, the non-JavaScript alternative needs to make sense. With AJAX, you can automatically update things behind the scenes. Otherwise, just have them post the data through a form and handle it server-side just like you would with AJAX.
      That is what my question is concerning. I know how to use AJAX responsibly, which is why I'm building it to work without Javascript first. But I have one form, with two buttons, UPDATE and CHECKOUT. How do I know that the user wants to UPDATE the cart rather than CHECKOUT and vice versa, when all the code is done on the server side?

      Thanks,

      Greg

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        Originally posted by gregerly
        That is what my question is concerning. I know how to use AJAX responsibly, which is why I'm building it to work without Javascript first. But I have one form, with two buttons, UPDATE and CHECKOUT. How do I know that the user wants to UPDATE the cart rather than CHECKOUT and vice versa, when all the code is done on the server side?

        Thanks,

        Greg
        You check for the button upon submission. If you give the button a name attribute, then it will be submitted just like other elements of your form. Be aware that the form can be submitted without clicking a button, so make it so the first button in your form is the default if no button is sent.

        Comment

        • gregerly
          Recognized Expert New Member
          • Sep 2006
          • 192

          #5
          I think I see where you are going with that volectricity. Does that mean that if I have two buttons in the form, say:

          [HTML]<button name='update'>U pdate Cart</button>
          <button name='checkout' >Checkout</button>[/HTML]

          and I only click the "update" button, the checkout button is not submitted as part of the $_POST array? That will make things easy if this is the case. I'll try it and update here.

          Thanks for the response!

          Greg

          Comment

          • kovik
            Recognized Expert Top Contributor
            • Jun 2007
            • 1044

            #6
            Originally posted by gregerly
            I think I see where you are going with that volectricity. Does that mean that if I have two buttons in the form, say:

            [HTML]<button name='update'>U pdate Cart</button>
            <button name='checkout' >Checkout</button>[/HTML]

            and I only click the "update" button, the checkout button is not submitted as part of the $_POST array? That will make things easy if this is the case. I'll try it and update here.

            Thanks for the response!

            Greg
            In order to test it, you could simply print out the $_POST array and see what you get.

            [code=php]print_r($_POST) ;[/code]

            Comment

            • gregerly
              Recognized Expert New Member
              • Sep 2006
              • 192

              #7
              Ok, figured it out. Only the button clicked is submitted with the form. I guess you learn something new everyday! Thanks for you help.

              We can consider this case closed!

              Greg

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, Greg.

                Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

                Comment

                • gregerly
                  Recognized Expert New Member
                  • Sep 2006
                  • 192

                  #9
                  Time to reopen the case.

                  Issue:

                  In Firefox, IE7, a button is only is only included with the POST array if it's clicked. So, if you have two buttons on a form, only the one that was clicked will be submitted with the form.

                  In IE6, all buttons are included in the POST array regardless of if it was clicked or not. So, If I have a form (shopping cart) and I want to give the user the ability to update or checkout, I can't tell what he's trying to do as both buttons are submitted regardless of which one was clicked.

                  So, how can I tell what the user is trying to do? I'm thinking I'm going to have to use javascript.

                  Comment

                  • jayturley
                    New Member
                    • Aug 2007
                    • 6

                    #10
                    Originally posted by gregerly
                    Time to reopen the case.

                    Issue:

                    In Firefox, IE7, a button is only is only included with the POST array if it's clicked. So, if you have two buttons on a form, only the one that was clicked will be submitted with the form.

                    In IE6, all buttons are included in the POST array regardless of if it was clicked or not. So, If I have a form (shopping cart) and I want to give the user the ability to update or checkout, I can't tell what he's trying to do as both buttons are submitted regardless of which one was clicked.

                    So, how can I tell what the user is trying to do? I'm thinking I'm going to have to use javascript.
                    I have successfully implemented a solution to this problem by having two submit buttons:

                    Code:
                    <input type="submit" name="update cart" id="update cart" value="update cart" />
                    <input type="submit" name="checkout" id="checkout" value="checkout" />
                    When the form is submitted, use the server-side code to determine the value of the submit element of the POST array. Then you can determine what to do from there.

                    When you Ajaxify, you will use degradable javascript to add return false; to the onclick event handler for the submit buttons to stop the form from submitting, and since you have access to the id of the element clicked, you can submit to whatever page you desire.

                    Hope this helps,

                    -Jay

                    Comment

                    • gregerly
                      Recognized Expert New Member
                      • Sep 2006
                      • 192

                      #11
                      Originally posted by jayturley
                      I have successfully implemented a solution to this problem by having two submit buttons:

                      Code:
                      <input type="submit" name="update cart" id="update cart" value="update cart" />
                      <input type="submit" name="checkout" id="checkout" value="checkout" />
                      When the form is submitted, use the server-side code to determine the value of the submit element of the POST array. Then you can determine what to do from there.

                      When you Ajaxify, you will use degradable javascript to add return false; to the onclick event handler for the submit buttons to stop the form from submitting, and since you have access to the id of the element clicked, you can submit to whatever page you desire.

                      Hope this helps,

                      -Jay
                      Hey Jay!

                      Thanks for the response. I switched over to the input type=submit and now things look like they are working great!

                      Nice work.

                      Greg

                      Comment

                      • pbmods
                        Recognized Expert Expert
                        • Apr 2007
                        • 5821

                        #12
                        Heya, Greg.

                        Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

                        Comment

                        • gregerly
                          Recognized Expert New Member
                          • Sep 2006
                          • 192

                          #13
                          Originally posted by pbmods
                          Heya, Greg.

                          Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
                          Ok, I'm really starting to think pbmods just has that phrase in his clipboard ready to paste into any thread. :) Seriously....do you?

                          Greg

                          Comment

                          • kovik
                            Recognized Expert Top Contributor
                            • Jun 2007
                            • 1044

                            #14
                            Originally posted by gregerly
                            Ok, I'm really starting to think pbmods just has that phrase in his clipboard ready to paste into any thread. :) Seriously....do you?

                            Greg
                            *Starting* to think? :P

                            Comment

                            • pbmods
                              Recognized Expert Expert
                              • Apr 2007
                              • 5821

                              #15
                              Heya, Greg.

                              Originally posted by gregerly
                              Ok, I'm really starting to think pbmods just has that phrase in his clipboard ready to paste into any thread. :) Seriously....do you?

                              Greg
                              That doesn't mean that I don't mean it :)

                              Now good luck with your project! *shove*

                              Comment

                              Working...