I'm currently running on codeigniter framework and running into some trouble. I am retrieving a list of questions and a list of their possible answers (something like a questionnaire) from the database and loading it out to participants to attempt.
Due to the the for loop I'm using, I'm unable to change the name or id value of individual questions. I sort of overcome this by appending the questionID to the name/id.
The trouble comes here when i post this over to my controller. How am I able to differentiate it when I need to insert it the results back into the database?
<?php foreach($dbresult->result() as $row) {?>
<p><?php echo $row->Question ;?></p>
<?php if ($row->QuestionType == "MCQ"){ ?>
<ul>
<li><b>A: </b><?php echo $row->SelectionA; ?></li>
<li><b>B: </b><?php echo $row->SelectionB; ?></li>
<li><b>C: </b><?php echo $row->SelectionC; ?></li>
<li><b>D: </b><?php echo $row->SelectionD; ?></li>
</ul>
Select one of the following options:<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" required value="A">A<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="B">B<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="C">C<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="D">D<br>
<?php } elseif($row->QuestionType== "truefalse") {?>
Select one of the following options:<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="true">True<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="false">False<br>
<?php } else {?>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="ok">OK<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="agree">Agree<br>
<?php }?>
<?php } ?>
The above is the way I have printed the questionnaire out, any possible solutions for this?
Thats hard to manage having a name attribute like that.
Use a grouping name attribute:
name="answers[<?php echo $row->QuestionID;?>]"
Simple example:
<?php foreach($dbresult->result() as $row) {?>
<p><?php echo $row->Question ;?></p>
<?php if ($row->QuestionType == "MCQ"){ ?>
<ul>
<li><b>A: </b><?php echo $row->SelectionA; ?></li>
<li><b>B: </b><?php echo $row->SelectionB; ?></li>
<li><b>C: </b><?php echo $row->SelectionC; ?></li>
<li><b>D: </b><?php echo $row->SelectionD; ?></li>
</ul>
Select one of the following options:<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" required value="A">A<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="B">B<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="C">C<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="D">D<br>
<?php } elseif($row->QuestionType== "truefalse") {?>
Select one of the following options:<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="true">True<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="false">False<br>
<?php } else {?>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="ok">OK<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="agree">Agree<br>
<?php }?>
<?php } ?>
So that in PHP, you could just call it by its name:
public function controller_name()
{
$answers = $this->input->post('answers');
foreach($answers as $question_id => $answer) {
}
}
Related
I have two questions with radio button (each question having 4 radio) in PHP
But there is single check in all 8 radio buttons, but it should be, with 4-4 radio buttons.
Look: https://imgur.com/Sytl72a
My Code:
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<?php
$lstmt = $user->runQuery("SELECT * FROM mcq WHERE LRN=:lrn ORDER BY Sr ASC ");
$lstmt->bindparam(":lrn",$id);
$lstmt->execute();
if($lstmt->rowCount() > 0)
{
$i=0;
while($lrow=$lstmt->fetch(PDO::FETCH_ASSOC))
{
extract($lrow);
$i++;
?>
<div>
<h1><?php echo $i; ?>) <?php echo $Question; ?></h1></br>
<h2> <input type="radio" name="radio" value="<?php echo $Oa; ?>"> A) <?php echo $Oa; ?></h2>
<h2> <input type="radio" name="radio" value="<?php echo $Ob; ?>"> B) <?php echo $Ob; ?></h2>
<h2> <input type="radio" name="radio" value="<?php echo $Oc; ?>"> C) <?php echo $Oc; ?></h2>
<h2> <input type="radio" name="radio" value="<?php echo $Od; ?>"> D) <?php echo $Od; ?></h2>
</div>
<hr></br></br>
<?php
}
}
?>
<button class="btn btn-large btn-primary" type="submit" name="btn-submit">Submit</button>
</form>
<?php
}
if(isset($_POST['btn-submit']))
{
echo $ufname = trim($_POST['radio']);
}
?>
Update your radio input name attribute just like this,
<div>
<h1><?php echo $i; ?>) <?php echo $Question; ?></h1></br>
<h2> <input type="radio" name="radio<?php echo $i; ?>?>" value="<?php echo $Oa; ?>"> A) <?php echo $Oa; ?></h2>
<h2> <input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Ob; ?>"> B) <?php echo $Ob; ?></h2>
<h2> <input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Oc; ?>"> C) <?php echo $Oc; ?></h2>
<h2> <input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Od; ?>"> D) <?php echo $Od; ?></h2>
</div>
And for submitting:
<?php
}
$count = $i;
for($j = 1; $j <= $count; $j++){ if(isset($_POST['btn-submit'])) {
echo $ufname = trim($_POST['radio'.$j]);
}
}
?>
It will definitely solve your problem,
I want to get the value clicked in radio button.
This is my code:
<ul class="collapsible popout" data-collapsible="accordion" id="clicks">
<?php
foreach ($preguntas['preguntas'] as $row)
{
$opciones = $pregunta->opciones($row[0]);
?>
<li>
<div class="collapsible-header"><i class="material-icons">question_answer</i><?php echo utf8_encode($row[2]); ?></div>
<div class="collapsible-body">
<?php foreach ($opciones as $opcion){ ?>
<p class="left-align" id="options">
<input class="with-gap" name="pregunta_<?php echo utf8_encode($row[0]); ?>" type="radio" id="opcion_<?php echo utf8_encode($opcion[0]); ?><?php echo $row[0] ?>" value="<?php echo $opcion[0]; ?>" />
<label for="opcion_<?php echo $opcion[0]; ?><?php echo utf8_encode($row[0]); ?>"><?php echo utf8_encode($opcion[2]); ?></label>
</p>
<?php } ?>
</div>
<?php } ?>
</li>
</ul>
I need to get the value of this input id="opcion_..."
<p class="left-align" id="options">
<input class="with-gap" name="pregunta_<?php echo utf8_encode($row[0]); ?>" type="radio" id="opcion_<?php echo utf8_encode($opcion[0]); ?><?php echo $row[0] ?>" value="<?php echo $opcion[0]; ?>" />
<label for="opcion_<?php echo $opcion[0]; ?><?php echo utf8_encode($row[0]); ?>"><?php echo utf8_encode($opcion[2]); ?></label>
</p>
The problem is name and id is changing, it's not the same
Any idea?
Thank you.
JQuery selector for part of attribute $("[attribute^='value']") like this:
$("[id^='opcion_']").click(function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label><input name="pregunta_xxx" type="radio" id="opcion_123" value="123">radio for 123</label>
<label><input name="pregunta_xxx" type="radio" id="opcion_456" value="456">radio for 456</label>
I am trying to store the values obtained from the form into the database. I am stuck where I do not know how to obtain the values individually from foreach($questions as $row) and store them into the database.
My database table has the following columns:
variantid | stepid | questionid | newquestion | radiovalues | description
When I click on save, I am trying to save all the details obtained from the view into the database. every value obtained with each loop of foreach($questions as $row) needs to get stored in a new row. I think I should be using insert_batch, but I do not know how I can put all the data into the array.
How can I proceed with this? The following is the code that I have written. I have not written anything in model as I am stuck.
<form id="theform">
<label for="variants">Variants:</label><br>
<?php
foreach($variants as $row)
{?>
<div class="form-group">
<input type="radio" name="variants[]" checked="true" id="variants" value="<?php echo $row->variantid ?>"/><?php echo $row->variantname ?><br/>
<?php }
?>
<div id='steplist'>
<?php
foreach($substeps as $row){
?>
<div id='img_div'>
<input type="radio" name="stepid[]" checked="true" id="stepid<?php echo $row->stepid; ?>" value="<?php echo $row->stepid; ?>"/><?php echo $row->stepname ?><br/>
</div>
<?php
foreach($questions as $each){
?><br><br>
<input type="text" name="questions[]" id="questions<?php echo $each->questionid; ?>" value="<?php echo $each->questionname; ?>" readonly/><br/>
<input type="hidden" name="questionid[]" id="questions<?php echo $each->questionid; ?>" value="<?php echo $each->questionid; ?>"/>
<a id="addq">Add question</a>
<input type="text" class="form-control" name="newquestion[]" style="font-weight:bold;width:100%;height:5%" id="new_questions<?php echo $each->questionid; ?>" /><br>
<div class="radio" id="answer">
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="no" required>no</label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="probably no" required>probably no </label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="unknown" required>unknown</label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="probably yes" required>probably yes</label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="yes">yes</label>
</div>
<textarea name="question1_text[]" id="description<?php echo $each->questionid; ?>" rows="3" cols="73"></textarea><br>
<?php
}
?>
<?php
}
?>
</div>
</form>
<input type='button' value='Save' class='save' id='save' />
<script>
$(document).ready(function(){
$("#save").click(function(e){
e.preventDefault();
$.ajax({
type:"POST",
url:"<?= base_url() ?>index.php/task/perform",
data:$('#theform').serialize(),
success:function(response){
alert(response);
}
});
});
});
This is my controller:
public function perform(){
var_dump($_POST);
$variantid = $this->input->post('variants');
$stepid = $this->input->post('stepid');
$questionid = $this->input->post('questions');
$newquestion = $this->input->post('newquestion');
$radioquestions = $this->input->post('radioquestions');
$question1_text = $this->input->post('question1_text');
$this->load->model('task_model');
$result= $this->task_model->performdata();
if($result){
echo "Data saved successfully";
}
}
Update
Thank you for the quick reply. I changed the code according to the suggestion. How can I get these values into the controller and send them into the database?
The HTML inside the foreach is invalid as it is missing a closing div tag and your input does not have the correct name attribute, for multiple inputs you should use variants[] and not the same ID. Do it in this manner:
<?php foreach ($variants as $row) { ?>
<div class="form-group">
<input type="radio" name="variants[]" checked="true" class="variants" value="<?php echo $row->variantid ?>"/><?php echo $row->variantname ?><br/>
</div>
<?php } ?>
<?php
$qq=mysql_query("select * from quiz");
$row=mysql_fetch_array($qq);
//var_dump($row);
$question = $row['question'];
$_SESSION['quiz_id']=$row['id'];
$ans_array = array($row['answer1'],$row['answer2'],$row['answer3'],$row['answer4']);
shuffle($ans_array);
?>
<form name="qform" id="qform">
<h2> <?php echo $question; ?></h2>
<label><input type="radio" value="<?php echo $ans_array[0]; ?>" name="answer"> <?php echo $ans_array[0]; ?></label>
<label><input type="radio" value="<?php echo $ans_array[1]; ?>" name="answer"> <?php echo $ans_array[1]; ?></label>
<label><input type="radio" value="<?php echo $ans_array[2]; ?>" name="answer"> <?php echo $ans_array[2]; ?></label>
<label><input type="radio" value="<?php echo $ans_array[3]; ?>" name="answer"> <?php echo $ans_array[3]; ?></label>
<p id="submit-row-btn"><input type="submit" id="quiz_submit" class="q_submit" value="Submit">
<input type="submit" id="quiz_ans_submit" value="Check Answer" class="q_submit" style="display:none">
<span id="ans_err"></span></p>
</form>
In my MySQL table, I have stored;
id question answer1 answer2 answer3 answer4 ans_key date
1 1+1= 2 3 1 6 1 2014-08-03
here is ans_key;
1 =answer1
2 =answer2
3 =answer3
4 =answer4
I am using Ajax to send user's answer.
I want to check the answer key with the relevant value,
after array shuffle, how to get the key value to check if the answer is right or not?
i found the answer
<?php
$ans_array = array(1=>$row['answer1'],$row['answer2'],$row['answer3'],$row['answer4']);
$shuffleKeys = array_keys($ans_array);
shuffle($shuffleKeys);
$newArray = array();
foreach($shuffleKeys as $key)
{
$newArray[$key] = $ans_array[$key];
?>
<label><input type="radio" value="<?php echo $newArray[$key] ?>" data-key="<?php echo $key;?>" name="answer"> <?php echo $newArray[$key]; ?></label>
<?php
}
?>
here data-key holds the key value of array
I have here a numerous amount of radio buttons and their values are taken from a database. I want to add the type of leave but I can't figure it out. To make myself more clear, once the "Sick" radio button is clicked and when pressing the submit button, the "Sick" credits or the available hours for Sick leave (The value of my radio button which is the (value=$sLeave) and at the same time, the type of leave which is the Sick leave (Hidden type) will be passed to the next page.
I tried this one in every type of leave:
if($sLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $sLeave; ?>" onclick="disableTextarea()" /><label for="Sick">Sick</label>
<input type="hidden" name"leaveType" value="Sick" />
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Sick">Sick</label>
<?php
}
?>
BUT all of the type of leave will be passed to the next page and it outputs the last hidden value. Not the one that is selected.
Here's the code:
<form id="leaveForm" name="lform" method="get" action="leaveFiled.php" />
if($sLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $sLeave; ?>" onclick="disableTextarea()" /><label for="Sick">Sick</label>
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Sick">Sick</label>
<?php
}
?>
<?php
if($vLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $vLeave; ?>" onclick="disableTextarea()" /><label for="Vacation">Vacation</label>
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Vacation">Vacation</label>
<?php
}
?>
<?php
if($eLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $eLeave; ?>" onclick="disableTextarea()" /><label for="Emergency">Emergency</label>
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Emergency">Emergency</label>
<?php
}
?>
<?php
if($mLeave!=0 && $sex=='F')
{
?>
<input type="radio" name="optLeave" value="<?php echo $mLeave; ?>" onclick="disableTextarea()" /><label for="Maternity">Maternity</label>
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Maternity">Maternity</label>
<?php
}
?>
<?php
if($pLeave!=0 && $sex=='M')
{
?>
<input type="radio" name="optLeave" value="<?php echo $pLeave; ?>" onclick="disableTextarea()" /><label for="Paternity">Paternity</label>
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Paternity">Paternity</label>
<?php
}
?>
<?php
if($uLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $uLeave; ?>" onclick="disableTextarea()" /><label for="Union">Union</label>
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Union">Union</label>
<?php
}
?>
<?php
if($oLeave!=0)
{
?>
<tr>
<td height="40" colspan="3" align="left">
<strong>
<input type="radio" name="optLeave" value="<?php echo $oLeave; ?>" onclick="enableTextarea()" />
<label for="Other Leave">Other Leave</label>
<label for="Reason of Leave">Reason of Leave:</label>
<input type="text" name="reasonLeave" size="35" disabled="disabled" />
</strong>
</td>
</tr>
<?php
}
else
{
?>
<tr>
<td height="40" colspan="3" align="left">
<strong>
<input type="radio" name="optLeave" disabled="disabled" />
<label for="Other Leave">Other Leave</label>
</strong>
</td>
</tr>
<?php
}
?>
Maybe the use of javascript may solve this problem but I'm hoping for another solution to this.
If you can use diffrent prefix with unique separator like underscore() or colon(:) whatever u want in values eg. sleave for sick leave or else
<input type="radio" name="optLeave" value="<?php echo "sleave_".$sLeave; ?>" onclick="disableTextarea()" /><label for="Sick">Sick</label>
<input type="radio" name="optLeave" value="<?php echo "uleave_".$uLeave; ?>" onclick="disableTextarea()" /><label for="Union">Union</label>
then in post u can separate the value by explode function and you will get the your values.
//use same separator here
$opt_leave = explode("_", $_REQUEST['optLeave']);
$leave_type = $opt_leave[0];
$leave = $opt_leave[1];
//now you can use $leave_type to check your conditions by switch or if conditional statements
if($leave_type == 'sleave') {
// action on sick leave
} else if($leave_type == 'uleave') {
// action on Union leave
}