I would like to create a html quiz with a dynamic number of questions and each question has a dynamic number of answers. My goal is to submit all of the questions when the quiz is completed. What changes to my HMTL do I need to make and what php do I use to get the array?
HTML
<form action="process.php" method="post">
<div class="questions">
<strong class="white">Question 1</strong>
<input name="questions[]" type="hidden" value="1">
<input name="answers[]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[]" type="checkbox" value="d"> <label>answer 4</label><br>
</div>
<div class="questions">
<strong class="white">Question 2</strong>
<input name="questions[]" type="hidden" value="2">
<input name="answers[]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[]" type="checkbox" value="d"> <label>answer 4</label><br>
<input name="answers[]" type="checkbox" value="e"> <label>answer 5</label><br>
<input name="answers[]" type="checkbox" value="f"> <label>answer 6</label><br>
</div>
</form>
PHP
$questionID = $_POST['questions'];
$answers = $_POST['answers'];
foreach( $questionID as $i => $qid ) {
echo "The question id is ".$qid." and selected answer(s) is ".$answers[$i]. "<br>";
}
I believe one of my html problems has to do with the answers are named the same for both questions. Another php problem is which answer(s) were selected. I do not want to use AJAX as that causes more load on the server and I use JS for a quiz timer. Thanks for your help.
I think this will do what you want:
$questionAnswers = array('question1'=>array('answer1','answer2','answer3','answer4'));
$questionText = array('question1'=>'Question 1');
Displaying the form:
<form>
<?php foreach($questionAnswers as $q => $answers)
{
echo '<div class="questions"><strong class="white">'.$questionText[$q].'</strong>'
for($i = 0; $i < count($answers); $i++)
{
echo '<input name="'.$q.'[]" type="checkbox" value="'.$i.'"> <label>'.$answers[$i].'</label><br>';
}
echo '</div>';
}
?>
</form>
Processing submission:
$foreach($questionAnswers as $q => $answers)
{
$userAnswers = isset($_REQUEST[$q]) ? $_REQUEST[$q] : array();
...
}
You should specify the number of the question in the input name attribute, e.g.:
<form action="process.php" method="post">
<div class="questions">
<strong class="white">Question 1</strong>
<input name="questions" type="hidden" value="1">
<input name="answers[1]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[1]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[1]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[1]" type="checkbox" value="d"> <label>answer 4</label><br>
</div>
<div class="questions">
<strong class="white">Question 2</strong>
<input name="questions" type="hidden" value="2">
<input name="answers[2]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[2]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[2]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[2]" type="checkbox" value="d"> <label>answer 4</label><br>
<input name="answers[2]" type="checkbox" value="e"> <label>answer 5</label><br>
<input name="answers[2]" type="checkbox" value="f"> <label>answer 6</label><br>
</div>
</form>
Then when you get a $_POST it will be like:
answers[1] = 'a',
answers[2] = 'b' ....
Related
I created a page for multiple rows submit data to mysql with php!
But, I need filter check the checkbox[] has been checked for submit current row data
In my demo,
If I checked the row2 and row3, I expected I will get id=2 & id=3
finally I get the id=1 & id=2
In the same situation, if I checked row3 only, I will get the id=1
I probably understand the principle, but I really can’t find a solution
<?php
$row = "";
if ($_POST) {
foreach ($_POST["checked"] as $key => $v) {
if (#$_POST['checked'][$key] == "on") {
$row[$key]['id'] = $_POST['id'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
}
print_r($row);
?>
<form action="" method="POST">
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="1">
<input type="text" name="other_value[]" value="a">
</p>
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="2">
<input type="text" name="other_value[]" value="b">
</p>
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="3">
<input type="text" name="other_value[]" value="c">
</p>
<button type="submit">submit</button>
</form>
I try #CBroe
if checked row3, I still get a
<?php
$row = "";
if ($_POST) {
foreach ($_POST["checked"] as $key => $v) {
$row[$key]['checkbox'] = $_POST['checkbox'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
print_r($row);
?>
<form action="" method="POST" >
<p>
<input type="checkbox" name="checked[]" value="1">
<input type="text" name="other_value[]" value="a">
</p>
<p>
<input type="checkbox" name="checked[]" value="2">
<input type="text" name="other_value[]" value="b">
</p>
<p>
<input type="checkbox" name="checked[]" value="3">
<input type="text" name="other_value[]" value="c">
</p>
<button type="submit">Submit</button>
</form>
#CBroe Thanks for your
<?php
$row = "";
if ($_POST) {
foreach ($_POST["id"] as $key => $v) {
$row[$key]['id'] = $_POST['id'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
print_r($row);
?>
<form action="" method="POST" >
<p>
<input type="checkbox" name="id[1]" value="1">
<input type="text" name="other_value[1]" value="a">
</p>
<p>
<input type="checkbox" name="id[2]" value="2">
<input type="text" name="other_value[2]" value="b">
</p>
<p>
<input type="checkbox" name="id[3]" value="3">
<input type="text" name="other_value[3]" value="c">
</p>
<button type="submit">submit</button>
</form>
I'm new to php and html and I was wondering, how having a code like below:
<!DOCTYPE html>
<html>
<body>
<form>
<p>Question Number 1</p>
<input type="radio" name="question1" value="A">A<br>
<input type="radio" name="question1" value="B">B<br>
<input type="radio" name="question1" value="C">C<br>
<input type="radio" name="question1" value="D">D
<p>Question Number 2</p>
<input type="radio" name="question2" value="A">A<br>
<input type="radio" name="question2" value="B">B<br>
<input type="radio" name="question2" value="C">C<br>
<input type="radio" name="question2" value="D">D
<p>Question Number 3</p>
<input type="radio" name="question3" value="A">A<br>
<input type="radio" name="question3" value="B">B<br>
<input type="radio" name="question3" value="C">C<br>
<input type="radio" name="question3" value="D">D
<p>Question Number 4</p>
<input type="radio" name="question4" value="A">A<br>
<input type="radio" name="question4" value="B">B<br>
<input type="radio" name="question4" value="C">C<br>
<input type="radio" name="question4" value="D">D
</form>
</body>
</html>
I want to show a question one bye one with button next and with check if
a value is null. What should I use? PHP or Javascript.
$('input[type="radio"]').on('change', function(e) {
$('#'+e.target.name+'').hide();
var nextRadioGroupName = "question" + (parseInt(e.target.name.replace('question', '')) + 1);
$('#'+nextRadioGroupName+'').show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<form>
<div id = "question1">
<p>Question Number 1</p>
<input type="radio" name="question1" value="A">A<br>
<input type="radio" name="question1" value="B">B<br>
<input type="radio" name="question1" value="C">C<br>
<input type="radio" name="question1" value="D">D
</div>
<div id = "question2" style="display:none">
<p>Question Number 2</p>
<input type="radio" name="question2" value="A">A<br>
<input type="radio" name="question2" value="B">B<br>
<input type="radio" name="question2" value="C">C<br>
<input type="radio" name="question2" value="D">D
</div>
<div id = "question3" style="display:none">
<p>Question Number 3</p>
<input type="radio" name="question3" value="A">A<br>
<input type="radio" name="question3" value="B">B<br>
<input type="radio" name="question3" value="C">C<br>
<input type="radio" name="question3" value="D">D
</div>
<div id = "question4" style="display:none">
<p>Question Number 4</p>
<input type="radio" name="question4" value="A">A<br>
<input type="radio" name="question4" value="B">B<br>
<input type="radio" name="question4" value="C">C<br>
<input type="radio" name="question4" value="D">D
</div>
</form>
Firstly add a div element for per question group to easily show/hide them
Define change event with Jquery and find the next question group as iteration like "question1", "question2" etc.
Then show the next question group
Code for RadioButton:
<h1 style="margin:0; margin-top:10px; padding:0; padding-left:25px; padding-bottom:10px; font-family:sans-serif;">
</h1>
<div style="background:#1794FF; color:#fafafa; padding:10px;">
<h3></h3>
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" />
<label for="radio3" class="css-label">Near Zipcode</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" />
<label for="radio1" class="css-label">City-Wide</label>
</td>
</tr>
</table>
</div>
<div style="background:#1794FF; color:#222; padding:10px;">
Php Code:
<?PHP
$selected_radio = $_POST['radiog_lite'];
print $selected_radio;
?>
After a form submission I arrive at the code above. However, it says the value is "on". Why isn't it printing the chosen radio button name?
May you can assign a value:
PHP Code
<form method="POST">
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="1" />
<label for="radio1" class="css-label">Neighbourhood Only</label><br>
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked" value="2"/>
<label for="radio2" class="css-label">Zipcode Only</label><br>
<input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" value="3" />
<label for="radio3" class="css-label">Near Zipcode</label><br>
<input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" value="4" />
<label for="radio1" class="css-label">City-Wide</label><br>
<input type="submit" value="Submit">
</form>
PHP Code:
<?php
$selected_radio = $_POST['radiog_lite'];
print $selected_radio;
?>
A radio button is has a default value of "on", you should specify your value in your input initialization like so:
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" value="Your Value Here" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>
So you can replace "Your Value Here" with "Zipcode Only" for this one. The label is just to describe the radio button value for the front-end user.
Use a value attribute on your input radio.
Use form method "post" and also put value for each radio:
<form method="post">
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="Neighbourhood Only" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<tr>
</table>
<input type="submit" />
</form>
I am writing a php site that has a form with a series of check boxes. I will be loading an array from a file that I would like to go through and check some of the boxes by default when the form is loaded.
Here is an example:
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" />
</form>
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
Is this possible? What would be the best way to do it.
You should fill your array before the HTML part. And then:
<input type="checkbox" name="option1" value="option1" <?php if (in_array("option1", $array)) { echo 'checked="checked"'; } />
Try this :
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" <?php if(in_array("option1",$array)){?> checked="checked"<?php}?> />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" <?php if(in_array("option2",$array)){?> checked="checked"<?php}?> />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" <?php if(in_array("option3",$array)){?> checked="checked"<?php}?> />
</form>
i want insert this form value to datanase :
<input type="checkbox" name="brand1" id="brand1" value="1"> <label for="brand1">Brand 1</label>
<input type="checkbox" name="brand2" id="brand2" value="1"> <label for="brand2">Brand 2</label>
<input type="checkbox" name="brand3" id="brand3" value="1"> <label for="brand3">Brand 3</label>
<input type="checkbox" name="brand4" id="brand4" value="1"> <label for="brand4">Brand 4</label>
<input type="checkbox" name="brand5" id="brand5" value="1"> <label for="brand5">Brand 5</label>
these text box are get by php from a table in database and may be Variable
i want insert to database by this format
if brand 1 are checked $brand="1,";
and Finally like this :
insert($name,$brands); and $brands = "1,2,3,4,5,";
if write this by if and while but it doesn't work because if insert run in while {} Five times insert Done and if insert run out of while {} , $brand = "5,"
thanks for your help or idea for this problem
it's mean :
<form method="post" action="#">
<?php
$result = $db->getall(brands);
if(!empty($result)) {
while ( list($key,$val)=each($result) ) {
$brand_id = stripslashes($val["id"]);
$brand_name = stripslashes($val["name"]);
?>
<input type="checkbox" name="brand<?php print"$brand_id"; ?>" value="1" style="cursor:pointer;"><label for="brand<?php print"$brand_id"; ?>" style="cursor:pointer;"> <?php print"$brand_name"; ?></label>
<?php }} ?>
Source Output:
<input type="checkbox" name="brand1" value="1"> <label for="brand1">Brand Name 1</label>
<input type="checkbox" name="brand2" value="1"> <label for="brand2">Brand Name 2</label>
<input type="checkbox" name="brand3" value="1"> <label for="brand3">Brand Name 3</label>
<input type="checkbox" name="brand4" value="1"> <label for="brand4">Brand Name 4</label>
<input type="checkbox" name="brand5" value="1"> <label for="brand5">Brand Name 5</label>
<input type="submit" value="Submit" />
</form>
when submit form , insert source is :
<?php
$result = $db->getall(brands);
if(!empty($result)) {
while ( list($key,$val)=each($result) ) {
$brand_id = brand.stripslashes($val["id"]);
$brand_name = stripslashes($val["name"]);
$brand_ids = "brand.$brand_id";
if($$brand_ids==1) {$brands="$brandid,"}
}} ?>
$db->add_submenu("$brands");
You should change the name of your checkboxes to brand[]. It will give you an array once submitted at $_POST['brand']
Ex.
<input type="checkbox" name="brand[]" value="1" ... />
<input type="checkbox" name="brand[]" value="2" ... />
<input type="checkbox" name="brand[]" value="3" ... />
<input type="checkbox" name="brand[]" value="4" ... />
<input type="checkbox" name="brand[]" value="5" ... />
on the other side you can either do something like the following:
// this will return '1, 2, 3, 4, 5' when all are selected.
$index = implode(", ", $_POST['brand']);
and at that point you will have the brands in comma delimited form.