...

View Full Version : Validating forms using if else



croz
03-10-2012, 06:32 PM
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:


<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:


<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!

felgall
03-10-2012, 09:09 PM
You can do it with one form if you rearrange the fields and change the JavaScript a little:


<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>


function validate ( checkbox, radiobutton )
{ if (checkbox == false)
{ alert("You must agree to the Terms and Conditions before you can add to cart");
return false;}
}

webdev1958
03-10-2012, 09:28 PM
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.

croz
03-10-2012, 11:03 PM
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!

webdev1958
03-10-2012, 11:38 PM
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.

croz
03-14-2012, 10:10 PM
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?

webdev1958
03-15-2012, 12:21 AM
Hi webdev1958

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

Earlier you said:



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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum