View Full Version : PHP radio button hadling
calumogg
10-15-2007, 01:35 PM
Hi,
On my checkout page I would like to add the option of either paying but cheque or by paypal. So on the HTML form I was going to add two radio buttons one for paypal and one for paying by cheque. So if the user click on the paypal one the PHP will direct them to paypal or if they select cheque it will just send them a e-mail.
Please could someone give me a example code that I can work on? Thanks
djr33
10-15-2007, 06:14 PM
Checking the input:
if ($_POST['radiofield']=='radiovalue') { ..... }
Redirecting [note: must be sent before any text output on the page]:
header('Location: http://paypal.com/pay/info/here/etc');
calumogg
10-15-2007, 06:34 PM
Cheers for the reply, would the below code work?
if ($_POST['radiofield']=='radiovalue') { ..... }
else ($_POST['radiofield']=='radiovalue2') {.....}
djr33
10-15-2007, 06:43 PM
I assume you realize this, but .... must be replaced with some action, like the redirect code.
if (condition) { this }
else { this }
There is no condition for an else, because that is the inverse of if--
if (condition) { this }
if (^not if^) { this }
You could use a second if statement:
if (condition1) { this }
else if (condition2) { this}
It depends on the behavior.
With checkboxes, I would suggest the if; else if method.
But with radio buttons where you expect one or the other, the if; else method will work fine.
Think of it like this:
if (exception) { this }
else { default }
And, in fact, you could just have if (exception) { redirect }, then have the other option output directly with no need for an else at all, because the redirect will make sure that the text is never seen if the exception is true.
Using if (exception) { this } else { default } will also cover any errors, where there wasn't a value or it was sent as something else, someone tried to hack the page, etc.
Example:
if ($_POST['gender']=='male') { $g = 'male'; } //if specifically 'male' was chosen set as male
else { $g = 'female'; } //anything else, including errors or random data, set as female.
calumogg
10-15-2007, 06:47 PM
Rite that makes snese, I just wasnt sure about the if else bit.
Thanks that just helped alot more stuff make sens!!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.