<?php
$ans=$_REQUEST['ans'];
$qes=$_REQUEST['qes'];
$ra=$_REQUEST['right_op'];
$count=0;
for($i=0; $i<count($ans); $i++)
{
echo "Question".$qes[$i]"<br>";
echo "Ans".$ans[$i]"<br>";
echo "Right Option".$ra[$i]."<br>";
if(isset($ans[$i]) == isset($ra[$i]))
{
$count++;
}
}
?>
when submit the first page, then it shows
Notice: Undefined offset: 1 in C:\xampp\htdocs\result.php on line 9
The first page where the data is posted.
<?php
$i=1;
$x=0;
$y=0;
$z=0;
do{
?>
<tr>
<td width="30"></td>
<td width="30" height="27"><?php echo "$i";?></td>
<td width="493"><?php echo $row_question['question']; ?>
<input type="hidden" name="q_id[<?php// echo $y; ?>]" id="q_id" value=" <?php echo $row_question['q_id']; ?>" />
<input type="hidden" name="qes[<?php echo $y; ?>]" id="qes" value="<?php echo $row_question['question']; ?>" /><input name="right_op[<?php echo $z; ?>]" type="hidden" id="right_op" value="<?php echo $row_question['right_op']; ?>" /></td>
</tr>
<tr>
<td> </td>
<td height="59" align="right"><h3>A)</h3>
<h3>B)</h3>
<h3>C)</h3>
<h3>D)</h3></td>
<td><h3>
<label>
<input type="radio" name="ans[<?php echo $x; ?>]" value="A" id="ans_0" />
<font color="#FFFFFF"><?php echo $row_question['op_a']; ?></font> </label>
<br />
<label>
<input type="radio" name="ans[<?php echo $x; ?>]" value="B" id="ans_1" />
<font color="#FFFFFF"><?php echo $row_question['op_b']; ?></font> </label>
<br />
<label>
<input type="radio" name="ans[<?php echo $x; ?>]" value="C" id="ans_2" />
<font color="#FFFFFF"><?php echo $row_question['op_c']; ?></font> </label>
<br />
<label>
<input type="radio" name="ans[<?php echo $x; ?>]" value="D" id="ans_3" />
<font color="#FFFFFF"><?php echo $row_question['op_d']; ?></font> </label>
<br />
</h3></td>
</tr>
<tr>
<td height="17" colspan="3"><hr /></td>
</tr><?php
$i++;
$x++;
$y++;
$z++;
} while ($row_question = mysql_fetch_assoc($question)); ?>
What is wrong in this code. Please tell me in details.
Thank You.
Always use isset() before access
if (isset($ra[$i]) && isset($ans[$i]) && isset($qes[$i])){
// your code
}
or
array_key_exists($i, $ra);
or
It looks like you expect all arrays to be the same size, which is not the case for $ra which is snorter than other arrays, therefore
echo "Right Option".$ra[$i]."<br>";
causes the notice. Looks like you need to fix your $ra size.
$ra doesn't have as many members in the array as $ans does, so when you hit the 2nd iteration of the loop there's nothing in $ra[1] to display. You need to figure out why the length of $ra and $ans are different - without more information about what you're doing it's difficult for us to know.
As #Nikola indicates, you should always check to make sure there's something in the index you're accessing before trying to output it using isset(). Whether or not it's valid for there not to be something in $ra[1] when there is something in $ans[1] is something only you can answer.
Inferring a lot from your code, it looks as though $ra is intended to indicate the right answer. In that case there's only going to be one, so you should use $ra[0] instead of $ra[$i], which will always show the first element in the $ra array. Of course I may be misinterpreting the intent of your code, so YMMV.
Related
I'm trying make a form where me or user can insert data. Some parts are coming form data base like: Work name and work price. In input fields can insert work amount. In each input row have checkbox, if checkbox ar checked, that row will be seen in php.
<table class="table">
<thead>
<tr>
<th scope="col">*</th>
<th scope="col">Work name</th>
<th scope="col">Quantity</th>
<th scope="col">Price for unit</th>
<th scope="col">Total price</th>
</tr>
</thead>
<form method="POST" action="process/pdf.process.php">
<tbody>
<?php
$works = ORM::for_table('work')->find_many();
foreach($works as $work){
?>
<tr id="<?php echo $work->id; ?>">
<td><input type="checkbox" name="useit[]" value="<?php echo $work->id; ?>"/></td>
<td><?php echo $work->name; ?></td>
<td><input type="text" placeholder="" class="amount"/> <?php echo $work->unit; ?></td>
<td class="work_price" data="<?php echo $work->price; ?>"><?php echo $work->price.'€/'.$work->unit; ?></td>
<td class="total_price">0€</td>
<input type="" name="work_id[]" value="<?php echo $work->id; ?>" />
<input type="hidden" name="work_name[]" value="<?php echo $work->name; ?>" />
<input type="hidden" name="amount[]" class="<?php echo $work->id; ?>_copy_amount" value="" />
<input type="hidden" name="unit[]" value="<?php echo $work->unit; ?>" />
<input type="hidden" name="unit_price[]" value="<?php echo $work->price; ?>€" />
<input type="hidden" name="unit_total[]" class="<?php echo $work->id; ?>_copy_total" value="" />
</tr>
<?php
}
?>
</tbody>
<input type="submit" name="do_pdf" value="Pga jkāuztaisa ar jquery" />
</form>
</table>
Now, there is php, but how can i show only checked rows in while loop?
<?php
$data = array();
$work_id = array();
$work_names = $_POST['work_name'];
$amounts = $_POST['amount'];
$units = $_POST['unit'];
$units_prices = $_POST['unit_price'];
$units_total = $_POST['unit_total'];
if(isset($_POST['useit'])){
$work_id = $_POST['useit'];
}
$data = array($work_id, $work_names, $amounts, $units, $units_prices, $units_total);
echo '<pre>';
echo htmlspecialchars(print_r($data, true));
echo '</pre>';
?>
There are different possibilities how to do that. One Suggestion (i limit it to the parts which are relevant)
form (note that i gave work_name the id as an index, use_it not (but it could have)
<td><input type="checkbox" name="useit[]" value="<?php echo $work->id; ?>"/></td>
<input type="hidden" name="work_name[<?php echo $work->id?>]" value="<?php echo $work->name; ?>" />
The form only submits the checked checkboxes values in an array, all other are omited. Therefore we could loop over the checked checkbox values like this
foreach($_POST['useit'] as $work_id){
$work_name = $work_names[$work_id];
//do something with the checked rows only (all others are not looped over)
}
This is only possible, due to the given id as an array key in the form work_name[<?php echo $work->id?>].
A general sidenote for better (and more secure) code: please note that your data could be modified by the user, and send back with wrong data (or worse). So please make sure to sanitize your input, or probably better in this case only submit the id in question and the new data and pickup the rest directly from your database. So you can make sure the hidden data has not been modified on the client side and send back wrong.
this is my html code:
<input name="dependencies[]"
and in php i do this:
$dependencies = $_POST['dependencies'];
and when I do this:
print_r($dependencies);
I can see the values like this:
Array ( [0] => [1] => )
My question
I want to add each value from that array to another array:
I didn't know how to do that
I tried:
foreach ($dependencies as $number){
echo $number;
}
but nothing has been printed
Update
this is the html
<input name="dependencies[]" value="<?php $question->id; ?>" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
and I can see the check boxes checked or not when I run the page
Update2
the whole code
<form action="../DB/addDependencies.php" method="post">
<input type="hidden" name="questionID" />
<table>
<tr>
<th>Porgugese Name</th>
<th>Englisn Name</th>
<th>Dependent</th>
</tr>
<?php
foreach ($questions as $question) {
?>
<tr>
<td>
<?php echo $question->ptName; ?>
</td>
<td>
<?php echo $question->enName; ?>
</td>
<td>
<input name="dependencies[]" value="<?php $question->id; ?>" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
</td>
</tr>
<?php
}
?>
</table>
<input type="submit" value="Save" name="submit" />
</form>
You need to change your HTML to this:
<input name="dependencies[<?php $question->id; ?>]" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
Then to test if a checkbox if checked you just need to do something like
function isQuestionChecked($question_id) {
return isset($_POST['dependencies']) && isset($_POST['dependencies'][$question_id]);
}
try to use
foreach ($dependencies as $key=>$val){
var_dump($key);
var_dump ($val);
}
instead of
foreach ($dependencies as $number){
echo $number;
}
;-) hope that will bring some ideas to your mind in this case
I know how to keep the checkboxes checked on form submit when there is an error, but I have a different issue now. I am using check boxes of the same name where people can click 1 or multiple. I want it to only keep the check boxes that the user checked filled if there is a problem like if they check q1 A q1B but not q1c I want only q1 A and Q1b to show checked when reloaded on an error. Right now on an error all checkboxes of the same name are checked. I have tried changing the name to q1[] but that did not work. Can you please take a look and let me know how to fix this?
Here is my code.
<tr>
<td style="width: 124px" class="style15">Tape Recorder<?php if(isset($problems['tape[]'])) {?><font color="red">*</font><?php } ?></td>
<td class="style9">
<input name="tape[]" id="tape1" type="checkbox" value="used before," <?php if(isset($_POST['tape[]'])) echo "checked"; ?>
</td>
<td class="style9">
<input name="tape[]" id="tape2" type="checkbox" value="helpful in past," <?php if(isset($_POST['tape[]'])) echo "checked"; ?> </td>
<td class="style9">
<input name="tape[]" id="tape3" type="checkbox" value="requesting from DACC" <?php if(isset($_POST['tape[]'])) echo "checked"; ?> </td>
<td class="style9">
<input name="tape[]" id="tape4" type="checkbox" value="NA" <?php if(isset($_POST['tape[]'])) echo "checked"; ?></td>
</tr>
<tr>
<td style="width: 124px">Note Taker <?php if(isset($problems['note'])) {?> <font color="red">*</font><?php } ?></td>
<td class="style9">
<input name="note" type="checkbox" value="used before," <?php if(isset($_POST['note'])) echo "checked"; ?>
</td>
<td class="style9">
<input name="note" type="checkbox" value= "been helpful in the past," <?php if(isset($_POST['note'])) echo "checked"; ?>
<td class="style9">
<input name="note" type="checkbox" value= "requesting from DACC" <?php if(isset($_POST['note'])) echo "checked"; ?>
<td class="style9">
<input name="note" type="checkbox" value="NA" <?php if(isset($_POST['note'])) echo "checked"; ?>
</tr>
A quick and dirty solution would be:
<input name="tape[]" id="tape[]" type="checkbox" value="NA" <?php if(isset($_POST['tape']) && is_array($_POST['tape']) && in_array('NA', $_POST['tape'])) echo 'checked="checked"'; ?> />
For that you need to change the 'NA' part for each different answer obviously. Though I would look at something like having a loop for repeated checkboxes or a callback function to determine whether or not to echo checked=checked.
Thank you Mr. Code
Here's my final code that I was able to figure out thanks to you
$mailBody .= "They requested additional information on ...\n\n";
$mailBody .= $moreinfo = join("\n", $_REQUEST["moreinfo"]);
$mailBody .= "\n\n";
$mailBody .= "They also had this to say...\n\n";
$mailBody .= "$comments\n\n";
//code on page
<input type="checkbox" name="moreinfo[selection1]" value="selection1" <?php if(isset($_POST['moreinfo']) && is_array($_POST['moreinfo']) && in_array('selection1', $_POST['moreinfo'])) echo 'checked="checked"'; ?> /> Selection 1<br>
<input type="checkbox" name="moreinfo[selection2]" value="selection2" <?php if(isset($_POST['moreinfo']) && is_array($_POST['moreinfo']) && in_array('selection2', $_POST['moreinfo'])) echo 'checked="checked"'; ?> /> Selection 2<br>
<input type="checkbox" name="moreinfo[selection3]" value="selection3" <?php if(isset($_POST['moreinfo']) && is_array($_POST['moreinfo']) && in_array('selection3', $_POST['moreinfo'])) echo 'checked="checked"'; ?> /> Selection 3<br>
I do not care that this looks crazy because it works.
You dont need to have the same name for checkboxes, only for radio buttons.
Radio buttons belong to a group so they have to have the same name but different values. Checkboxes however, can have different names for each checkbox! Therefore just change each checkbox to a differnent name
I am trying to create a function where users can check a message to delete. Obviously if more than one message is checked, they will all be deleted.
I have the following code
<form action="process.php" method="post">
<input type="hidden" name="deleteMessages" />
<?php
while($row=mysql_fetch_assoc($q))
{
if($row['to_viewed'] == 0)
{
?>
<tr>
<td><input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
</td>
<td><b><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['title'] ?></a></b></td>
<td><b><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['from']; ?></a></b></td>
<td><b><?php echo $row['created']; ?></b></td>
</tr>
<?php
}
else if($row['to_viewed'] == 1)
{
?>
<tr>
<td><input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
</td>
<td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['title'] ?></a></td>
<td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['from']; ?></a></td>
<td><?php echo $row['created']; ?></td>
</tr>
<?php
}
}
?>
<input type="submit" value="Delete All" />
</form>
I want to pass the checkbox through and if the value is 1, process it and delete it.
But how would I achieve this with mulitple messages no matter if there is one message or ten?
Thanks
In your form put:
<input type="checkbox" name="check_box_delete[]" value="<?php echo $row['id']; ?>" />
Then to process:
if(isset($_POST['check_box_delete']))
{
foreach($_POST['check_box_delete'] as $id)
{
// Delete $id
}
}
You can let php store mutliple values in an array by naming the input fields with an array designator suffix [].
For example, all checkboxes named checkbox[] will then be stored in $_POST['checkbox'][].
Note that this may not be applicable to checkboxes as their $_POST value only exists if they were checked.
<input type="checkbox"
name="mid_to_delete[]"
value="some message id"
id="mid_to_delete_some_message_id">
<label for="mid_to_delete_some_message_id">Delete "Some subject"</label>
and then
<?php
foreach ($_POST['mid_to_delete'] as $mid) {
delete_some_message_id($mid);
}
?>
Since only successful controls are submitted, and unchecked checkboxes are not successful, all the values you get will be ones that have been selected for deletion. (Obviously you still need to perform auth/authz to make sure that the messages being deleted are ones the user is allowed to delete)
i tried to develop a system that allowing users to answer a quiz that will be generated randomly from a database. the problems now is to mark the quiz. how can i post value of radio button because it has different name.
this is some of my code.
thanks.
$selq="SELECT * FROM question WHERE q_subject_kod = '$subkod' AND q_chapter= '$chap' order by RAND() limit $bil ";
$result=mysql_query($selq);?>
<form name="quiz" action="mark.php?bil=<?php echo $bil?>&chap=<?php echo $chap?>&action=1&subject=<?php echo $subkod?>" method="post">
<?php
##Cycle through randomly selected questions
if (mysql_num_rows($result)>=0) {
$qi = 1;
$e=0?>
<table name="q" border="1" cellpadding="2" width="100%">
<tr><td></td><td></td></tr>
<?php
while ($qry = mysql_fetch_array($result)) {
$idsol=$qry['id'];
$sol=$qry['q_quest'];
$jwpn=$qry['q_ranws'];?>
<tr><td><?php echo $qi?></td> <td><?php echo $sol?></td></tr>
<tr><td></td><td>
A<input type="radio" name="ans.<? echo $e?>" id="<? echo $idsol?>"value="<?php echo $qry['q_anws'];?>" /><?php echo $qry['q_anws'];?><br />
B <input type="radio" name="ans.<? echo $e?>" id="<? echo $idsol?>" value="<?php echo $qry['q_anws1'];?>" /><?php echo $qry['q_anws1'];?><br />
C <input type="radio" name="ans.<? echo $e?>" id="<? echo $idsol?>" value="<?php echo $qry['q_anws2'];?>" /><?php echo $qry['q_anws2'];?><br />
D <input type="radio" name="ans.<? echo $e?>" id="<? echo $idsol?>" value="<?php echo $qry['q_anws3'];?>" /><?php echo $qry['q_anws3'];?><br />
<input type="text" name="val.<? echo $idsol?>" value="<? echo $idsol?>">
</td>
</tr>
<?php
$qi++;
$e++;}}}?>>
Just use the Question, all Radio Buttons need the same name anyhow, and you don't nesecarly need an ID. So that would work just fine:
A<input type="radio" name="ans_<? echo $idsol?>" value="<?php echo $qry['q_anws'];?>" /><?php echo $qry['q_anws'];?><br />
B <input type="radio" name="ans_<? echo $idsol?>" value="<?php echo $qry['q_anws1'];?>" /><?php echo $qry['q_anws1'];?><br />
C <input type="radio" name="ans_<? echo $idsol ?>" value="<?php echo $qry['q_anws2'];?>" /><?php echo $qry['q_anws2'];?><br />
D <input type="radio" name="ans_ <? echo $idsol?>" value="<?php echo $qry['q_anws3'];?>" /><?php echo $qry['q_anws3'];?><br />
But just for the Matter of principle, try to store the Answers in a different Table then the Questions, this Way every Answer can have his own "id" and you are not limited to 4.