SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Guru dwzemens's Avatar
    Join Date
    Mar 2005
    Location
    United States
    Posts
    768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick cash, easy question: Passing radio button form variables with _POST

    First correct solution receives $10.00 via Paypal. OK, it's not much, but I bet the answer will take someone 30 seconds to post. So, really, that is an hourly rate of $1,200.00/hr. Now *that* is a lot of money.

    I am really struggling with my form validation, generating an email from the form, and displaying form information in the email.

    Here is a snippet from my form:
    <label><input id="rec_yes" type="radio" name="rec_radio" checked="checked" /> Yes</label>
    <label><input id="rec_no" type="radio" name="rec_radio" /> No</label>

    I have a radio button that simply requires the user to enter YES or NO. The form data is then sent to a PHP script that using _POST as follows:
    $rec_yes = $_POST['rec_yes'];
    $rec_no = $_POST['rec_no'];


    Then, I try to generate the text for the email as follows:
    $message .= "Yes: $rec_yes ('on' = Yes)\n";
    $message .= "No: $rec_no ('on' = No)\n";


    However, the data does not display. I have concluded that I am referring to the radio button group incorrectly in the PHP when passing the info with _POST. Can someone please help me understand the correct way to refer to this data so that I can display it in my email message?

    Thanks very much.
    Web Design, Marketing, Etc .............
    ....

  2. #2
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try to print_r($_POST) on the receiving page to see what, if anything, gets passed.

    If you want to hand out money you should have opened this in the marketplace btw .

  3. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Radio buttons have a VALUE attribute that defines what is sent when they are checked. You should define that.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a radio button that simply requires the user to enter YES or NO. The form data is then sent to a PHP script that using _POST as follows:
    Code:
    $rec_yes = $_POST['rec_yes'];
    $rec_no = $_POST['rec_no'];
    Radio buttons don't work like that. The 'name' attribute of the radio button group is what gets posted along with a value. For your radio button group, there will be one value for the name 'rec_radio':

    $value = $_POST['rec_radio'];

    and the value will be the 'value' attribute of the radio button that was selected.

  5. #5
    SitePoint Guru dwzemens's Avatar
    Join Date
    Mar 2005
    Location
    United States
    Posts
    768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have applied the VALUE to the buttons, but still no display. It seems too me (just a guess as I do not know) that I need to refer to the radio button as if it was an array or something?

    Like this:
    $rec_yes = $_POST['rec_radio[0]'];
    $rec_no = $_POST['rec_radio[1]'];

    But, that does not work, either. If I do this:
    $rec_yes = $_POST['rec_radio'];
    $rec_no = $_POST['rec_radio'];
    it assigns the value of "YES" to each variable.\

    Thanks for the suggestions, but still confused.

    - dwz
    Web Design, Marketing, Etc .............
    ....

  6. #6
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dwzemens
    It seems too me (just a guess as I do not know) that I need to refer to the radio button as if it was an array or something?
    If you had followed my advice you would know what is passed in what manner .

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Only the selected radio button will be sent, its name will be what you named it with the NAME= attribute, and its value will be whatever you specified in the VALUE= attribute of the HTML.

    HTML Code:
    <input type="radio" name="rec_radio" value="yes"> Yes
    <input type="radio" name="rec_radio" value="no"> No
    PHP Code:
    if ($_POST['rec_radio'] == "yes") {
        echo 
    "Yes was selected";
    } elseif (
    $_POST['rec_radio'] == "no") {
        echo 
    "No was selected";
    } else {
        echo 
    "Neither radio button was selected";


  8. #8
    SitePoint Guru dwzemens's Avatar
    Join Date
    Mar 2005
    Location
    United States
    Posts
    768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Dan, armed with that understanding I now made it work.

    - dwz
    Web Design, Marketing, Etc .............
    ....

  9. #9
    SitePoint Guru dwzemens's Avatar
    Join Date
    Mar 2005
    Location
    United States
    Posts
    768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you to everyone. 7stud was first with the info that got me on the right track, and Dan layed it out so even I could understand it!

    - dwz
    Web Design, Marketing, Etc .............
    ....

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A radio button group returns 'on' or 'off'(in IE6 and FF1.0) if there is no value attribute for the selected radio button--not 'yes' or 'no'.

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Send the $10 to any charitable organization that pays for the education of needy kids in Africa.

  12. #12
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Slight correction: it returns 'on' if there is no value attribute for the selected radio button, and if no radio button in the group is selected, then php will throw an undefined index error.

  13. #13
    SitePoint Guru dwzemens's Avatar
    Join Date
    Mar 2005
    Location
    United States
    Posts
    768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works:

    <label><input id="rec_yes" type="radio" name="rec_radio" checked="checked" value="Yes" /> Yes</label>
    <label><input id="rec_no" type="radio" name="rec_radio" value="No" /> No</label>

    PHP _POST code:
    $rec_it = $_POST['rec_radio'];


    MAIL function display code, which displays the VALUE of the "rec_radio" radio button:
    $message .= "Recommend product? - $rec_it \n";

    Works great, thanks.

    - dwz
    Web Design, Marketing, Etc .............
    ....

  14. #14
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's always interesting to see how people behave when money comes into play. Boy, "$1,200.00/hr. Now *that* is a lot of money."
    Also the arrogance that comes out in the person who's giving away this money. "That guy posted something that I THINK is stupid, I'm not going to dignify that with a response." You are pathetic.

    And this is still work for hire, no matter how you put it. So it should be in "looking to hire" in the marketplace where you have to pay $9,95 for a thread and not here.

    Oh, and before you think I am annoyed that I didn't get the money: I answer here because I like doing it. If I wanted money for it I'd go to the "looking to hire" section.

  15. #15
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What are you going on about?

  16. #16
    SitePoint Guru dwzemens's Avatar
    Join Date
    Mar 2005
    Location
    United States
    Posts
    768
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Icheb
    It's always interesting to see how people behave when money comes into play. Boy, "$1,200.00/hr. Now *that* is a lot of money."
    Also the arrogance that comes out in the person who's giving away this money. "That guy posted something that I THINK is stupid, I'm not going to dignify that with a response." You are pathetic.

    And this is still work for hire, no matter how you put it. So it should be in "looking to hire" in the marketplace where you have to pay $9,95 for a thread and not here.

    Oh, and before you think I am annoyed that I didn't get the money: I answer here because I like doing it. If I wanted money for it I'd go to the "looking to hire" section.
    Icheb,

    Your comments are quite harsh, don't you think. What I did was add some "humor" and a catchy phrase to this posting. How you came to the conclusion that I am "pathetic" strikes me as ridiculous.

    I admit, in retrospect, that I should not have made the offer to pay someone for their help. That was my mistake. If that violated a sitepoint policy, then I apologize, suggest I will not make the same mistake again, and stand ready to be admonished by a sitepoint forum moderator.

    However, I have no intention of being admonished by *you*, Icheb. To you I offer nothing.

    Other people who posted here made constructive comments. Your comments are off base and not warranted, nor are they wanted.
    Web Design, Marketing, Etc .............
    ....


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
  •