## 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"; ?> />
Related
I am making a book management system.I have a return button ,with that button user can return books.
Here is my release-books.php:
<?php
$sql_select = "SELECT * FROM carti ORDER BY titlu";
$rezultat = mysqli_query($conn, $sql_select);
if (mysqli_num_rows($rezultat) > 0) {
while ($row = mysqli_fetch_assoc($rezultat)) {
$disabled = $row['stoc'] > 0 ? "" : "disabled"; ?>
<tr><td><input type="submit" name="id" value="<?php echo $row['idCarte']; ?>" <?php echo $disabled; ?> formaction="imprumutare.php"></input></td>
<td><input type="submit" name="returnare" value="<?php echo $row['idCarte']; ?>" formaction="returnare_carte.php"></input>
</td>
<td><input type="text" name="nume" value="<?php echo $row['titlu']; ?>" ></input></td>
<td><input type="text" name="" value="<?php echo $row['autor']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['editura']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['categorie']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['data_adaugarii']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['stoc']; ?>"></input></td>
</tr>
<?php
}
}
?>
And here is my borrowing.php
include('conexiune.php');
//sfarsit if
//Imprumutare
if (isset($_POST['id'])) {
$identificator = $_POST['id'];
$carte_nume = $_POST['nume'];
$current_date = date('y:m:d');
$current_date_plus_14days = date('y:m:d', strtotime("+14 days"));
$nume_carte = $_POST['nume'];
$insert_in_imprumuturi = "INSERT INTO imprumuturi(idc,nume_carte,data,termen_returnare,carti_imprumutate) VALUES('$identificator','$carte_nume','$current_date','$current_date_plus_14days','1')";
mysqli_query($conn, $insert_in_imprumuturi) or die(mysqli_error($conn));
$sql_rezervare = "UPDATE carti SET stoc=stoc-1 WHERE iDCarte='$identificator' ";
if (mysqli_query($conn, $sql_rezervare)) {
header('Refresh:0,url=emitere_carti.php');
} else {
die(mysqli_error($conn));
}
}
But I do not know how to disable the return button when the returned books volume is equal or greater that the originial volume
Can somone help me?
Check If your books volume is equal or greater than original volume, if it is. Use PHP echo to disable the Return Button.
<button type="submit" name="return"
<?php
if($returned_books_volume >= $original_books_volume){
echo 'disabled';
}
?>
>Return</button>
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";
}
?>
Hey guys i have created a register form with an image upload too but when i try to update this form i try to get the id but the isset of my image is not working so it just wont run my update query do check it out
this is the updation form where all the values will be displayed for edit now can i run the update function in the isset condition of my submit button and then update the data
<title>Register Update</title>
<?php
//error_reporting(0);
$id=$_GET['id'];
function __autoload($classname)
{
include "$classname.php";
}
$obj = new connect();
$st=$obj->con();
if (isset($_POST['sub']))
{
$upd= new update();
$upd->updatedata($_POST);
}
$qry = "select * from register ";
$run = mysqli_query($st,$qry);
$row = mysqli_fetch_assoc($run);
{
$g = $row['gen'];
$l = $row['lang'];
}
$query=mysqli_query($st,"select * from register where id='$id'");
//echo "<ul>";
while($query2=mysqli_fetch_assoc($query))
{
//print_r($query2);
echo "<form method='POST' action='RegisterRetrieve.php'>";
echo "<table>";
?>
<p><input type="hidden" name="sid" value="<?php echo $query2['id']; ?>"></p>
<tr>
<td>
First Name:
</td>
<td><input type="text" name="uname" value="<?php echo $query2['uname']; ?>"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="pwd" value="<?php echo $query2['pwd']; ?>"></td>
</tr>
<tr>
<td>Email Id:</td>
<td><input type="text" name="emailid" value="<?php echo $query2['emailid']; ?>"
</td>
</tr>
<tr>
<td>Radio Button: Are you male or female?</td>
<?php
if ($g == "male"){
echo "<td><input type='radio' name='gen' value='Male' id='gen' checked> Male <input type='radio' name='gen' value='Female' id='gen'> Female </td>";
}
else
{
echo "<td><input type='radio' name='gen' value='Male' id='gen'> Male <input type='radio' name='gen' value='Female' id='gen' checked> Female </td>";
}
?>
</tr>
<tr>
<td>Check Box: Check the languages you know?</td>
<td><?php
$lang=explode(',',$l);
//print_r($lang);
if(in_array('Cricket', $lang))
echo '<input type="checkbox" name="lang[0]" value="Cricket" checked>Cricket';
else
echo '<input type="checkbox" name="lang[0]" value="Cricket">Cricket';
if(in_array('Basketball', $lang))
echo '<input type="checkbox" name="lang[1]" value="Basketball" checked>Basketball';
else
echo '<input type="checkbox" name="lang[1]" value="Basketball">Basketball';
if(in_array('Hockey', $lang))
echo '<input type="checkbox" name="lang[2]" value="Hockey" checked>Hockey';
else
echo '<input type="checkbox" name="lang[2]" value="Hockey">Hockey'."<br>";
?>
</td>
</tr>
<tr>
<td>Mobile No:</td>
<td><input type="text" name="mobile" value="<?php echo $query2['mobile']; ?>"
</td>
</tr>
<tr>
<td>10th Marks:</td>
<td><input type="text" name="marks_10" value="<?php echo $query2['10marks'];?>"
</td>
</tr>
<tr>
<td>
12th Marks:</td>
<td><input type="text" name="marks_12" value="<?php echo $query2['12marks'];?>"</td>
</tr>
<tr>
<td>
Browse Image:</td>
<td><input type="file" name="file1"></td>
<td><img src='img/<?php echo $query2['name'];?>' width='150px' height='150px'></td>
</tr>
<tr>
<td>
<select name="priority">
<option value="admin">
admin
</option>
<option value="<?php echo $query2['priority']; ?>"><?php echo $query2['priority']; ?>
</option>
<option value="superadmin">
superadmin
</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="submit" name="sub"><br>
</td>
</tr>
<?php
echo "<table>";
echo "</form>";
}
//echo "</ul>";
?>
now my update query which i m using but when i try to isset my image it just wont go in that condition
<?php
class update extends connect
{
function updatedata($rel)
{
$obj= new connect();
$obj->con();
extract($_POST);
$id=$_GET['id'];
$line = implode("," ,$lang);
print_r($_POST);
if(isset($_FILES["file1"]))
{
extract($_POST);
echo "hello";
$name = $_FILES['file1']['name'];
$type = $_FILES['file1']['type'];
$size = $_FILES['file1']['size'];
$tmp_name = $_FILES['file1']['tmp_name'];
$loc = 'img/';
$ext = substr($name,strpos($name,'.')+1);
if($_FILES['file1']['size']>= '10000' || $_FILES['file1']['size']<="23000000")
{
//echo $size;
}
else{
// echo "size is not supported";
}
$val = $_FILES['file1']['size'];
if($ext == 'jpg' || $ext == 'png')
{
//echo $lang;
//print_r($_POST);
//exit;
$val =("update register set uname='$uname',pwd='$pwd',emailid='$emailid',gen='$gen',lang='$line',mobile='$mobile',10marks='$marks_10',12marks='$marks_12' file1='$name' where id=$sid");
//print_r($qry);
$res=mysqli_query($this->con(),$val);
//print_r($run);
if($res)
{
move_uploaded_file($tmp_name,$loc.$name);
//echo "data saved";
//echo "Data inserted";
}
else
{
//echo "Data Not Inserted";
}
}
}
}
//print_r($val);
// return $res;
}
?>
Your html is broken:
</tr>
<?php
echo "<table>"; <--shouldn't this be </table>?
echo "</form>";
}
I have a quiz which shows 1 question per page. If the user clicks next question without selecting a multiple choice answer, I'm trying to get validation to appear so that they can't advance unless they select an answer. When the user currently presses next question the error: 'Notice: Undefined index: answer' appears
any help?
quiz.php:
if(isset($_POST['checkQuiz'])) {
$a=$_POST['a'];
$quiz_id=$_SESSION['quiz_id'];
$index=$_SESSION['index'];
$resultQuery = mysqli_query($con,"SELECT `correctValue` FROM quiz_questions WHERE quiz_id = '$quiz_id' LIMIT 1 OFFSET $index");
$cor=0;
$incorrect=0;
while ($correct = mysqli_fetch_array($resultQuery)){
if ($_POST['answer'] == $correct[0]) {
$_SESSION['rightAnswers']+=1;
}
if ($_POST['answer'] != $correct[0]) {
$_SESSION['wrongAnswers']+=1;
}
}
}
<form method="post" action="" class="form complete">
<table>
<td>
<td width = "50" id="question"><?php echo $result['question'] . "<br>"; ?></td>
</td>
<tr height = "10"></tr>
<td id= "number" width = "20" class="number"><?php echo $questionNumber ?>)</td>
<td id = "possible_answers" height = "100"width = "700">
<input type="radio" name="answer" onClick="changeColour('a')" value="<?php echo $result['answerA'] ?>"> <?php echo $result['answerA']; ?> <br>
<input type="radio" name="answer" onClick="changeColour('b')" value="<?php echo $result['answerB'] ?>"> <?php echo $result['answerB']; ?> <br>
<input type="radio" name="answer" onClick="changeColour('c')" value="<?php echo $result['answerC'] ?>"> <?php echo $result['answerC']; ?> <br>
<input type="radio" name="answer" onClick="changeColour('d')" value="<?php echo $result['answerD'] ?>"> <?php echo $result['answerD']; ?> <br><br>
</table>
<?php
$_SESSION['questionNumber']=$questionNumber;
}
$a=$a+1;
?>
<input type="submit" name="exitQuiz" value="Exit Quiz" id="button1">
<?php
if ($questionNumber<$_SESSION['numberOfQuestions']) {
?>
<input type="submit" name="checkQuiz" value="Next Question" id="button1">
<input type="hidden" value="<?php echo $a ?>" name="a">
<?php
}
?>
<?php
if ($questionNumber==$_SESSION['numberOfQuestions']) {
?>
<input type="submit" name="checkResult" value="Quiz Result" id="button1">
<input type="hidden" value="<?php echo $a ?>" name="a">
<?php
} ?>
You need to check that $_POST['answer'] is set, like this:
while ($correct = mysqli_fetch_array($resultQuery)){
if (!isset($_POST['answer']) || $_POST['answer'] != $correct[0]) {
$_SESSION['wrongAnswers']+=1;
} elseif ($_POST['answer'] == $correct[0]) {
$_SESSION['rightAnswers']+=1;
}
}
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
));
?>