i want to make a multiple choice that if we choose wrong answer, then a text 'incorrect' will be displayed in side of the answer that we choose and if we choose right answer, then a text 'correct' will be displayed in side of the answer that we choose. i using radio button. please help.
this is my index.php:
<?php
include 'koneksi.php';
session_start();
$query = mysql_query("select * from t_soal") or die (mysql_error());
//$_SESSION['soal'] = mysql_fetch_array($query);
$_SESSION['soal'] = array();
$_SESSION['no'] = 1;
$_SESSION['score'] = 0;
$_SESSION['option'] = array();
$_SESSION['jawab'] = array();
$i=0;
while($row = mysql_fetch_assoc($query)){
$_SESSION['soal'][] = $row;
$_SESSION['option'][] = array($_SESSION['soal'][$i]['a'], $_SESSION['soal'][$i]['b'], $_SESSION['soal'][$i]['c'], $_SESSION['soal'][$i]['d']);
$i++;
}
if(isset($_SESSION['soal'])){
header("location:test.php");
}
this is test.php:
<?php
session_start();
$soal = $_SESSION['soal'];
$no = $_SESSION['no'];
if(isset($_POST['next'])){
$_SESSION['jawab'][] = $_POST['option'];
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
}
}
if(isset($soal[$no-1])){
?>
<!DOCTYPE html>
<html>
<head>
<title>Latihan Soal</title>
</head>
<body>
Kembali ke soal 1
<form action="" method="POST">
<p>
<?php
echo $no.". "; $_SESSION['no']++;
echo $soal[$no-1]['soal'];
$jawaban = $_SESSION['option'][$no-1];
shuffle($jawaban);
?>
</p>
<?php
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
}
?>
<input type="submit" name="next" value="next">
</form>
</body>
</html>
<?php
}else{
header("location:result.php");
}
?>
Assuming all your script is working already, then:
In PHP:
You will need to assign a new parameter on this script:
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
}
to
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
$_SESSION['correctAnswer'] = $soal[$no-2]['kunci']; // add sthing like this
}
and then, from
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
}
to
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
echo $jawaban[$i] == $_SESSION['correctAnswer'] ? " correct" : " incorrect";
}
In JS: (much easier)
HTML files will looks like this:
<div class="correctAnswer">
<input type="radio" name="theanswer" id="choice1" />
<label for="choice1">Choice 1</label>
</div>
<div class="incorrectAnswer">
<input type="radio" name="theanswer" id="choice2" />
<label for="choice2">Choice 2</label>
</div>
and the JS will looks like this:
$(".correctAnswer").click(function() {
$(".checks").remove();
$(this).append("<span class='checks'> correct</span>");
});
$(".incorrectAnswer").click(function() {
$(".checks").remove();
$(this).append("<span class='checks'> incorrect</span>");
});
If you want a working example, please check this fiddle link here.
Hope it helps !
Related
This is my code :
NON WORKING CODE
<div class="myform">
<form action="blah.php" method="post">
<input type="hidden" value="<?php json_encode($myarr); ?>
</form>
</div>
<?php
$myarr = array("foo");
for ($i = 1; $i <= 10; $i++) {
array_push($myarr,$i);
print "<div class='mylist'>$i</div>";
}
?>
</div>
WORKING CODE
<div class="container">
<?php
$myarr = array("foo");
for ($i = 1; $i <= 10; $i++) {
array_push($myarr,$i);
print "<div class='mylist'>$i</div>";
}
?>
<div class="myform">
<form action="blah.php" method="post">
<input type="hidden" value="<?php json_encode($myarr); ?>
</form>
</div>
</div>
Now, this is more or less the situation.I cannot alter the position of the div tags as my whole website layout is based on it.How can I make sure the first one works because that ordering of the div tags keeps my web design and everything intact.
NOTE: Please don't suggest me to use jQuery or Javascript.I do not wish to use them unless ther is no other way out. :(
Thanks
With out executing of foreach you can't get that variable for encode.
<?php
$myarr = array("foo");
for ($i = 1; $i <= 10; $i++) {
array_push($myarr,$i);
}
?>
<div class="myform">
<form action="blah.php" method="post">
<input type="hidden" value="<?php echo json_encode($myarr); ?>
</form></div>
<div class="myform">
<form action="blah.php" method="post">
<input type="hidden" value="<?php echo json_encode($myarr); ?>
</form></div>
for ($i = 1; $i <= 10; $i++) {
print "<div class='mylist'>$i</div>";
}
</div>
<?php
$myarr = array("foo");
<div class="myform">
<form action="blah.php" method="post">
<input type="hidden" value="<?php json_encode($myarr); ?>
</form>
</div>
for ($i = 1; $i <= 10; $i++) {
array_push($myarr,$i);
print "<div class='mylist'>$i</div>";
}
</div>
?>
Try this maybe. Hope I understood you...
I have a loop that iterates 3 times. Inside the loop I have a HTML form that has radio buttons. I'm processing the input using PHP. When I echo the form data, its not showing the correct values. Is it a wrong way of processing the data ? Any help is appreciated.
test.php
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<form action = 'test.php' method = 'post'>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
</form>
<?php
}
?>
<input type = 'submit' value = 'Go'>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
Move your form outside of your for loop. You are currently creating three forms with one submit button (which isn't attached to any of them).
try this way
<form action = 'test.php' method = 'post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
use following code: Or you can use radiogroup array it is easier than this.
<form action = 'test.php' method = 'post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
#Cagy79 added additional submit buttons.. I have revised the code,
<form action ="" method='post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
This works. :)
You have 3 forms without a submit button (it needs to be in between the tags). You need to create one form with one submit button like this so all your data is posted to $_POST.
<form method='post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
I am creating form where the user can input a integer and get the multiplications (1-10) of that number. Here is my web form for user input:
<html>
<head>
<title>Assignment 9.1</title>
</head>
<body bgcolor="black" text="white">
<form method="post" action="table.php"
<strong>Enter No:</strong>
<input type="text" name="num" size="10">
<input type="submit" value="Get Table">
</form>
</body>
</html>
The table I have created is this:
<?php
$num = $_POST['num'];
if($num)
(
for ($i=1; $i<=10; $i++)
(
$mul = $num * $i;
echo "$num * $i = $mul<br>";
)
)
else
(
echo "Invalid Entry!";
)
?>
I am getting an error for the table. The error is for line 5 (FOR). I have no idea why I am getting this error. Can anyone Help?
Blocks of code are marked with curly brackets { ... }, you are currently using parentheses ( ... )
As far as I can tell, that's pretty much the only thing wrong with it. You might want to add a bit of validation:
$num = isset($_POST['num']) ? intval($_POST['num']) : 0;
But that'd just a touch-up. The brackets are your actual problem.
You need to use {} instead () in the for and if.
Like this:
<?php
$num = $_POST['num'];
if($num)
{
for ($i=1; $i<=10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
}
else
{
echo "Invalid Entry!";
}
?>
Hope it helps you!
You are using parentheses when you should be using braces. Try this...
<?php
$num = $_POST['num'];
if($num)
{
for ($i=1; $i<=10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
}
else
{
echo "Invalid Entry!";
}
?>
save the file as table.php and run working 100%
<html>
<head><title>Table</title></head>
<body>
<form action="" method="POST">
<center><input type="text" name="number" size="20" > </center> <br>
<center><input type="submit" name="table" value="get table"> </center>
</form>
</body>
</html>
<?php
$num=$_POST['number'];
if($num<=20)
{
for ($i=1; $i<=10; $i++)
{
$mul=$num*$i;
echo "<center>$mul </center><br>";
}
}
else
{
echo "<center>envalid entry</center>";
}
?>
<html>
<head><title>Table</title></head>
<body>
<form action="" method="POST">
<center><input type="text" name="number" size="20" > </center> <br>
<center><input type="submit" name="table" value="get table"> </center>
</form>
</body>
</html>
<?php
$num=$_POST['number'];
if($num<=20)
{
for ($i=1; $i<=10; $i++)
{
$mul=$num*$i;
echo "<center>$mul <br></center>";
}
}
else
{
echo "<center>envalid entry</center>";
}
?>
I'm doing form in php but I have some problem.
First I will have 3 different form in the same page.
What I want is only 1 form appear and then with the answer a second form will appear and so on.
The answer of the form will be display on the same page.
For now my first form work and after get the answer go to the 2nd form but I want to submit the 2nd form the problem appear.
It delete the answer of my first form and don't do anything (everything start like I am in my first form).
I try to find the problem but can't have idea about how to solve it.
Here is my code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Q1?
<input type="number" name="nbtemplate" min="1" max="30">
<input type="submit" name="submitbutton1" value="Confirm!">
</form>
<?php
if(!isset($submitbutton1)) {
if (!empty($_POST['nbtemplate']) != "") {
echo "<b>{$_POST['nbtemplate']}</b> !\n";
echo "<br />";
$Nnbtemplate = $_POST['nbtemplate'];
$result = mysql_query("UPDATE tb SET day='$Nnbtemplate'") or die(mysql_error());
?>
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method='post'>
Q2? <br>
<?php
for ($i = 1; $i <= $Nnbtemplate; $i++) { // start loop
echo "Template ";
echo $i;
?>
<input type="number" name="nbtime" min="1" max="96">
<?php
}
echo '<input type="submit" name="submitbutton2" value="Confirm!">';
echo '</form>';
if(isset($submitbutton1) && !isset($submitbutton2)) {
if (!empty($_POST['nbtime']) != "") {
echo "<b>{$_POST['nbtime']}</b> !\n";
echo "<br />";
$nbtime = $_POST['nbtime'];
for ($j = 1; $j <= $nbtime; $j++) {
echo "Time";
echo $j;
?>
Q3:
<input type="time" name="starttime"> To <input type="time" name="endtime">
<?php
}
echo '<input type="submit">';
echo '</form>';
}
}
}
}
?>
That is some gnarly code you got there, brother. This is a really simple task when handled with some javascript. Then you can have a single post to your php. I like using the jQuery framework so here's a couple links I found quickly: this one and this one
Example code in response to comment about building form elements dynamically:
<html>
<head>
<!-- load jquery library -->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<form action="toyourpage.php">
How Many?:
<input type="text" name="number" id="number">
<div id="add"></div>
</form>
<!-- javascript go -->
<script type="text/javascript">
$(document).ready(function()
{
$('input#number').keyup(function()
{
var num = $(this).val(); // get num
if(!isNaN(num)) // check if number
{
$('div#add').html(''); // empty
for(i = 1; i <= num; i++) // add
{
$('div#add').append('New Field ' + i + ': <input type="text" name="next_' + i + '" id="next' + i + '"><br>');
}
}
else
{
alert('Valid number required');
}
});
});
</script>
</body>
</html>
I did some changes on Your code, and have tested, it works.
You had any mistakes in your {} brackets and if conditions. Also as I commented I added extract($_POST).
<?php
extract ( $_POST );
if (! isset ( $submitbutton1 ) && !isset($submitbutton2)) {
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Q1? <input type="number" name="nbtemplate" min="1" max="30"> <input
type="submit" name="submitbutton1" value="Confirm!">
</form>
<?php ;
}
if (isset ( $submitbutton1 )) {
if (! empty ( $_POST ['nbtemplate'] ) != "") {
echo "<b>{$_POST['nbtemplate']}</b> !\n";
echo "<br />";
$Nnbtemplate = $_POST ['nbtemplate'];
$result = mysql_query("UPDATE tb SET day='$Nnbtemplate'") or
die(mysql_error());
?>
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method='post'>
Q2? <br>
<?php
for($i = 1; $i <= $Nnbtemplate; $i ++) { // start loop
echo "Template ";
echo $i;
?>
<input type="number" name="nbtime" min="1" max="96">
<?php
}
echo '<input type="submit" name="submitbutton2" value="Confirm!">';
echo '</form>';
}
}
if ( isset ( $submitbutton2 )) {
if (! empty ( $_POST ['nbtime'] ) != "") {
echo "<b>{$_POST['nbtime']}</b> !\n";
echo "<br />";
$nbtime = $_POST ['nbtime'];
for($j = 1; $j <= $nbtime; $j ++) {
echo "Time";
echo $j;
?>
Q3:
<input type="time" name="starttime"> To <input type="time"
name="endtime">
<?php
}
echo '<input type="submit">';
echo '</form>';
}
}
?>
<form action="" method="post">
<?php
$i=0;
while(i<4){ ?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
}
?>
<input type="submit" name="btnsubmit"/>
</form>
if(isset($_POST['btnsubmit']))
{
$i=0;
while($i<4)
{
echo $i;
$chek=$_POST['chkApprove_'.$i];// Error Undefined Index
$i++;
}
}
Error is displayed as Undefined index: chkApprove_0...chkApprove_3. Am i doing something wrong here.
$ was missing in you while loop before "i". and $i was not increamenting.
<?php
$i=0;
while($i<4){ ?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
$i++;
}
?>
try this : you start from $i=0 but not increment $i++.
<form action="" method="post">
<?php
$i=0;
while($i < 4)
{
?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
$i++;
}
?>
<input type="submit" name="btnsubmit"/>
</form>
<?php
if(isset($_POST['btnsubmit']))
{
$i=0;
while($i < 4) {
echo $i;
$chek=$_POST['chkApprove_'.$i];// Error Undefined Index
$i++;
}
}
?>
well you have a syntax error and you forget to increment $i in this loop:
$i=0;
while(i<4){ ?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
}
should be
$i=0;
while($i<4){ ?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
$i++;
}
Because you don't increment $i in the intial loop, but in the latter looop you do, you get the undefined index
Your corrected code:
<form action="" method="post">
<?php
$i=0;
while($i<4){
?>
<input type="checkbox" name="<?php echo 'chkApprove_' . $i; ?>"/>
<?php
$i++;
}
?>
<input type="submit" name="btnsubmit"/>
</form>
<?php
if (isset($_POST['btnsubmit'])) {
$i = 0;
while ($i < 4) {
echo $i;
$chek = $_POST['chkApprove_' . $i];
// Error Undefined Index
$i++;
}
}