Results 1 to 5 of 5

Thread: PHP radio button hadling

  1. #1
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default PHP radio button hadling

    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

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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');
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Cheers for the reply, would the below code work?

    Code:
    if ($_POST['radiofield']=='radiovalue') { ..... }
    else ($_POST['radiofield']=='radiovalue2') {.....}

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Rite that makes snese, I just wasnt sure about the if else bit.
    Thanks that just helped alot more stuff make sens!!

Bookmarks

Posting Permissions

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