php count radio values - php

I got a list of 20 questions with A or B answer, and for the result I need to count certain answers together to get a score, and its A and B mixed.
Question 1 <input type="radio" name="1" value="A" />
<input type="radio" name="1" value="B" />
Question 2 <input type="radio" name="2" value="A" />
<input type="radio" name="2" value="B" />
Question 3 <input type="radio" name="3" value="A" />
<input type="radio" name="3" value="B" />
$result1 = $1A + $2B + $4A + $7B etc (count total of these answers)
$result2 = $3B + $5B + $6B etc
the results should be numeric, with checkboxes it would be easy because of unique names so values can be 1, but with radios I don't know how other then a lot of 'if 1=B then $var++', but i'm sure there is an easier way.

Here you go:
<?php
var_dump($_POST);
/*
#param array $userInput - just the $_POST
#param array $expected must be in format : [
[1,'A'],
[2,'B']
]
#return int
*/
function countExpectedAnswers(array $userInput, array $expected) /*: int*/{
$result = 0;
foreach($expected as $ex){
if(isset ($userInput[$ex[0]]) && $userInput[$ex[0]] === $ex[1]){
$result ++;
}
}
return $result;
}
$expectedAnswers = [
[1,'A'],
[2,'A'],
[3,'B']
];
echo 'Answered correctly: ' . countExpectedAnswers($_POST,$expectedAnswers);
?>
<form method = 'post'>
<div>
Question 1 <input type="radio" name="1" value="A" >
<input type="radio" name="1" value="B" >
</div>
<div>
Question 2 <input type="radio" name="2" value="A" >
<input type="radio" name="2" value="B" >
</div>
<div>
Question 3 <input type="radio" name="3" value="A" >
<input type="radio" name="3" value="B" >
</div>
<input type = 'submit' value = 'go'>
</form>

Related

Saving Radio Button Inputs into Array

I am working on a custom survey that is dynamically populated with database information and needs to be saved as an array. I can make everything work as it should except I would like to use a star rating system for many of the answers and it seems radio buttons won't work like this:
<input type="radio" name="surveyRating[]" value="1" />
<input type="radio" name="surveyRating[]" value="2" />
<input type="radio" name="surveyRating[]" value="3" />
<input type="radio" name="surveyRating[]" value="4" />
<input type="radio" name="surveyRating[]" value="5" />
Because there are multiple instances of this same code. This will be saved into an array so surveyRating will be used over and over again. If I click a radio button in one group, it changes to a radio in another group.
I have read I can possibly do this with checkboxes instead. Is that the best alternative? Or is there another option I should be looking at? I want the final product to be star ratings 1-5. Not sure if I can do that with checkboxes.
EDIT
Ok so using name="surveyRating[1]" is helping as far as the radio buttons not conflicting on the client side. However, it is not saving correctly the way I have my php set up now. Here is how it is saved currently. What do I need to change to make the [1] work appropriately. Currently it is only saving the last iteration.
$new = array();
$ratings = $_POST['surveyRating'];
$count = count( $ratings );
for ( $i = 0; $i < $count; $i++ ) {
if ( $ratings[$i] != '' ) {
$new[$i]['surveyRating'] = stripslashes( strip_tags( $ratings[$i] ) );
}
}
EDIT 2
So to demonstrate how I accomplished this using the answer below, I had to add [0] as the first iteration in the loop. I was using $items = 0; $items++ to dynamically add a number to each loop.
To make it start at 0, I set $items = -1 that way the first iteration is 0 instead of 1. Hope that makes sense.
Yep, it does because you have the name with []. That's for array. Remove it and you will get it without array:
<input type="radio" name="surveyRating" value="1" />
<input type="radio" name="surveyRating" value="2" />
<input type="radio" name="surveyRating" value="3" />
<input type="radio" name="surveyRating" value="4" />
<input type="radio" name="surveyRating" value="5" />
For radio buttons with several groups, you have to do something like this:
<!-- Group 1 -->
<input type="radio" name="surveyRating[1]" value="1" />
<input type="radio" name="surveyRating[1]" value="2" />
<input type="radio" name="surveyRating[1]" value="3" />
<input type="radio" name="surveyRating[1]" value="4" />
<input type="radio" name="surveyRating[1]" value="5" />
<!-- Group 2 -->
<input type="radio" name="surveyRating[2]" value="1" />
<input type="radio" name="surveyRating[2]" value="2" />
<input type="radio" name="surveyRating[2]" value="3" />
<input type="radio" name="surveyRating[2]" value="4" />
<input type="radio" name="surveyRating[2]" value="5" />

How will I populate value in textarea and radio buttons

I have fetch data from database in $tableRe. Now I have to print values in textarea and also have to check the radio button.
Here is my code,
$sql = "select (address, gender) from stud table";
if($result=mysqli_query($conn,$sql)) {
while($row = mysqli_fetch_array($result)) {
$tableRe[]=$row;
}
}
<form>
Address : <br>
<textarea value="<?php echo $tableRe[0]['address']; ?>"></textarea><br>
Gender : <br>
<input type="radio" value="Male">Male
<input type="radio" value="Female">Female <br>
<input type="submit" value="Save">
</form>
Please help me regarding this. Thanks in advance.
You need to apply condition on checked HTML attribute.
Try this:
<form>
Address : <br>
<textarea><?php echo $tableRe[0]['address']; ?></textarea> <br/>
Gender : <br>
<input type="radio" value="Male" <?php echo $tableRe[0]['gender'] == 'Male' ? 'checked' : ''; ?> >Male
<input type="radio" value="Female" <?php echo $tableRe[0]['gender'] == 'Female' ? 'checked' : ''; ?>>Female <br>
<input type="submit" value="Save">
</form>
Value has to be placed between the openning and closing tags :
<texterea name="whatever">Textarea value goes here</textarea>
To set a radio/checkbox as selected/checked, you need to add to it a "checkded" attribute :
<!-- HTML4 -->
<input type="radio" name="whatever" value="value1" checked="checked" /> Label 1
<input type="radio" name="whatever" value="value2" /> Label 2
<input type="radio" name="whatever" value="value3" /> Label 3
<!-- HTML5 -->
<input type="radio" name="whatever" value="value1" checked /> Label 1
<input type="radio" name="whatever" value="value2" /> Label 2
<input type="radio" name="whatever" value="value3" /> Label 3

output result calculate instantly php

I have a code here...the thing that i want is to calculate the total instantly and show in the textbox without clicking the action button.
code:
<?php
include('include/connect.php');
if(isset($_POST['enter']))
{
$name = $_POST['name'];
$score1 = $_POST['optA'];
$score2 = $_POST['optB'];
$score3 = $_POST['optC'];
$score4 = $_POST['optD'];
$total1 = $_POST['total'];
$total = ($score1 + $score2 + $score3 + $score4);
mysql_query("INSERT INTO score (name,score1,score2,score3,score4,total) VALUE ('$name','$score1','$score2','$score3','$score4','$total')");
echo "succesful";
}
else
{
echo "you fail to execute!";
}
?>
code:
<form method="post" action="index.php">
<input type="text" name="name" />
<input type="radio" name="optA" value="1" />1
<input type="radio" name="optA" value="2" />2
<input type="radio" name="optA" value="3" />3<br>
<input type="radio" name="optB" value="1" />1
<input type="radio" name="optB" value="2" />2
<input type="radio" name="optB" value="3" />3<br>
<input type="radio" name="optC" value="1" />1
<input type="radio" name="optC" value="2" />2
<input type="radio" name="optC" value="3" />3<br>
<input type="radio" name="optD" value="1" />1
<input type="radio" name="optD" value="2" />2
<input type="radio" name="optD" value="3" />3
<input type="text" name="total" value="<?php echo $total ?>" />
<input type="submit" value="enter" name="enter" />
</form>
PHP is executed before the content reaches the browser, so no elements will be loaded, so you cannot access them. It is a server-side language, not client-side; therefore you cannot do this. An example of something client-side is Javascript. However, you can create a HTML element and set its attributes, then echo it onto the page.
You'll have to use JavaScript in some form or another to not have to post the answer back to the server and wait for a response.
Here's how you can do it in JQuery.
function calculateTotal() {
var total = 0;
$('form input:radio:checked').each(function () {
total = total + parseInt($(this).val());
});
$('form [name=total]').val(total);
return false;
}
In your HTML you'll need to change your submit button to something else like this:
<span onclick="calculateTotal()">Enter</span>

PHP How to keep radio button state to the next page

I made a form with radio buttons. How can I preserve it's state after a user picked a choice? Then same form will show again in the next page and the radio button that the user picked is enabled.
//page1.html
<form method="post" action="page2.html">
<p>
<input type="radio" name="q1" value="A" />
A. <br />
<input type="radio" name="q1" value="B" />
B. <br />
<input type="radio" name="q1" value="C" />
C. <br />
<input type="radio" name="q1" value="D" />
D.
<p>
<input type="submit" name="action" value="Enter" />
</p>
</form>
To get the value of q1 on the next page, you would use $_POST['q1']. You can verify that the element has been posted, and the value matches the specific radio button by using if(isset($_POST['q1'])) && $_POST['q1'] == VALUE. So your form code would look like -
<input type="radio" name="q1" value="A" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'A')) echo 'checked="checked" ';?>/>
A. <br />
<input type="radio" name="q1" value="B" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'B')) echo 'checked="checked" ';?>/>
B. <br />
<input type="radio" name="q1" value="C" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'C')) echo 'checked="checked" ';?>/>
C. <br />
<input type="radio" name="q1" value="D" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'D')) echo 'checked="checked" ';?>/>

Radio button rating system for various categories of questions that add a score at the end [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have a php page that has a few HTML questions on it, each question has a "type" and you answer that question on a scale of 1-5, In the example below there would be 3 question types: A, B, & C. How can I add the TOTAL score for each question "type"(score is made from the radio buttons for the 1-5 scale below each question) and then have those total scores stored as PHP variables?
Here is the HTML code, I have NO CLUE where to start but I need to make this for a school event we are doing soon and I don't wanna let them down :) Haha! Thanks for all the help guys, sorry I know so little about HTML forms! Anyways, here's the HTML code, the php can go anywhere I haven't written it yet /: haha:
Type A rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
Type B rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
Type A rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
Type C rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
Type A rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
Type B rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
Type B rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
<input type="submit" name="Sum The Ratings" value="Vote">
Create a big form with all the questions in it, then for each question, set the name of the input with a keyword different for each questions. When you will treat your form, you will get with $_POST['name'] the value selected, just add them and make a ration or wathever you wan't
<?php
$name_cat_a = "A_";
$name_cat_b = "B_";
$cat_a_quest = array("Question A1", "Question A2");
$cat_b_quest = array("Question B1", "Question B2");
if(!isset($_POST[submit])){
echo '<form action="test.php" method=post>';
echo 'Type A rating:';
echo '<br />';
$ind = 0;
foreach($cat_a_quest as $question){
echo $question;
echo '<br>';
$name = $name_cat_a . $ind;
$ind ++;
for($i=0;$i<5;$i++){
echo '<input type="radio" name="'.$name.'" value="'.($i+1).'" />'.($i+1) ;
}
echo '<br />';
}
echo 'Type B rating:';
echo '<br />';
$ind = 0;
foreach($cat_b_quest as $question){
echo $question;
echo '<br>';
$name = $name_cat_b . $ind;
$ind ++;
for($i=0;$i<5;$i++){
echo '<input type="radio" name="'.$name.'" value="'.($i+1).'" />'.($i+1);
}
echo '<br />';
}
echo '<input type="hidden" name="submit" value="1" />';
echo '<input type="submit" name="Sum The Ratings" value="Vote">';
echo '</form>';
}
else{
$moyen_a = 0;
$moyen_b = 0;
$nmb_ques_a = count($cat_a_quest);
$nmb_ques_b = count($cat_b_quest);
for($i=0; $i<$nmb_ques_a; $i++){
$moyen_a = $moyen_a + intval($_POST['A_'.$i]);
}
$moyen_a = $moyen_a / $nmb_ques_a;
for($i=0; $i<$nmb_ques_b; $i++){
$moyen_b = $moyen_b + intval($_POST['B_'.$i]);
}
$moyen_b = $moyen_b / $nmb_ques_b;
echo 'A:'.$moyen_a.'<br />';
echo 'B:'.$moyen_b.'<br />';
}
?>
There i have explicitely name cat_a and cat_b but you can put all your cat in an array, then you loop into it, in this loop, you loop while ther is questions, and then you loop for the 5 answers

Categories