You need to set the radio button with a default name.
name="button1"
Code:if (isset($_POST['button1'])) { $yes += 1; } else { $no += 1; }
|
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!
You need to set the radio button with a default name.
name="button1"
Code:if (isset($_POST['button1'])) { $yes += 1; } else { $no += 1; }
A better way is to use arrays as your radio button names.
Given this HTML code (ignore some poor style):
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.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>
For example, if I check 'yes' for Q1 and 'no' for Q2, my PHP code receives:
My code:array(1) { ["yes"]=> string(1) "1" } array(1) { ["no"]=> string(1) "0" }
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.
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.
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?
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |