I am trying to make a very basic quiz in PHP and mySQL.
Essentially, I have a user "Post quiz" page that will allow the user to post 3 questions with 3 options and one solution. If the user presses a radio button that has the value equal to the answer of the question (which was made in the "post quiz" page) then it will throw back text saying they got it right, other wise it will say it is wrong.
My Problem:
I have gotten everything to work bar one thing: When I click one of the radio buttons in the form, they all seem to post and throw back the same condition.
Maybe my code is just fundamentally wrong and I don't see it, but I have been at this for a week, can someone find the error for me?
Quiz Page:
Code saying what each button is equal to,
$_POST['q2o1'] = $tresult['q2_opt1'];
$_POST['q2o2'] = $tresult['q2_opt2'];
$_POST['q2o3'] = $tresult['q2_opt3'];
$q2answer = $tresult['q2answer'];
$question2 = $tresult['question2'];
$_POST['q3o1'] = $tresult['q3_opt1'];
$_POST['q3o2'] = $tresult['q3_opt2'];
$_POST['q3o3'] = $tresult['q3_opt3'];
$q1answer = $tresult['q1answer'];
$question1 = $tresult['question1'];
$_POST['q1o1'] = $tresult['q1_opt1'];
$_POST['q1o2'] = $tresult['q1_opt2'];
$_POST['q1o3'] = $tresult['q1_opt3'];
$q3answer = $tresult['q3answer'];
$question3 = $tresult['question3'];
$q1_opt1 = $_POST['q1o1'];
$q1_opt2 = $_POST['q1o2'];
$q1_opt3 = $_POST['q1o3'];
$q2_opt1 = $_POST['q2o1'];
$q2_opt2 = $_POST['q2o2'];
$q2_opt3 = $_POST['q2o3'];
$q3_opt1 = $_POST['q3o1'];
$q3_opt2 = $_POST['q3o2'];
$q3_opt3 = $_POST['q3o3'];
HTML code for the radio buttons and question layout:
<?php echo '<form action="quiz.php?id='.$id.'" method ="post">';?>
<h3 id="question2"><u>Question 1: </u></h3><p> <i><?php echo $question1;?></i></p>
<br>
<p>A: <?php echo $q1_opt1;?></p>
<p>B: <?php echo $q1_opt2;?></p>
<p>C: <?php echo $q1_opt3;?></p>
<br>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="radio" name="q1o1" value =<?php echo "".$q1o1."";?> id="option1" autocomplete="off"> A
</label>
<label class="btn btn-primary">
<input type="radio" name="q1o2" value =<?php echo "".$q1o2."";?>id="option2" autocomplete="off"> B
</label>
<label class="btn btn-primary">
<input type="radio" name="q1o3" value =<?php echo "".$q1o3."";?> id="option3" autocomplete="off"> C
</label>
</div>
<button type="submit" name ="submitquiz" class="btn btn-success">Submit!
<span class="glyphicon glyphicon-ok"></span></button>
PHP saying if the POST is submitted for each button:
<?php
if(isset($_POST['submitquiz'])){
if(isset($_POST['q1o1']) && $_POST['q1o1'] == $q1answer ){
$c1= '<p><font color="#62FD01">That is the correct answer!</font></p>';
} else{
$w1= '<p> <font color="red">That is the wrong answer!</font></p>';
}if(isset($_POST['q1o2']) && $_POST['q1o2'] == $q1answer ){
$c1= '<p><font color="#62FD01">That is the correct answer!</font></p>';
} else{
$w1= '<p> <font color="red">That is the wrong answer!</font></p>';
}
if(isset($_POST['q1o3']) && $_POST['q1o3'] == $q1answer ){
$c1= '<p><font color="#62FD01">That is the correct answer!</font></p>';
} else{
$w1= '<p> <font color="red">That is the wrong answer!</font></p>';
}
}
else {
echo '<p><font color="orange">You did not answer this question</font></p>';
}
?>
<?php
if(isset($w1)){
echo $w1;
}else{
echo $c1;
}
?>
Related
I have a form which can submited.and have list,that listed all submited form details.
I tried it in different ways.I want to fill the form with the corresponding details when I clicked the edit button.
Here is my php file
<div class="row">
<div class="col-sm-9">
<b>Leader Name : </b><?php echo($row["lead_name"]); ?><br>
<b>Phone Number : </b><?php echo($row["phone_number"]); ?><br>
<b>Email : </b><?php echo($row["email"]); ?><br>
<b>Part Created Time : </b><?php echo($row["create_date_and_time"]); ?>
<br>
</div>
<div class="col-sm-3 ">
<form role="form" action='index.php' method='POST'>
<input type='hidden' name='party_id' value='<?php echo($row["party_id"]); ?> '>
<input type="submit" class="btn btn-sm btn-success btn-block" id="edit" name="edit" value="Edit" style="font-size: 12px; padding: 3px;">
<input type="submit" class="btn btn-sm btn-danger btn-block" id="delete" name="delete" value="Delete" style="font-size: 12px; padding: 3px;">
</form>
<?php
if (isset($_POST['delete'])) {
print("<script> alert('delete'); </script>");
$party_id = isset($_POST['party_id']) ? $_POST['party_id'] : "";
$queryDelete = "DELETE FROM party_details WHERE party_id='$party_id'";
if ($conn->query($queryDelete)) {
$_SESSION['party'] = "";
$_SESSION['trips'] = [];
print("<script>
alert('Party removed');
window.location.href='../tripCreate';
</script>");
} else {
print("<script>alert('Error when remove ! ');</script>");
}
$_POST = array();
}
if (isset($_POST['edit'])) {
$party_id1 = isset($_POST['party_id']) ? $_POST['party_id'] : "";
$query1 = "SELECT * FROM party_details WHERE party_id='$party_id1'";
$result1 = $conn->query($query1);
$row1 = $result1->fetch_assoc();
}
?>
</div>
first of all, you should specify not only result you want to achieve, but also what kind of problem you are facing.
is it php error, or information not being displayed in resulted page?
one thing i spotted is that you got $row1 = $result1->fetch_assoc(); but in form you echo $row[] (instead of $row1[]), which i dont see being created anywhere.
also, did you try var_dump($row) in php and check its content (or $row1...)?
Actually i want to develop a application from where user can set attendance for student. so the html form for attendance will come from my db query. and it's coming too but the prob is that how can i insert that form information to my db . actually i searched lot but i didn't get any result for this as perfect as i want i mean please can anyone help me . thanks in advance
<form action="attendance.php" method="post">
<?php include '../database-config.php';
foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row){
echo "<div>
<label>".htmlentities($row['student_id'])."</label>
<input type='radio' name='atten".htmlentities($row['student_id'])."' checked='checked'>Present
<input type='radio' name='atten".htmlentities($row['student_id'])."'>Absent
</div></br>";
}
?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
<form action="attendance.php" method="post">
<?php include '../database-config.php';
$result = mysql_query("SELECT * FROM student WHERE active_class='VII'");
foreach($result as $row)
{
?>
<div>
<label><?php echo $row['student_id']?></label>
<input type="radio" name="attend" value="present" checked>Present
<input type="radio" name="attend" value="absent">Absent
</div>
</br>
<?php
}
?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
so in php you can get value like this
<?php
$attend = $_POST['attend'];
echo $attend;
?>
So in $attend it contain value(value="present") of radio button.
it may be present or either absent
damn getting tired xD
this should work though but you have to add the column attendency to the database table by yourself cheers
<form action="" method="post">
<?php
include '../database-config.php';
if(isset($_POST['attendency']) && isset($_POST['id']))
{
$id_to_update = $_POST['id'];
$status = $_POST['attendency'];
$ar = array('p','a');
$attend = !empty($status) && in_array($status,$ar) ? $status : 'p';
//you have to create a column named attendency for this to work
$sql = "INSERT INTO student(attendency) VALUES ('$attend ') WHERE user_id = '$id_to_update '";
$dbh->query($sql);
}
foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row)
{
if($row['attendency'] == 'p')
{
$p = 'checked="checked"';
$a = '';
} else {
$a = 'checked="checked"'
$p = '';
} ?>
<div>
<input type="hidden" name="id" value="<?=$row['student_id']?>">
<label><?=$row['student_id']?></label>
<input type='radio' name='attendency' <?=$p?>>Present
<input type='radio' name='attendency' <?=$a?>>Absent
</div></br>
<?php } ?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
hello everyone sorry but am just beginner in php , mysql ... i was developing question and answer website which i have page for all Question (Q.php) and page for displaying a specific question (QR.php) and get information according to data sent from Q.php via url ($_GET['start'] i also have page to confirm that the answer is already submitted .... but i got error when entering the id from get method and the message from post method ... any answer will be appreciated
Q.php
<?php
include("pagination.php");
if(isset($res))
{
while($result = mysql_fetch_assoc($res))
{
echo '<div class="shop-item">' ;
echo ' <div class="price">' ;
echo $result['Inquirer'] ;
echo ' </div>' ;
echo ' <div class="price">' ;
echo $result['question'] ;
echo ' </div>' ;
echo ' <div class="actions"> ';
echo '<input type="button" class="btn btn-large " value="More Info" onclick="window.location=\'QR.php?start=' . urlencode($result['id']) . ' \';" />';
echo '</div> ';
echo ' </div> ';
}
}
?>
QR.php
<form action="QRR.php" method="POST">
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>
<div align="Right">
<textarea class="form-control" rows="4" name ="answer" id="Test">
</textarea>
<br>
<div class="actions">
<?php echo '<input type="button" class="btn btn-large " value="Post Answer" onclick="window.location=\'QRR.php?start=' . urlencode($_GET['start']) . ' \';" />'; ?>
</div>
</div>
</div>
</form>
QRR.php
<?php
// variables
$answer=$_REQUEST['answer'];
require ("coonection.php");
$FURL = $_REQUEST['hi'];
//query
$query = "INSERT INTO `answers`(`answer_id`, `question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('','$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data)
{
echo "Your Questions Has Been Successfully Added ";
}
?>
if i removed passing hi from QR to QRR answer stored //$answer
if i removed storing answer from the text area the id from url stroed //$FURL
This isnt the most beautiful code, as i just copied yours and made a few modifications.. but this form will submit back to the same page, and the php will run and insert ONLY if the form is submitted.
if(isset($_POST['answer'])) says "if the variable _POST answer is set, run the php code and insert.. if it is not set, do nothing.
You will notice the form action is left blank, you can set it to the page name yo are on.. as the form will send the variables to the same page. This is a good way of doing it because if there are errors, you can prepopulate the input or textareas with the code they just typed in.
<?php
// variables
if(isset($_POST['answer'])){
$answer=$_POST['answer'];
require ("coonection.php");
$FURL = $_POST['hi']; // there is no input name 'hi' set in your form. so this code will fail due to that.
//query
$query = "INSERT INTO `answers`(`question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data){
echo "Your Questions Has Been Successfully Added ";
}
}
?>
`
you will see i removed your answer_id. if you use this code, make sure that field is set to primary auto increment in your database.
<form action="" method="POST">
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>
<div align="Right">
<textarea class="form-control" rows="4" name="answer" id="Test"></textarea>
<br>
<div class="actions">
<button type="submit" class="btn btn-large " value="Post Answer">Post Answer</button>
</div>
</div>
</div>
</form>
NOTE: both of these snippets will go in the same page.
I have built a website where the user plays a simple guess the animal game with the system. The database that the script connects to is a one table binary tree, with each row having a node ID. I need to print the URL on screen that contains the current stage of the game so that if it were copied and pasted it would take another user to the same point. How do I do this?
$query = "SELECT `message`, `parentID`,`answerYesID`, `answerNoID`, `nodeID` FROM `creature`";
$where = "";
if(isset($_POST['answer'])){
$_SESSION['node'] = $_POST['answer'];}
elseif (isset($_SESSION['node'])){ $where = "WHERE `nodeID` = '{$_SESSION['node']}'";}
else { $where = "WHERE `parentID` IS NULL";}
?>
<?php
if (isset($_POST['reset'])){
$where = "WHERE `parentID` IS NULL";
$_SESSION['node'] = 1;
echo $_SESSION['node'];
}
if (isset($_POST['submit']) && (isset ($_POST['answer']))){
$where = "WHERE `nodeID` = '{$_POST['answer']}'";
}elseif (isset($_POST['submit']) && (!isset ($_POST['answer']))){
$where = "WHERE `nodeID` = '{$_SESSION['node']}'";
}
echo'<div class="form">';
$result = mysqli_query($dbconn, $query.$where);
$row = mysqli_fetch_assoc($result);
echo "<p class = \"answer\">";
echo$row['message'];
echo "</p>";
?>
<form action="assignment.php" method="POST">
<input type="radio" name="answer" value="<?php echo $row['answerYesID'];?>">Yes
<input type="radio" name="answer" value="<?php echo $row['answerNoID'];?>">No
<input type="submit" name="submit" value="submit" class = "submit">
<input type='submit' name='reset' value='reset' class = "reset">
click here...
</form>
</div>
<div class = "speech">
<span class= "welcome" ><h1>Welcome!</h1></span>
<p>My name is Barry and I'm the game keeper.<br>
This is the Creatures Expert Game. Answer <br>
the first question to begin. Let's see if I can <br>guess what creature you are thinking of!<br>
<br> Hit the reset button to restart at anytime.</p>
<a><img src="speech.png"></a>
</div>
<div class ="sparky">
<a><img src="sparkydog.png"></a>
</div>
<div class ="sign">
<a><img src="sign.png"></a>
</div>
</body>
</html>
the page is viewable at s573022.neongrit.net/assignment
Since you are using SESSION, instead of href="assignment.php" you can echo:
click here...
But the problem with your existing code is that you are setting $_SESSION['node'] only if you detect a $_POST['answer'] which is impossible if you do not submit a form.
Therefore, you need to first check if you have the nodeId set in the url like so at the very beginning of the file:
if (isset($_GET['nodeId']) && $_GET['nodeId'] != '') {
$_SESSION['node'] = $_GET['nodeId'];
}
else { //do whatever you do now to get the answer }
I wanna update my product when there's user login. Here's my code in edit.php
<?php
$id= (int)$_GET['id'];
$query = "SELECT * FROM game WHERE gameId=".$id."";
$rs = mysql_query($query);
while($data = mysql_fetch_array($rs))
{
?>
<form action="doUpdate.php" method="post">
<?php echo "<image src=\"images/".$id.".png\" alt=\"gameImage\" </image>"?>
<div class="cleaner"></div>
<div class="myLabel">Name</div><div>: <input type="text" value="<?php echo $data['gameName'];?>" name="gameName"/></div>
<div class="myLabel">Developer</div><div>: <input type="text" value="<?php echo $data['gameDeveloper'];?>" name="gameDeveloper"/></div>
<div class="myLabel">Price</div><div>: <input type="text" value="<?php echo $data['gamePrice'];?>" name="gamePrice"/></div>
<br/>
<div id="txtError" style="color:#D70005">
<?php
if(isset($err))
{
if($err==1) echo"All Fields must be filled";
else if($err==2) echo"Price must be numeric";
else if($err==3) echo"Price must be between 1-10";
}
?>
</div>
<input type="submit" value="Submit"/>
<input type="button" value="Cancel"/></span>
<?php
}
?>
</form>
This is my code in doUpdate.php
<?php
$nama = $_POST['gameName'];
$dev = $_POST['gameDeveloper'];
$harga =$_POST['gamePrice'];
$id= (int)$_REQUEST['id'];
if($nama == "" || $dev == "" || $harga == "" )
{
header("location:edit.php?err=1");
}
else if(!is_numeric($harga))
{
header("location:edit.php?err=2");
}
else if($harga < 1 || $harga >10)
{
header("location:edit.php?err=3");
}
else
{
$query = "UPDATE game SET gameName='".$nama."', gameDeveloper='".$dev."', gamePrice=".$harga." where gameId=".$id."";
mysql_query($query);
header("location:product.php");
}
?>
Why I can't change name, developer, or price even I already give the action in form? And why if I delete the name, developer, and price to know wether the validation works or not, it said that Undefined index in edit.php $id= (int)$_GET['id']; ?
You are trying to get $_REQUEST['id'] in doUpdate.php but there is no such field in the form.
You have to add it as a hidden field.
Also you have to format your strings.
Every string you're gonna put into query you have to escape special characters in.