How to disable individual checkbox? - php

I want to display array values followed by a checkbox inside a html table using a for loop :
<?php
for ($j = 0; $j < $data['user']['droit']['cnt']; $j++)
{
?>
<table>
<tr>
<td><?php echo $data['user']['droit'][$j]['menu_titre']; ?></td>
<?php
$checked = "";
if (is_array($data['user_droit']) && count($data['user_droit']) > 0 && in_array($data['user']['droit'][$j]['menu_code'], $data['user_droit']))
$checked = " checked ";
?>
<td><input type="checkbox" name="<?php echo $data['user']['droit'][$j]['menu_code']; ?>"
value="<?php echo $data['user']['droit'][$j]['menu_code']; ?>" <?php echo $checked ?> />
</td>
</tr>
</table>
<?php
}
?>
I don't know how to make the individual checkbox to be disabled if it is checked from the loop. How to achieve that ?

or set $checked = " checked disabled ";
<?php
for ($j = 0; $j < $data['user']['droit']['cnt']; $j++)
{
?>
<table>
<tr>
<td><?php echo $data['user']['droit'][$j]['menu_titre']; ?></td>
<?php
$checked = "";
if (is_array($data['user_droit']) && count($data['user_droit']) > 0 && in_array($data['user']['droit'][$j]['menu_code'], $data['user_droit']))
{
$checked = " checked disabled ";
}
?>
<td><input type="checkbox" name="<?php echo $data['user']['droit'][$j]['menu_code']; ?>"
value="<?php echo $data['user']['droit'][$j]['menu_code']; ?>" <?php echo $checked ?> />
</td>
</tr>
</table>
<?php
}
?>

Related

multiple checkbox update with row

i have this form
how do i update this record by row with checkbox ? the checkbox hold a value i sent from previous form .
this would be the database
is this right??
while ($row = mysqli_fetch_array($result)){
.
.
.
.
<td><center>
<input type="hidden" name="matric[]" value="<?php echo $matric?>"/><p>
<input class="w3-check" type="checkbox" value='$hour' name="unattend<?php echo $matric?>[]">
<label> Unattend</label></p> </center></td>
<td><center>
<input type="hidden" name="matric[]" value="<?php echo $matric?>"/><p>
<input class="w3-check" type="checkbox" value='$hour' name="mc<?php echo $matric?>[]">
<label> I have MC </label></p> </center></td> }
how would the update mysql be ?
This is the form
<?php
$i = 1;
while ($row = mysqli_fetch_array($result)) {
$id=$row['matric_number'];
?>
<form method="POST" action="attend.php">
<tr>
<td><center><?php echo $i; ?></center></td>
<td><center><?php echo $row['student_name']; ?></center></td>
<td><center><?php echo $row['matric_number']; ?></center></td>
<td><center>
<p>
<input class="w3-check" name="unattend[]" type="checkbox" id="unattend[]" value="<?php echo $id; ?>">
<label> Unattend</label></p> </center></td>
<td><center>
<p>
<input class="w3-check" name="mc[]" type="checkbox" id="mc[]" value="<?php echo $id; ?>">
<label> I have MC </label></p> </center></td>
<td><center>
<?php echo "<a href=student.php?id1=".$row['matric_number'].">"."Details"; ?></center></td>
</tr>
<?php
$i++;
}
mysqli_close ($link);
?>
</table>
The php to update the data by row is
<?php
if (isset($_POST['unattend'])) {
$hour=$_POST['hour'];
$id=$_POST['unattend'];
$N = count($id);
for($i=0; $i < $N; $i++)
{
$select = mysqli_query($link,"SELECT * FROM pbu WHERE matric_number='$id[$i]'")or die(mysql_error());
while ($row = mysqli_fetch_array($select)) {
$value=$row['unattend'];
$total=$value+$hour;
$result = mysqli_query($link,"UPDATE pbu SET unattend='$total' WHERE matric_number='$id[$i]'")or die(mysql_error());
echo "unattend is updated for matric number ".$id[$i]."<br>";
}
}
}
if (isset($_POST['mc'])){
$id1=$_POST['mc'];
$N = count($id1);
for($i=0; $i < $N; $i++)
{
$select = mysqli_query($link,"SELECT * FROM pbu WHERE matric_number='$id[$i]'")or die(mysql_error());
while ($row = mysqli_fetch_array($select)) {
$values=$row['mc'];
$totals=$values+$hour;
$results = mysqli_query($link,"UPDATE pbu SET mc='$totals' WHERE matric_number='$id1[$i]'")or die(mysql_error());
echo "mc is updated for ".$id1[$i]."<br>";
}
}
}
else{
echo "nothing inserted";
}
?>

PHP MySQL Insert survey answers into table

If i have this HTML and PHP Code:
<?php
if(isset($_POST["submit"]))
{
$num = $_POST['number'];
for ($i = 0; $i < $num; $i++)
{
if($i==0)
{
echo '0';
$answer1 = $_POST["answer1"];
$answer2 = $_POST["answer2"];
$answer3 = $_POST["answer3"];
$answer4 = $_POST["answer4"];
$answer5 = $_POST["answer5"];
$answer6 = $_POST["answer6"];
$answer7 = $_POST["answer7"];
$answer8 = $_POST["answer8"];
}
else
{
echo 'no 0';
$answer1 = $_POST["answer1$i"];
$answer2 = $_POST["answer2$i"];
$answer3 = $_POST["answer3$i"];
$answer4 = $_POST["answer4$i"];
$answer5 = $_POST["answer5$i"];
$answer6 = $_POST["answer6$i"];
$answer7 = $_POST["answer7$i"];
$answer8 = $_POST["answer8$i"];
}
//then insert the line items into the lineitems table with the bill sequence from the adhocbills table
$sql="INSERT into surveys_completed (survey_sequence, question_seq, answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8) values ('".$_POST["survey_sequence"]."', '".$_POST["question_seq"]."', '".$answer1."', '".$answer2."', '".$answer3."', '".$answer4."', '".$answer5."', '".$answer6."', '".$answer7."', '".$answer8."')";
$rs=mysql_query($sql,$conn) or die(mysql_error());
echo $sql.'<br><br>';
}
}
?>
<?php
/*
if(mysql_real_escape_string($_GET["string"]) == '')
{
echo 'No Survey was selected';
exit();
} */
if(mysql_real_escape_string($_GET["company"]) != '')
{
//sql for company
$sql="SELECT * from surveys where company = '".mysql_real_escape_string($_GET["company"])."' and string = '".mysql_real_escape_string($_GET["string"])."' ";
}
elseif(mysql_real_escape_string($_GET["string"]) != '')
{
//sql for string
$sql="SELECT * from surveys where string = '".mysql_real_escape_string($_GET["string"])."' ";
}
$rs=mysql_query($sql,$conn) or die(mysql_error());
$survey=mysql_fetch_array($rs);
echo '<h3>'.$survey["title"].'</h3>';
?>
<form method="post" action="/home.php?id=surveys/complete_survey">
<table width="600" border="0" cellspacing="5" cellpadding="5">
<?php
$sql2="SELECT * from surveys_questions where survey_seq = '".$survey["sequence"]."' ";
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
$counter=0;
while($survey_questions=mysql_fetch_array($rs2))
{
$counter++;
?>
<tr>
<td colspan="2"><strong><?php echo $counter; ?>. <?php echo $survey_questions["question"]; ?></strong>
COUNTER<input type="text" name="number" id="number" value="<?php echo $counter; ?>" />
SURVEY<input type="text" name="survey_sequence" id="survey_sequence" value="<?php echo $survey["sequence"]; ?>" />
QUESTION<input type="text" name="question_seq" id="question_seq" value="<?php echo $survey_questions["sequence"]; ?>" /></td>
</tr>
<tr>
<td><?php if($survey_questions["answer1"] != '') { echo '<input type="checkbox" name="answer1'.$counter.'" value="Y" /> '.$survey_questions["answer1"]; } ?></td>
<td><?php if($survey_questions["answer2"] != '') { echo '<input type="checkbox" name="answer2'.$counter.'" value="Y" /> '.$survey_questions["answer2"]; } ?></td>
</tr>
<tr>
<td><?php if($survey_questions["answer3"] != '') { echo '<input type="checkbox" name="answer3'.$counter.'" value="Y" /> '.$survey_questions["answer3"]; } ?></td>
<td><?php if($survey_questions["answer4"] != '') { echo '<input type="checkbox" name="answer4'.$counter.'" value="Y" /> '.$survey_questions["answer4"]; } ?></td>
</tr>
<tr>
<td><?php if($survey_questions["answer5"] != '') { echo '<input type="checkbox" name="answer5'.$counter.'" value="Y" /> '.$survey_questions["answer5"]; } ?></td>
<td><?php if($survey_questions["answer6"] != '') { echo '<input type="checkbox" name="answer6'.$counter.'" value="Y" /> '.$survey_questions["answer6"]; } ?></td>
</tr>
<tr>
<td><?php if($survey_questions["answer7"] != '') { echo '<input type="checkbox" name="answer7'.$counter.'" value="Y" /> '.$survey_questions["answer7"]; } ?></td>
<td><?php if($survey_questions["answer8"] != '') { echo '<input type="checkbox" name="answer8'.$counter.'" value="Y" /> '.$survey_questions["answer8"]; } ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="2"><input type="submit" name="submit" id="submit" value="Complete Survey" />
</tr>
</table>
</form>
So the code selects the survey from the survey table and then gets all the questions from survey_questions for the selected survey.
Each question has up to 8 answers (all checkboxes with value of Y)
The user goes though and checks the box for the answer(s) they choose so when the form is submitted, how can i make it insert into surveys_completed table each answer.
So for each question, it adds a row in the surveys_completed table.
The structure of the surveys_completed table is:
sequence
survey_sequence
question_seq
answer1
answer2
answer3
answer4
answer5
answer6
answer7
answer8
create a page called getval.php then put the below code and you have to make sure you
connect to the database using pdo thats when this code is gonna work for you .
H i hope it will help you good luck
<?php
$answer1=(isset($_POST['answer1']))? trim($_POST['answer1']): '';
$answer2=(isset($_POST['answer2']))? trim($_POST['answer2']): '';
$answer3=(isset($_POST['answer3']))? trim($_POST['answer3']): '';
$answer4=(isset($_POST['answer4']))? trim($_POST['answer4']): '';
$answer5=(isset($_POST['answer5']))? trim($_POST['answer5']): '';
$answer6=(isset($_POST['answer6']))? trim($_POST['answer6']): '';
$answer7=(isset($_POST['answer7']))? trim($_POST['answer7']): '';
$answer8=(isset($_POST['answer8']))? trim($_POST['answer8']): '';
$query="INSERT INTO surveys_completed(
answer1,answer2,answer3,answer4,answer5,answer5,answer6,answer7,answer8 )
VALUES(:answer1,:answer2,:answer3,:answer4,:answer5,:answer5,
:answer6,:answer7,:answer8)";
$insert = $con->prepare($query);
$insert->execute(array(
':answer1'=>$answer1,
':answer2'=>$answer2,
':answer3'=>$answer3,
':answer4'=>$answer4,
':answer5'=>$answer5,
':answer6'=>$answer6,
':answer7'=>$answer7,
':answer8'=>$answer8
));
?>

How to Keep Radio Button Value after Next/Prev page?

## Keep Radio Button Value after Next/Prev page .. ##
This is for Student Online Exam
if(!isset($_GET['start'])){
$_GET['start'] = "undefined";
}
$start=$_GET['start']; /*To take care global variable if OFF*/
if(!($start > 0)) { /*This variable is set to zero for the first page*/
$start = 0;
}
$startpos = ($start -0);
$limit = 1;
// No of records to be shown per page.
$currentpos = $startpos + $limit;
$back = $startpos - $limit;
$next = $startpos + $limit;
$countquery=" SELECT * FROM quiz ";
$countresult=mysql_query($countquery);
echo mysql_error();
$totalrecords=mysql_num_rows($countresult);
//$quiz_table = mysql_query("select * from quiz Limit 1");
?>
<form method="POST" action="" onclick="">
<table>
<?php
$quiz_table=" SELECT * FROM quiz ORDER BY qid limit $startpos, $limit ";
$result=mysql_query($quiz_table);
echo mysql_error();
while ($row = mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo "$row[qid]"; ?></td>
<td><?php echo "$row[Question]"; ?></td>
</tr>
<tr>
<td><input type="radio" name="a" id="a" value="a"/> <?php echo "$row[opt1]"; ?></td>
<td><input type="radio" name="a" id="a" value="b"/> <?php echo "$row[opt2]"; ?></td>
<td><input type="radio" name="a" id="a" value="c"/> <?php echo "$row[opt3]"; ?></td>
<td><input type="radio" name="a" id="a" value="d"/> <?php echo "$row[opt4]"; ?></td>
</tr>
<tr>
<td><!--<input type="submit" name="smt" value="check"/>-->
<?php
if($back >=0) {
print "<a href='onlineexam.php?start=$back'><font face='Verdana' size='2'><input type='button' name='prev' value='PREV'></font></a>";
}
if($currentpos < $totalrecords) {
## Keep Radio Button Value after Next/Prev page .. ##
print "<a href='onlineexam.php?start=$next'><font face='Verdana' size='2'><input type='button' name='nxt' value='NEXT'></font></a>";
}
?></td>
<td><input type="submit" name="smt" value="Finish"/></td>
</tr>
<?php
}
$quiz_table="SELECT * from quiz";
$result=mysql_query($quiz_table);
while ($row = mysql_fetch_array($result))
{
$id=$row['qid'];
$ques=$row['Question'];
$op1=$row['opt1'];
$op2=$row['opt2'];
$op3=$row['opt3'];
$op4=$row['opt4'];
$crt=$row['woptcode'];
if(isset($_POST['smt'])){
$ans= $_POST['a'];
if(isset($_POST['a'])){
if($crt == $ans ){
$mark=0;
$mark=$mark+1;
mysql_query("INSERT INTO stuans(qid,Question,opt1,opt2,opt3,opt4,woptcode,stuanswer,mark)VALUES('$id','$ques','$op1','$op2','$op3', '$op4','$crt','$ans','$mark')");
} else{
$mark=0;
mysql_query("INSERT INTO stuans(qid,Question,opt1,opt2,opt3,opt4,woptcode,stuanswer,mark)VALUES('$id','$ques','$op1','$op2','$op3', '$op4','$crt','$ans','$mark')");
}
}
}
}
..THANKS..
You can use the checked attribute.
<input type="radio" checked>
So if you had some variables (booleans), $opt1, $opt2, $opt3, $opt4, then you could do something similar to this:
<input type="radio" name="opt1" <?php if ($opt1) echo "checked"; ?> />
<input type="radio" name="opt2" <?php if ($opt2) echo "checked"; ?> />
<input type="radio" name="opt3" <?php if ($opt3) echo "checked"; ?> />
<input type="radio" name="opt4" <?php if ($opt4) echo "checked"; ?> />

On submit echo td dropdowns and ad new td

I have the following code:
<form action="" method="POST">
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if($count > 11) $count = 11;
?>
<table>
<!-- Keeps track of the current number of rows -->
<input type="hidden" name="count" value="<?php echo $count+1; ?>"/>
<?php for($i = 0; $i < $count; $i++):
// Loop through all rows gathering the data here, and then creating the fields below
$val0 = isset($_POST['field'][$i]['0']) ? $_POST['field'][$i]['0'] : '';
$val1 = isset($_POST['field'][$i]['1']) ? $_POST['field'][$i]['1'] : '';
$val2 = isset($_POST['field'][$i]['2']) ? $_POST['field'][$i]['2'] : '';
?>
<tr>
<td><input name="field[<?php echo $i; ?>][0]" value="<?php echo $val0; ?>"/></td>
<td><input name="field[<?php echo $i; ?>][1]" value="<?php echo $val1; ?>"/></td>
<td><input name="field[<?php echo $i; ?>][2]" value="<?php echo $val2; ?>"/></td>
</tr>
<?php endfor; ?>
</table>
<input type="submit" value="click me" />
How can I make the fields into a dropdowns and when you press submit echo out the dropdown as text instead as a dropdown?
First: Fill in the following:
Make this to action="mytestpage.php"
Give this a name attribue like: name="send"
Give these fields types like: type="Text"
If you want a dropdown menu use <select> in combination with <option>...
Here is a start to learn how:
http://www.echoecho.com/htmlforms11.htm
To print your result do this:
if(isset($_POST['send'])
{
print($_POST['youroptionname']);
}
I hope it helped you.

Php: on submit echo td fields and ad new td

I have the following code:
<form action="" method="POST">
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if($count > 11) $count = 11;
?>
<table>
<!-- Keeps track of the current number of rows -->
<input type="hidden" name="count" value="<?php echo $count+1; ?>"/>
<?php for($i = 0; $i < $count; $i++):
// Loop through all rows gathering the data here, and then creating the fields below
$val0 = isset($_POST['field'][$count]['0']) ? $_POST['field'][$count]['0'] : '';
$val1 = isset($_POST['field'][$count]['1']) ? $_POST['field'][$count]['1'] : '';
$val2 = isset($_POST['field'][$count]['2']) ? $_POST['field'][$count]['2'] : '';
?>
<tr>
<td><input name="field[<?php echo $count; ?>][0]" value="<?php $val0; ?>"/></td>
<td><input name="field[<?php echo $count; ?>][1]" value="<?php $val1; ?>"/></td>
<td><input name="field[<?php echo $count; ?>][2]" value="<?php $val2; ?>"/></td>
</tr>
<?php endfor; ?>
</table>
<input type="submit" value="click me" />
</form>
The problem is when I press submit it add 3 other fields but it clears the other fields. How can I keep the content from the fields but make them uneditable?
You are not echoing your values and your $count should be $i, as you'll end up with same field names
<form action="" method="POST">
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if($count > 11) $count = 11;
?>
<table>
<!-- Keeps track of the current number of rows -->
<input type="hidden" name="count" value="<?php echo $count+1; ?>"/>
<?php for($i = 0; $i < $count; $i++):
// Loop through all rows gathering the data here, and then creating the fields below
$val0 = isset($_POST['field'][$i]['0']) ? $_POST['field'][$i]['0'] : '';
$val1 = isset($_POST['field'][$i]['1']) ? $_POST['field'][$i]['1'] : '';
$val2 = isset($_POST['field'][$i]['2']) ? $_POST['field'][$i]['2'] : '';
?>
<tr>
<td><input name="field[<?php echo $i; ?>][0]" value="<?php echo $val0; ?>"/></td>
<td><input name="field[<?php echo $i; ?>][1]" value="<?php echo $val1; ?>"/></td>
<td><input name="field[<?php echo $i; ?>][2]" value="<?php echo $val2; ?>"/></td>
</tr>
<?php endfor; ?>
</table>
<input type="submit" value="click me" />
for($j=0;$j<3;$j++){
echo '<input type="hidden" name="field['.$i.'][0]" value="'.$_POST[field][$i][0].'" />';
}

Categories