Hi sevenupcan,
Sorry I have been really busy and have not been around the forums.
Your code above is absolutely fine but I have re-written it just to give you some ideas about other things you can do...
PHP:
// These functions can potentially be in another includes file or class
function validate_numeric($testme) {
$reg_ex = "/^[0-9]+$/";
return preg_match($reg_ex, $testme);
}
function doTotal($qty, $price, $fee = 250){
$total = $qty * $price;
if($qty == 1){
$total += $fee;
}
return $total;
}
// Process the cost
$totalcost = 0;
// first validate that the qty and price are only numerical as these have been "posted"
if(!validate_numeric($_POST['qty']) || !validate_numeric($_POST['price'])){
// do something here to inform the user that invalid data was entered
// you could redirect the page, display error message etc
} else {
$totalcost = doTotal($_POST['qty'], $_POST['price']);
}
You might notice that there are 2 big changes with the method doTotal(), one is that I have removed the need to announce the variables as global and the other is that I have added this "$fee = 250" in the method parameters.
Adding a default value for a variable in the method call means that you dont need to pass it in every time. You can now call doTotal in two ways ..
PHP:
$total = doTotal(1, 10); // this means that fee will be 250 by default
$total = doTotal(1, 10, 450); // this means we just set "fee" to 450.
The reason to remove the need for global variables is that with your doTotal method, you will always need to have the variables $fee, $price and $qty defined in your script. Whereas, as you can see in my example usage above, you could pass any parameters you like in.
sevenupcan said:
One thing I have noticed is that my variables which are posted from the page before aren't being echoed when the user reaches the PayPal/Cheque section of the form and I'm not sure why. It's almost as if they are being forgotten when the form is submitted. Do I need to re-include these values to carry them on?
This is the exact problem that I mentioned above, and why I mentioned that Jaz's solution wont work.
When you POST a HTML form the values from the form are entered into the $_POST array and sent to the server. If you then go to another link or page, these posted values are lost. You either need to repost manually using something like
cURL or
fsockopen() which is just a pain in the butt and a little too much.
If you "pass on the values" again via GET instead of POST i.e. adding them to the url like mypage.php?value1=x&value2=x this is not a good solution when working with payment processors since browsers have limitations to the length of a string. You also need to encode the URL properly.
So, my original solution is still the one that I would go for. I have worked with paypal intergration a lot and it is very easy to work with but I would always POST the data.
One other solution which might be the best for you, is to include the values that were posted into another form with hidden fields (final confirmation page comes to mind) and then have that post to paypal or whatever you need.
Something like
1. (payment.php) User -> Payment Page (selects payment type here ex paypal or cheque)
2. (payment.php) User -> clicks on Send which takes them to
3. (confirm.php) Details in the POST data are entered into hidden fields in another HTML Form and the URL for the FORM is changed based on the selection that the user made (cheque/paypal)
4 (confirm.php) User clicks on Submit/Send/Confirm
5. User either arrives at paypal or the cheque page.
Darren