Thread: PHP count all radio buttons' values?

Page 1 of 5 123 ... LastLast
Results 1 to 10 of 42
  1. #1 PHP count all radio buttons' values? 
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    So basically. I got like 30 radio buttons and the values are either no or yes. So I want to use PHP to count how many yes's and how many no's there are and then go to certain function if certain number.
    Now, I got the function, if statement etc but I don't know how to count the values. I was thinking of array but then I realized I have no idea how to do that. ( Doesn't have to be array, lol )

    So if I picked 7 no's and 3 yes's then PHP would count that there are 7n and 3y -> go to certain function.
    I tried googleing it but I found nothing that I understood or maybe I'm just bad googler, lol.

    Please make it as simple and as clean as possible. Thanks in advance guys!
    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Feb 2013
    Posts
    4,409
    Thanks given
    59
    Thanks received
    478
    Rep Power
    138
    You need to set the radio button with a default name.
    name="button1"

    Code:
    if (isset($_POST['button1'])) {
    $yes += 1;
    } else {
    $no += 1;
    }
    Reply With Quote  
     

  3. #3  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Quote Originally Posted by Datbeastmayne View Post
    You need to set the radio button with a default name.
    name="button1"

    Code:
    if (isset($_POST['button1'])) {
    $yes += 1;
    } else {
    $no += 1;
    }
    If the name is the same then only one can be chosen, right? So that would leave 29 empty radio buttons? o.o
    Reply With Quote  
     

  4. #4  
    Registered Member
    Join Date
    Apr 2014
    Posts
    14
    Thanks given
    1
    Thanks received
    2
    Rep Power
    11
    Quote Originally Posted by Miko View Post
    If the name is the same then only one can be chosen, right? So that would leave 29 empty radio buttons? o.o
    Correct. you would.
    Reply With Quote  
     

  5. #5  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    29
    Posts
    763
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    A better way is to use arrays as your radio button names.

    Given this HTML code (ignore some poor style):
    Code:
    <form action="" method="post">
        <div>Q1</div>
        Yes<input type="radio" name="q1[yes]" value="1" /><br />
        No<input type="radio" name="q1[no]" value="0" /><br />
        <div>Q2</div>
        Yes<input type="radio" name="q2[yes]" value="1" /><br />
        No<input type="radio" name="q2[no]" value="0" /><br />
        <input type="submit" value="Submit" name="submit" /><br />
    </form>
    Basically what this sends is a 'value' to your server of the radio buttons that have been selected. Notice that the options for Q1 and Q2 have different names. This means that your PHP code (or whatever backend language you're using) will receive the the 'value' of each radio button that has been selected for each question. For an empty selection, your code will receive null.

    For example, if I check 'yes' for Q1 and 'no' for Q2, my PHP code receives:
    array(1) { ["yes"]=> string(1) "1" } array(1) { ["no"]=> string(1) "0" }
    My code:
    Code:
    if (isset($_POST['submit'])) {
        var_dump($_POST['q1']);
        var_dump($_POST['q2']);
    }
    Reply With Quote  
     

  6. #6  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Quote Originally Posted by Anthony` View Post
    A better way is to use arrays as your radio button names.

    Given this HTML code (ignore some poor style):
    Code:
    <form action="" method="post">
        <div>Q1</div>
        Yes<input type="radio" name="q1[yes]" value="1" /><br />
        No<input type="radio" name="q1[no]" value="0" /><br />
        <div>Q2</div>
        Yes<input type="radio" name="q2[yes]" value="1" /><br />
        No<input type="radio" name="q2[no]" value="0" /><br />
        <input type="submit" value="Submit" name="submit" /><br />
    </form>
    Basically what this sends is a 'value' to your server of the radio buttons that have been selected. Notice that the options for Q1 and Q2 have different names. This means that your PHP code (or whatever backend language you're using) will receive the the 'value' of each radio button that has been selected for each question. For an empty selection, your code will receive null.

    For example, if I check 'yes' for Q1 and 'no' for Q2, my PHP code receives:


    My code:
    Code:
    if (isset($_POST['submit'])) {
        var_dump($_POST['q1']);
        var_dump($_POST['q2']);
    }
    Guess I have to go with this until something else comes up. Too bad user can select all the radiobuttons since the name is different. Thanks anyways!

    Edit: Wait am I missing something here? It'll take too long to make all 30. It would be the same to do it with switch statement, lol.
    Reply With Quote  
     

  7. #7  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    29
    Posts
    763
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    Quote Originally Posted by Miko View Post
    Guess I have to go with this until something else comes up. Too bad user can select all the radiobuttons since the name is different. Thanks anyways!

    Edit: Wait am I missing something here? It'll take too long to make all 30. It would be the same to do it with switch statement, lol.
    What? If you don't want the different name make it the same. Keep in mind that out of all 30 radio buttons, they can only make one selection. I don't know what you're trying to do here so I'm confused as well. If you want them all with the same names but to have different selections, sounds like you need to be using checkboxes.
    Reply With Quote  
     

  8. #8  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Quote Originally Posted by Anthony` View Post
    What? If you don't want the different name make it the same. Keep in mind that out of all 30 radio buttons, they can only make one selection. I don't know what you're trying to do here so I'm confused as well. If you want them all with the same names but to have different selections, sounds like you need to be using checkboxes.
    Maybe I wasn't clear earlier. It's hard for me to describe this, lol.

    So, I'm making a test page with 30 yes and no questions. One yes is +1 points. I want PHP to echo certain thing if points are lower than x. But you see, I want it to be clean and simple. Not 30 different switch cases.
    Reply With Quote  
     

  9. #9  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    29
    Posts
    763
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    Quote Originally Posted by Miko View Post
    Maybe I wasn't clear earlier. It's hard for me to describe this, lol.

    So, I'm making a test page with 30 yes and no questions. One yes is +1 points. I want PHP to echo certain thing if points are lower than x. But you see, I want it to be clean and simple. Not 30 different switch cases.
    Are they for separate questions? The layout I had above has a yes/no box for each question.
    Reply With Quote  
     

  10. #10  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Quote Originally Posted by Anthony` View Post
    Are they for separate questions? The layout I had above has a yes/no box for each question.
    No they aren't. There is one question with 'yes' or 'no' answer choice. Multiply that with 30.

    But hmm.. I just can't picture it for some reason. Instead of printing the results, how could you add the points? Every 'yes' answer is +1 point. So how can you calculate the points with that method of yours?
    Reply With Quote  
     

Page 1 of 5 123 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Change content of dropdown list depending on radio buttons
    By Aekramer in forum Application Development
    Replies: 3
    Last Post: 01-22-2013, 02:46 AM
  2. [569]all logout button and interfaces.
    By Discardedx2 in forum Snippets
    Replies: 5
    Last Post: 11-22-2009, 02:11 AM
  3. [HTML+PHP]NEARLY all of http://www.runescape.com + Updated
    By Jammy780 in forum Application Development
    Replies: 45
    Last Post: 05-07-2009, 08:25 AM
  4. Replies: 8
    Last Post: 02-14-2008, 11:07 AM
  5. Resetting all buttons [ANTI LEECH]
    By Diablo1123 in forum Tutorials
    Replies: 15
    Last Post: 05-25-2007, 06:45 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •