How to repeat radio buttons using PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Im wanting to repeat radio buttons using php. Below is the html form
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
Basically, i have 4 groups of questions with rating 1-6 (how would i do it so i dont need to write out all this html code over and over, is there a shorthand method using php so that the selected one also goes into a mysql database too?
Completely confused and new to php, any help would be great.

Look at loop for
//$i => groups (/4)
//$a => radio buttons (/6)
for($i = 1; $i <= 4; $i++){
for($a = 1; $a <= 6; $a++){
echo '<input type="radio" name="RadioGroup'.$i.'" value="'.$a.'">'.$a.'<br/>';
}
echo '<br/><br/>';
}
And the function
function loopMe($group, $answer){
for($i = 1; $i <= $group; $i++){
for($a = 1; $a <= $answer; $a++){
echo '<input type="radio" name="RadioGroup'.$i.'" value="'.$a.'">'.$a.'<br/>';
}
echo '<br/><br/>';
}
}
For use it
loopMe(3, 4);

As others have suggested, the FOR loop is your best bet here however, you can expand a little by adding simplicity and expanding the commands.
$rating = 6; //This sets the highest rating number
for($i=0 $i<$rating; $i++){
//This echos the input by rating, if rating is 6, it'll repeat 6 times.
echo "<input type=radio name=RadioGroup1 value='" . $i . "'>\r\n";
}
Now for the other half of the problem.
When posting information to a database, you need to send it to another page for processing and storage (or the same page if you handle it correctly).
This is a basic way to store THIS radio group (this is in procedural style):
$link = mysqli_connect('localhost', 'username', 'password', 'dbname');
$query = "INSERT INTO myTable (RadioGroup1) VALUES ($_POST['RadioGroup1'])";
mysqli_query($link, $query); //replace link with your database connections variable

Try a FOR loop function...as below:
<?php
for ($x=0; $x<=6; $x++)
{
echo '<input type="radio" name="RadioGroup1" value="'.$x.'">'.$x;
}
?>
To learn more about this function, visit:
http://php.net/manual/en/control-structures.for.php

see this example below... hope this helps you...
page1.php
<html>
<body>
<form id="frmQuestion" method="post" action="page2.php">
<p>Question 1</p>
<input type="radio" name="question1" value="1" checked="checked" />1
<input type="radio" name="question1" value="2" />2
<input type="radio" name="question1" value="3" />3
<input type="radio" name="question1" value="4" />4
<input type="radio" name="question1" value="5" />5
<input type="radio" name="question1" value="6" />6
<br/>
<p>Question 2</p>
<input type="radio" name="question2" value="1" checked="checked" />1
<input type="radio" name="question2" value="2" />2
<input type="radio" name="question2" value="3" />3
<input type="radio" name="question2" value="4" />4
<input type="radio" name="question2" value="5" />5
<input type="radio" name="question2" value="6" />6
<br/>
<?php
for($i=3; $i<=20; $i++)
{
echo "<p>Question " . $i . "</p>";
for ($j=1; $j<=6; $j++)
{
if($j == 1)
{
echo "<input type='radio' name='question". $i ."' value='". $j."' checked='checked' />" . $j;
}
else
{
echo "<input type='radio' name='question". $i ."' value='". $j."' />". $j;
}
}
echo "<br/>";
}
?>
<input type="submit" value="Send" />
</form>
</body>
</htm>
page2.php
<?php
echo "question 1:" . $_POST["question1"] . "<br/>";
echo "question 2:" . $_POST["question2"] . "<br/>";
echo "question 3:" . $_POST["question3"] . "<br/>";
echo "question 4:" . $_POST["question4"] . "<br/>";
// for more.
for ($n = 5; $n<=20; $n++)
{
$question = "question" . $n;
echo "question". $n . ":" . $_POST[$question] . "<br/>";
}
?>

Related

html/php forms with survey database

I hope someone can help me! I try to make a form with input from mysql. I want a statement to come from a database. Following this I want a user to be able to give a value 1-7(strongly disagree - strongly agree). I want the number of questions be based on my MySQL query. The main reason for this is that I can reuse the code for different types of surveys.
The problem is: I can't get the questions into a single form with separated values, linked to the question. I'm guessing it has something to do with the name= part. I think the name should be related to the database query somehow but I can't find out what the solution is.
If someone could help me I would be very thankful. Also if someone knows a good book or online course/video explaining how to make a online survey tool it would be very helpful.
This is what I have so far:
<?php
echo '<form action="results.php" method="post">';
global $connection;
$sql = "SELECT * FROM questions";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["question"];
echo "<br>";
echo ' <input type="radio" name="likkert" value=1>
<input type="radio" name="likkert" value=2>
<input type="radio" name="likkert" value=3>
<input type="radio" name="likkert" value=4>
<input type="radio" name="likkert" value=5>
<input type="radio" name="likkert" value=6>
<input type="radio" name="likkert" value=7>
<br>';
}
} else {
echo "0 results";
}
echo '<input type="submit"></form>';
?>
Assuming the fact that you have an id column, associated with each question, in questions table, you have to include this id column value (which should be unique) with each question's rating options, like this:
// your code
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["question"];
echo "<br />";
?>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=1>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=2>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=3>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=4>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=5>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=6>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=7>
<br />
<?php
}
} else {
echo "0 results";
}
// your code
And after form submission, process the questions and their corresponding ratings like this:
foreach($_POST['likkert'] as $questionId => $rating){
// your code
}

issue having radio button checked in session [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
i want radio buttons remain selected across all pagination. For taht
I have collected the checked value in pagination at the top of page.
But it is not working well.
What is wrong with this code ?
This is how i get selected value of radio button in session
session_start();
if(isset($_POST['answer'])) {
$_SESSION['answer'] = $_POST['answer'];
}
This is my php code
<input type="radio" name="answer" value="yes" <?php if(isset($_SESSION['answer'])=='yes') {echo "checked"; }?> />
<input type="radio" name="answer" value="no" <?php if(isset($_SESSION['answer'])=='no') {echo "checked"; }?> />
<input type="radio" name="answer" value="yes1" <?php if(isset($_SESSION['answer'])=='yes1') {echo "checked"; }?> />
<input type="radio" name="answer" value="yes2" <?php if(isset($_SESSION['answer'])=='yes2') {echo "checked"; }?> />
It gives me error(notice) like answer is undefined index.
isset() returns true or false. Your if statement should be, for example:
if (isset($_SESSION['answer']) && $_SESSION['answer'] == 'yes')
Try with:
$values = array('yes', 'no', 'yes1', 'yes2');
foreach ( $values as $value ) {
$checked = isset($_SESSION['answer']) && $_SESSION['answer'] == $value ? 'checked="checked"' : '';
echo '<input type="radio" name="answer" value="' . $value . '" ' . $checked . '/>';
}
<input type="radio" name="answer" value="yes" <?php if($_SESSION['answer']=='yes') { ?> checked="checked" <?php }?> />
<input type="radio" name="answer" value="no" <?php if($_SESSION['answer']=='no') { ?> checked="checked" <?php }?> />
<input type="radio" name="answer" value="yes1" <?php if($_SESSION['answer']=='yes1') { ?> checked="checked" <?php }?> />
<input type="radio" name="answer" value="yes2" <?php if($_SESSION['answer']=='yes2') { ?> checked="checked" <?php }?> />
please use this html

PHP, Radio Buttons return on, not value

Sorry if this is a really simple question, I am just learning php, but I've been writing a script that can create quizzes, and right now html hates me. When I click a radio button and submit my form the result is always that the button is on, not the value I assign it. Here is the excerpt of my code:
echo '<form action="index.php" method="get">';
for ($i=0; $i<$_SESSION["fsize"];$i++)
{
echo 'Type of Answer for Question ' . $i + 1 . ": <br> ";
echo '<input type="radio" name="f' . $i . ' value="radio">Radio Buttons <br>';
echo '<input type="radio" name="f' . $i . ' value="text">Text area <br>';
echo '<input type="radio" name="f' . $i . ' value="Checkboxes">Checkboxes <br>';
The html of my page, which is very incomplete, generated by my php is this:
<html>
<body>
<form action="index.php" method="post">
Number of questions:
<input type="number" name="fsize">
<input type="submit">
</form>
<form action="index.php" method="get">
1:
<br>
<input type="radio" name="f0 value="radio">
Radio Buttons
<br>
<input type="radio" name="f0 value="text">
Text area
<br>
<input type="radio" name="f0 value="Checkboxes">
Checkboxes
<br>
1:
<br>
<input type="radio" name="f1 value="radio">
Radio Buttons
<br>
<input type="radio" name="f1 value="text">
Text area
<br>
<input type="radio" name="f1 value="Checkboxes">
Checkboxes
<br>
1:
<br>
<input type="radio" name="f2 value="radio">
Radio Buttons
<br>
<input type="radio" name="f2 value="text">
Text area
<br>
<input type="radio" name="f2 value="Checkboxes">
Checkboxes
<br>
<input type="submit">
\n
</form>
</body>
</html>
I specifically switched to get rather than post so I could see my results, and this is the important part of my url /index.php?f0+value%3D=on&f1+value%3D=on&f2+value%3D=on
Does anyone know how I can get it to submit properly? and if you want to see the full php script here it is:
<html>
<body>
<?php
session_start();
echo '<form action="index.php" method="post"> Number of questions: <input type="number"
name="fsize">';
echo '<input type="submit"></form>';
if (isset($_POST["fsize"]))
{
$_SESSION["fsize"] = $_POST["fsize"];
}
if (isset($_SESSION["fsize"]))
{
echo '<form action="index.php" method="get">';
for ($i=0; $i<$_SESSION["fsize"];$i++)
{
echo 'Type of Answer for Question ' . $i + 1 . ": <br> ";
echo '<input type="radio" name="f' . $i . ' value="radio">Radio Buttons <br>';
echo '<input type="radio" name="f' . $i . ' value="text">Text area <br>';
echo '<input type="radio" name="f' . $i . ' value="Checkboxes">Checkboxes <br>';
if (isset($_GET["f" . $i]))
{
$_SESSION["f".$i] = $_GET["f".$i];
echo "I exist!";
}
if (isset($_SESSION["f".$i]))
{
echo '<textarea rows="4" cols="50"> What is the question? </textarea>';
switch($_SESSION["f".$i])
{
case("radio"):
echo "you chose a radio button!";
break;
case("text"):
echo "You chose a text area!";
break;
case("Checkboxes"):
echo "You chose checkboxes!";
break;
default:
break;
}
}
else if (isset($_GET["f".$i]))
{
echo '<textarea rows="4" cols="50"> What is the question? </textarea>';
switch($_GET["f".$i])
{
case("radio"):
echo "you chose a radio button!";
break;
case("text"):
echo "You chose a text area!";
break;
case("Checkboxes"):
echo "You chose checkboxes!";
break;
default:
break;
}
}
}
echo '<input type="submit"> </form>';
}
?>
</body>
</html>
I have a few other problems with it too I know, but this is the most infuriating.
Sorry about the length, Hovestar.
You've forgot a closing " in each name attribute of your radio controls:
<input type="radio" name="f0" value="radio">
<!-- ^ this here is important -->
Therefore, your browser thinks this radio's name attribute is "f0 value=" and is missing a value attribute. Therefore it adds the default attribute on.
Try this.
echo '<form action="index.php" method="get">';
for ($i=0; $i<$_SESSION["fsize"];$i++)
{
echo 'Type of Answer for Question ' . $i + 1 . ": <br> ";
echo '<input type="radio" name="f' . $i . '" value="radio">Radio Buttons <br>';
echo '<input type="radio" name="f' . $i . '" value="text">Text area <br>';
echo '<input type="radio" name="f' . $i . '" value="Checkboxes">Checkboxes <br>
if you have more statements make sure to close it properly using ".
This will be the answer from the Captain Obvious School of Web Design.
Are you ready for it? ... Be sure that you actually assign each radio button a value. It took me 2.5 hours of (frustrating and confusing) research to realize that I had neglected to do this. Each button had a unique id assigned which I was expecting to be returned using:
showAnswerWhen = $('input:radio[name="score"]:checked').val();
Which (of course) had no effect on this HTML:
<div class="pull-right score-bar">
Show answers:
<input type="radio" name="score" id="immediately"/>
<label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="immediately">Immediately</label>
<input type="radio" name="score" id="when" checked/>
<label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="when">When I answer</label>
<input type="radio" name="score" id="end"/>
<label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="end">At the end</label>
</div><br/>
showAnswerWhen finally had the correct value (not just 'on') with the following HTML:
<div class="pull-right score-bar">
Show answers:
<input type="radio" name="score" id="immediately" value="immediately"/>
<label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="immediately">Immediately</label>
<input type="radio" name="score" id="when" value="when" checked/>
<label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="when">When I answer</label>
<input type="radio" name="score" id="end" value="end"/>
<label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="end">At the end</label>
</div>
Turns out an id is not a value. So unreasonable! :-)

Can't access array via $_POST

I have the following html code:
<form method="post" action="arrayplay.php">
<input type="checkbox" value="1" name="todelete[]"/>
<input type="checkbox" value="2" name="todelete[]"/>
<input type="checkbox" value="3" name="todelete[]"/>
<input type="checkbox" value="4" name="todelete[]"/>
<input type="submit" value="delete" name="delete"/>
</form>
And the following PHP script:
//arrayplay.php
foreach ($_POST['todelete'] as $id)
{
echo $id . "<br/>";
}
?>
It is supposed to echo out each element value but instead I get an error. I am getting really frustrated. If I use:
<form method="post" action="arrayplay.php">
<?php
$dbc= //connection
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<input type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
echo $row['first_name'];
echo ' ' . $row['last_name'];
echo ' ' . $row['email'];
echo '<br />';
}
mysqli_close($dbc);
?>
<input type="submit" name="submit" value="Remove" />
</form>
It works perfectly fine! Why? The first (hard coded html) holds the exact same value as the one that retrieves them from the database. I am having a real hard time understanding retrieving values from an array with $_POST. Why does name=foo[] create an array? Is it an associative or numeric array? I'm sorry for all of the questions, I'm just really ready to pull my hair out.
if you just named the input foo it would only get one value. because square brackets are commonly used for arrays, foo[] is how in the html form, you indicate an array. of course on the PHP side you just call it foo as you are aware from your working example.
I've tested this and it should work:
<?php
if ($_POST['delete']) {
foreach ($_POST['todelete'] as $id) {
echo $id.' selected<br />';
}
}
?>
<form method="post" action="arrayplay.php">
<input type="checkbox" value="1" name="todelete[]"/>
<input type="checkbox" value="2" name="todelete[]"/>
<input type="checkbox" value="3" name="todelete[]"/>
<input type="checkbox" value="4" name="todelete[]"/>
<input type="submit" value="delete" name="delete"/>
</form>
If you're still having troubles, you can try:
<?php
if ($_POST['delete']) {
for ($i = 0; $i < 4; $i++) {
if (isset($_POST['todelete'][$i])) {
echo $_POST['todelete'][$i].' selected<br />';
}
}
}
?>

getting the value posted in the form in the array

I am making and computerized examination system in which a student gets 50 questions and three choices for each question. And I have displayed the question in tabs in which each tab contains 10 questions. I have made the form but confused to get the posted value.
Here is my code:
<div id="page-wrap">
<div id="tabs">
<ul>
<li>Questions 1-10</li>
<li>Questions 11-20</li>
<li>Questions 21-30</li>
<li>Questions 31-40</li>
<li>Questions 41-50</li>
</ul>
<form method="post" action="result.php" >
<?php
$data = $objQuestion_set->selectDetail($exam_id, $registration_id);
$objQuestion_bank = new Question_bank();
?>
<div id="fragment-1" class="ui-tabs-panel ui-tabs-hide">
<?php
$value[] = $data['question_1'];
$value[] = $data['question_2'];
$value[] = $data['question_3'];
$value[] = $data['question_4'];
$value[] = $data['question_5'];
$value[] = $data['question_6'];
$value[] = $data['question_7'];
$value[] = $data['question_8'];
$value[] = $data['question_9'];
$value[] = $data['question_10'];
for($i = 1; $i <=10; $i++)
{
$question = $objQuestion_bank->selectDetail($value[$i-1]);
echo "<div><p>".$i . '.' . $question['question']."</p>";
echo "<br />";
?>
<input type="radio" id="answer_1_2" name="answer_<?php echo $i;?>" value="<?php echo $question['answer_1'];?>" /><label for="answer_1"><?php echo $question['answer_1'];?></label>
<input type="radio" id="answer_1_2" name="answer_<?php echo $i;?>" value="<?php echo $question['answer_2'];?>" /><label for="answer_1"><?php echo $question['answer_2'];?></label>
<input type="radio" id="answer_1_2" name="answer_<?php echo $i;?>" value="<?php echo $question['answer_3'];?>" /><label for="answer_1"><?php echo $question['answer_3'];?></label>
<input type="hidden" id="question_id_<?php echo $i;?>" name="question_id_<?php echo $i;?>" value="<?php echo $question['question_id']; ?>"class="hidden_element" />
<?php
echo "</div>";
echo "<br /><br />";
}
?>
</div>
Now i want to get the posted value of question_id which is hidden field and print it.
You haven't really given much information, and there are some errors in your posted code, so I'm going to answer this generally and leave it to you to try to integrate it.
You are assigning essentially useless names to your form values that make it incredibly complicated to retrieve values in a meaningful way. PHP's argument parsing provides useful abilities to parse arguments into arrays, so I would recommend using them.
For instance, if you had the following input boxes and names in your form, the POSTed data would be very straightforward.
<input type="radio" name="values[<?php print $i; ?>][answer]" value="1">
<input type="radio" name="values[<?php print $i; ?>][answer]" value="2">
<input type="radio" name="values[<?php print $i; ?>][answer]" value="3">
<input type="radio" name="values[<?php print $i; ?>][answer]" value="4">
<input type="hidden" name="values[<?php print $i; ?>][question]" value="10">
Or you could just have this if the $i values isn't important.
<input type="radio" name="values[][answer]" value="1">
<input type="radio" name="values[][answer]" value="2">
<input type="radio" name="values[][answer]" value="3">
<input type="radio" name="values[][answer]" value="4">
<input type="hidden" name="values[][question]" value="10">
Both of these would make your $_POST array into a nice nested array like the following.
<?php
$_POST['values'] == array(
array(
'answer' => 3,
'question' => 1
),
array(
'answer' => 2,
'question' => 2
)
)
You haven't actually shown how you are processing the submitted values, so I can't really give more information, but this is the best way to structure form data for easy processing.
This page has some very basic examples.
http://www.askaboutphp.com/23/parsing-url-queryingstring.html

Categories