Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Validating forms using if else

    Hi everyone,

    I'm using JS to validate a (terms and conditions) checkbox that is to be used in conjunction with an 'add to cart' button. The script I'm using is:

    Code:
    <script type="text/javascript">
    	 
    function validate ( checkbox, radiobutton )
    { if (checkbox == false)
    { alert("You must agree to the Terms and Conditions before you can add to cart");}
    else
    {document.form.submit();}
    }
     </script>

    An example of a form being used for the 'add to cart' and checkbox is as follows:

    Code:
      <form name="form" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_s-xclick">
    <input type="hidden" name="hosted_button_id" value="MC5XC8DG72UME">
    <input type="hidden" name="currency_code" value="GBP">
    <input type="hidden" name="lc" value="GB">
    <table><tr><td>
    <input type="hidden" name="radioChecked" value="false">
    </td></tr></table>
    
     <a href="Javascript:validate(document.form.CheckBox.checked, document.form.radioChecked.value);">
    
     <img src="http://i1134.photobucket.com/albums/m604/franktheoilman/frankbutton.jpg" border="0" alt="PayPal — The safer, easier way to pay online"><img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1"></a><br>
     <INPUT TYPE="CheckBox" NAME="CheckBox" VALUE="CheckBox"></form>

    This works pretty well, but due to some restrictions of using paypal (that cannot be avoided) I could do with having more than one 'add to cart' button on the same page (to account for different product sizes).

    As each 'add to cart' has a unique "hosted button id" I'm assuming each button has to be contained within its own form (with its own name/id and checkbox).
    And as the code I have seems to be reliant on the form name in order to submit, the problem i'm have is how to add an additional form with its own id and have either id submit independently.

    Any help with this would be greatly appreciated!

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,628
    Thanks
    0
    Thanked 648 Times in 638 Posts
    You can do it with one form if you rearrange the fields and change the JavaScript a little:

    Code:
    <form name="form" action="https://www.paypal.com/cgi-bin/webscr" method="post" onsubmit="return validate(document.form.CheckBox.checked, document.form.radioChecked.value);"><div>
    <input type="hidden" name="cmd" value="_s-xclick">
    <input type="hidden" name="currency_code" value="GBP">
    <input type="hidden" name="lc" value="GB">
    <input type="hidden" name="radioChecked" value="false">
    <img src="http://i1134.photobucket.com/albums/m604/franktheoilman/frankbutton.jpg" border="0" alt="PayPal — The safer, easier way to pay online"><img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1"></a><br>
     <INPUT TYPE="CheckBox" NAME="CheckBox" VALUE="CheckBox">
    
    <input type="submit" name="hosted_button_id" value="MC5XC8DG72UME" value="add to cart 1">
    
    <input type="submit" name="hosted_button_id" value="xxxxxxxxxxxxxxx" value="add to cart 2">
    </div></form>
    Code:
    function validate ( checkbox, radiobutton )
    { if (checkbox == false)
    { alert("You must agree to the Terms and Conditions before you can add to cart");
       return false;}
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    croz (03-10-2012)

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by croz View Post

    As each 'add to cart' has a unique "hosted button id" I'm assuming each button has to be contained within its own form (with its own name/id and checkbox).
    Only if you want to do it the hard way.

    Your 'Add to cart' button can be any type of button. What I do, and I have my own customised php cart, is when the add button is clicked, the product id, quantity and current price for the product are sent (via AJAX) to my php cart object which stores the data in 2 associative arrays.

    On the checkout page, I retrieve the contents from the cart by looping through the 2 arrays while creating a single form, as specified in the paypal docs, to send the data for the purchased items to paypal to then handle the online payment.

    So you really need only one form.

  • Users who have thanked webdev1958 for this post:

    croz (03-10-2012)

  • #4
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Guys,

    Stephen;

    That's brilliant! Thank you very much! I have it working as I had hoped. To my shame I've been trying to figure that out for some time.

    webdev1958;

    That sounds like a great solution, but way beyond my limited abilities!

  • #5
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by croz View Post
    webdev1958;

    That sounds like a great solution, but way beyond my limited abilities!
    I hope you are at least storing your visitors' cart details on your server somehow. If not, how are you identifying whether any data sent to the paypal url is legit and actually came from your form? Anybody can view your form and attempt to buy goods at false lower prices by sending bogus data to the paypal url.

    Paypal's IPN helps identify fraudulent orders/transactions. But all that is handled server side.

  • #6
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by webdev1958 View Post

    is when the add button is clicked, the product id, quantity and current price for the product are sent (via AJAX) to my php cart object which stores the data in 2 associative arrays.

    On the checkout page, I retrieve the contents from the cart by looping through the 2 arrays while creating a single form, as specified in the paypal docs, to send the data for the purchased items to paypal to then handle the online payment.
    Hi webdev1958

    Could you point me in the right direction so I can figure out how to do this?

  • #7
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by croz View Post
    Hi webdev1958

    Could you point me in the right direction so I can figure out how to do this?
    Earlier you said:

    Quote Originally Posted by croz View Post
    webdev1958;

    That sounds like a great solution, but way beyond my limited abilities!
    Do you have a good grasp pf AJAX, PHP and PHP classes/objects and how they work? Google them first if you don't.

    But basically, when the add to cart button is clicked, I send via AJAX the productID and the quantity for the selected product to my php shopping cart class/object. The shopping cart contains methods that add/delete/modify items. You can add methods for other cart functions you require. Then throughout your online store, you call the various shopping cart methods to perform the required task at the time.
    Last edited by webdev1958; 03-15-2012 at 12:25 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •