Forum Moderators: coopster
I'm trying to put together a donations page for our charity. It links through to WorldPay (a payment processor) and I've got most of it set up except for a problematic radio button. I don't know much PHP I'm afraid so I'm hoping some generous soul might be able to fill in the blanks for me.
an edited version of things is this:
<form action="https://select-test.worldpay.com/wcc/purchase" method=POST>
I have a text input box in which people type in an amount they would like to donate - name is "donation"
<input name="donation" type="text">
There are two radio buttons so people can choose whether the donation amount is a one-off donation or whether they want to donate that amount every month.
<li><input type="radio" checked="checked"
name="type" value="single"> Single donation
</li>
<li><input type="radio"
name="type" value="regular"> Every month</li>
Basically, I need a PHP condition which says:
IF type=regular then post these values
<input type=HIDDEN NAME="NormalAmount" value="$DONATION"?>
<INPUT TYPE=HIDDEN NAME="futurePayType" VALUE="regular">
<INPUT TYPE=HIDDEN NAME="option" VALUE="0">
<INPUT TYPE=HIDDEN NAME="intervalUnit" VALUE="3">
<INPUT TYPE=HIDDEN NAME="intervalMult" VALUE="1">
<INPUT TYPE=HIDDEN NAME="currency" VALUE="GBP">
IF type=single then post these values
<INPUT TYPE=HIDDEN NAME="amount" VALUE="$DONATION">
The values are all WorldPay ones - you don't need to worry about those as I have checked them separately and they work fine.
It's the bits in bold that I need translating into PHP.
Thank you in advance for any help offered...
Ben
<?php// We check if the user pressed
// the submit button
if( isset( $_POST[ "submit" ] ) ) {
// We check if they chose single or regular.
// This simply finds out what the user chose.
switch( $_POST[ "type" ] ) {
case "single":
// Do whatever you want here if they chose single
echo '<form action="' . $_SERVER[ "PHP_SELF" ] . '" method="post">
<input type="hidden" name="amount" value="single">
<input type="submit" name="continue" value="Click here to continue...">
</form>';
break;
case "regular":
// Do whatever you want here if they chose regular
echo '<form action="' . $_SERVER[ "PHP_SELF" ] . '" method="post">
<input type="hidden" name="amount" value="regular">
<input type="submit" name="continue" value="Click here to continue...">
</form>';
break;
default:
// If it was none of these (it's possible if they sent
// the information through a different HTML code)
echo "You didn't choose either of the possible choices!";
}
// If they haven't pressed the submit button,
// check if they're trying to confirm that
// they want to make their donation
} elseif( isset( $_POST[ "continue" ] ) ) {
// Do what you want here once they've confirmed that
// the want to continue
echo "You decided to continue with the value '" . $_POST[ "amount" ] . "'.";
// If it's neither of these cases, we show
// the first form ot them.
} else {
$str = '<form action="' . $_SERVER[ "PHP_SELF" ] . '" method="post">
<ul>
<li><input type="radio" checked="checked" name="type" value="single">Single donation</li>
<li><input type="radio" name="type" value="regular">Every month</li>
</ul>
<input type="submit" name="submit" value="Submit">
</form>';
echo $str;
}?>
[edited by: McBlack at 6:43 pm (utc) on Dec. 26, 2008]
Thanks very much for your reply - I really appreciate your help. I've been playing around with the code you sent me for a couple of hours but can't seem to get it to work with what I've got already - I think it's because I didn't explain the current situation propperly to begin with.
I have a donations page that collects amount, type, address, name, postcode, email etc and when people click the button at the bottom it sends it all by POST to WorldPay. That provides a confirmation screen so I want the process on our site to all happen in one page.
A rough mockup can be seen here <snip> It probably won't be clicking through properly though.
So - it's basically what I had in my original post, but i didn't mention that there are other form input fields in the form too. The data inputted into these other fields stays the same regardless of which of the radio buttons is clicked.
If you POST these variables (along with the name, address etc ones) to WorldPay then it knows to process it as a monhtly recurring transaction:
<input type=HIDDEN NAME="NormalAmount" value="$DONATION"?>
<INPUT TYPE=HIDDEN NAME="futurePayType" VALUE="regular">
<INPUT TYPE=HIDDEN NAME="option" VALUE="0">
<INPUT TYPE=HIDDEN NAME="intervalUnit" VALUE="3">
<INPUT TYPE=HIDDEN NAME="intervalMult" VALUE="1">
and if you post these ones:
<INPUT TYPE=HIDDEN NAME="amount" VALUE="$DONATION">
(plus the name, address etc)
then it knows to process it as a single donation.
In my naivety I thought that the (if, else) condition with the radio buttons could be achieved with a couple of lines of PHP but now I think that it probably isn't that simple?
Thanks,
Ben
[edited by: eelixduppy at 6:37 am (utc) on Dec. 27, 2008]
[edit reason] no URLs, please [/edit]
<?php$donation = "10.50"; // Amount of money to donate
$type = "regular"; // Regular or single?>
<form action="https://select-test.example.com/wcc/purchase" method="post">
<input type=hidden name="NormalAmount" value="<?=$donatin?>">
<input type=hidden name="futurePayType" value="<?=$type?>">
<input type=hidden name="option" value="0">
<input type=hidden name="intervalUnit" value="3">
<input type=hidden name="intervalMult" value="1">
<input type="submit" value="Click here to donate $10.50">
</form>
Or, if you have it on your sever, you could use cURL.
<?php$donation = "10.50"; // Amount of money to donate
$type = "regular"; // Regular or single$ch = curl_init(); // New cURL
$post = array( "NormalAmount" => $donation,
"futurePayType" => $type,
"option" => "0",
"intervalUnit" => "3",
"intervalMult" => "1" ); // This is our post datacurl_setopt( $ch, CURLOPT_URL, "https://select-test.example.com/wcc/purchase" ); // Our target website
curl_setopt( $ch, CURLOPT_POST, 1 ); // Enable post data
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post ); // Send post datacurl_exec( $ch ); // Execute
?>
[edited by: eelixduppy at 6:34 pm (utc) on Dec. 28, 2008]
[edit reason] example.com [/edit]