So, I'm building a simple program that requires the user input a number which is then posted and generated into several random similar numbers.
The program then requires the user choose the correct variable, which would then be validated by the program.
I'm having issues with what I think is a variable not being 'available' for lack of a better word in the if/else statement.
I feel like I'm making a really simple/stupid mistake.
<?php
$numb = $_GET["number"];
switch ($numb) {
case 1:
echo "1x<br>";
$ans = 1; $n1 = rand(($ans - 5), ($ans + 5)); $n2 = rand(($ans - 5), ($ans + 5)); $n3 = 1; $n4 = rand(($ans - 5), ($ans + 5));
break;
case 2:
echo "2";
break;
case 3:
echo "3";
break;
} echo $ans;
if(isset($_POST['submit']))
{
$rb = $_POST['radio'];
if($rb == $ans){echo "test";}
else{echo "fail";}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="radio" name="radio" value="<?php echo $n1; ?>"><?php echo $n1 ?>
<input type="radio" name="radio" value="<?php echo $n2; ?>"><?php echo $n2 ?>
<input type="radio" name="radio" value="<?php echo $n3; ?>"><?php echo $n3 ?>
<input type="radio" name="radio" value="<?php echo $n4; ?>"><?php echo $n4 ?>
<input type="submit" name="submit" value="submit" />
</form>
You have to define the variables before useing them in the switch/case.
// Check if get is used
if (isset($_GET['number'])){
$numb = $_GET["number"];
// predefine Variables here
$ans = '';
$n1 = '';
$n2 = '';
$n3 = '';
$n4 = '';
switch ($numb)
{
case 1:
echo "1x<br>";
$ans = 1;
$n1 = rand(($ans - 5), ($ans + 5));
$n2 = rand(($ans - 5), ($ans + 5));
$n3 = 1;
$n4 = rand(($ans - 5), ($ans + 5));
break;
case 2:
echo "2";
break;
case 3:
echo "3";
break;
}
echo $ans;
if (isset($_POST['submit']))
{
$rb = $_POST['radio'];
if ($rb == $ans)
{
echo "test";
}
else
{
echo "fail";
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="radio" name="radio" value="<?php echo $n1; ?>"><?php echo $n1 ?>
<input type="radio" name="radio" value="<?php echo $n2; ?>"><?php echo $n2 ?>
<input type="radio" name="radio" value="<?php echo $n3; ?>"><?php echo $n3 ?>
<input type="radio" name="radio" value="<?php echo $n4; ?>"><?php echo $n4 ?>
<input type="submit" name="submit" value="submit"/>
</form>
<?php
} else {
echo "Number not set in get!";
}
You have to use $global variable.
PHP Variables Manual
<?php
$numb = $_GET["number"];
$ans = 0;
switch ($numb) {
case 1:
echo "1x<br>";
$ans = 1;
$n1 = rand(($ans - 5), ($ans + 5));
$n2 = rand(($ans - 5), ($ans + 5));
$n3 = 1;
$n4 = rand(($ans - 5), ($ans + 5));
break;
case 2:
echo "2";
break;
case 3:
echo "3";
break;
} echo 'Answer : [' . $ans . ']<br>';
if(isset($_POST['submit']))
{
$rb = $_POST['radio'];
echo 'PostAnswer : [' . $ans . ']<br>';
if($rb == $ans)
{
echo "Good!";
}
else
{
echo "Bad!";
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?number=1" method="POST">
<input type="radio" name="radio" value="<?php echo $n1 ?>"><?php echo $n1 ?>
<input type="radio" name="radio" value="<?php echo $n2 ?>"><?php echo $n2 ?>
<input type="radio" name="radio" value="<?php echo $n3 ?>"><?php echo $n3 ?>
<input type="radio" name="radio" value="<?php echo $n4 ?>"><?php echo $n4 ?>
<input type="submit" name="submit" value="submit" />
</form>
Your page will change after pressing submit button.
ex:) url_php = test.php request parameter : ?number=1
1) inputing /test.php?number=1
2) $ans = 1
3) After pressing submit button
4) $ans = ""
Like that! You have to maintain ?number=1 or fix it!
Related
EDIT: When I print the $answer variable by itself it always returns the correct answer of the current question.
I am currently coding a PHP script that produces a simple, but completely random math quiz. How it works is that is gets two random numbers between 0-9 and a random operator from '-', '+', '*'. The user must enter into the text box the answer of the shown question. From here it's pretty straightforward to understand.
However, the issue I am having is that no matter what the user enters the only questions that are validated as correct are ones where the answer is 0.
Here is my code so far.
<?php
require 'functions.php';
$body = "";
$score = 0;
$count = 0;
if(isset($_POST['submit']))
{
$firstDigit = $_POST['lho'];
$secondDigit = $_POST['rho'];
$operator = $_POST['op'];
$userAnswer = $_POST['answer'];
$count = $_POST['count'];
$score = $_POST['score'];
$answer = evaluate($firstDigit, $secondDigit, $operator);
if($answer == $userAnswer)
{
$count++;
$score++;
$body .= "\n<h1>Congratulations!</h1>\n\n";
$body .= "$score out of $count";
}
else
{
$count++;
$body .= "\n<h1>Sorry!</h1>\n\n";
$body .= "$score out of $count";
}
}
header( 'Content-Type: text/html; charset=utf-8');
print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
?>
<?php
include("./header.php");
?>
<h1>Math Quiz</h1> <br /> <br />
<?php
print $body;
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h3><?php echo $firstDigit; ?> <?php echo $operator; ?> <?php echo $secondDigit; ?> = ?
<input type="text" name="answer" size="2" /></h3>
<p><input type="submit" name="submit" value="Try It!" />
<input type="hidden" name="lho" value="<?php echo randdigit(); ?>" />
<input type="hidden" name="rho" value="<?php echo randdigit(); ?>" />
<input type="hidden" name="op" value="<?php echo randop(); ?>" />
<input type="hidden" name="score" value="<?php echo $score++; ?>" />
<input type="hidden" name="count" value="<?php echo $count++; ?>" /></p>
</form>
<?php
include("./footer.php");
?>
My evaluate function is this:
function evaluate($d1, $d2, $op) {
switch($op) {
case '+' : // addition
$result = $d1 + $d2;
break;
case '-' : // subtraction
$result = $d1 - $d2;
break;
case '*' : // multiplication
$result = $d1 * $d2;
break;
default : // Unidentified, return safe value
$result = 0;
}
return $result;
}
Here is the randop() function and the randdigit() function:
/* Return a number in the range 0-9 inclusive
*/
function randdigit() {
return mt_rand(0,9);
} // end functionranddigit()
function randop(){
$ops = array('+', '-', '*');
// pick a random index between zero and highest index in array.
$randnum = mt_rand(0,sizeof($ops)-1);
return $ops[$randnum]; // Use the index to pick the operator
}
First on the 48th line :
<h3><?php echo $firstDigit; ?> <?php echo $operator; ?> <?php echo $secondDigit; ?> = ?
When you first load the form and until it has been submitted once,
$firstDigit, $operator, $secondDigit
Aren't set.
Then, this line wich is the equation to solve is filled with the old equation that needed to be solved AND your hidden fields are filled with new numbers, invisible to the user using randdigit() and randop().
<input type="hidden" name="lho" value="<?php echo randdigit(); ?>" />
<input type="hidden" name="rho" value="<?php echo randdigit(); ?>" />
<input type="hidden" name="op" value="<?php echo randop(); ?>" />
Here is the code that works well for me :
<?php
require 'functions.php';
$body = "";
$score = 0;
$count = 0;
$newFdigit = randdigit();
$newSdigit = randdigit();
$newOperator = randop();
if(isset($_POST['submit']))
{
$firstDigit = $_POST['lho'];
$secondDigit = $_POST['rho'];
$operator = $_POST['op'];
$userAnswer = $_POST['answer'];
$count = $_POST['count'];
$score = $_POST['score'];
$answer = evaluate($firstDigit, $secondDigit, $operator);
if($answer == $userAnswer)
{
$count++;
$score++;
$body .= "\n<h1>Congratulations!</h1>\n\n";
$body .= "$score out of $count";
}
else
{
$count++;
$body .= "\n<h1>Sorry!</h1>\n\n";
$body .= "$score out of $count";
}
}
header( 'Content-Type: text/html; charset=utf-8');
print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
?>
<h1>Math Quiz</h1> <br /> <br />
<?php
print $body;
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h3><?php echo $newFdigit; ?> <?php echo $newOperator; ?> <?php echo $newSdigit; ?> = ?
<input type="text" name="answer" size="2" /></h3>
<p><input type="submit" name="submit" value="Try It!" />
<input type="hidden" name="lho" value="<?php echo $newFdigit ?>" />
<input type="hidden" name="rho" value="<?php echo $newSdigit; ?>" />
<input type="hidden" name="op" value="<?php echo $newOperator; ?>" />
<input type="hidden" name="score" value="<?php echo $score++; ?>" />
<input type="hidden" name="count" value="<?php echo $count++; ?>" /></p>
</form>
I have a for loop that displays radio buttons and I want the first one to display as checked. But when I put a if statement inside the for loop for this the page nevers loads. Any ideas?
$mains = array(0=>'Beef Steak', 1=>'Chicken Breast', 2=>'Pork Chops');
$mainscount = count($mains);
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if($mainNO = 0){ echo 'checked="checked"'; } ?>/>
<?php } ?>
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if ($mainNO == 0) {
echo ' checked="checked" ';
} ?>/>
<?php } ?>
you use = where you should use ==
u are using assingment operator in comparision statement
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if ($mainNO == 0) {
echo " checked";
} ?>/>
and in HTML5 you can use checked only
<input type="checkbox" checked>
// Note: You used single = in if condition that is wrong, it will create indefinite loop . Tested code.
$mains = array(0=>'Beef Steak', 1=>'Chicken Breast', 2=>'Pork Chops');
$mainscount = count($mains);
for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) {
// Checked if value is 0
if($mainNO == 0){ $checked = 'checked="checked"'; }else { $checked =''; };
echo "<label for='mains".$mainNO."' class='radiobutton'>".$mains[$mainNO]."</label>";
echo "<input type='radio' name='mains' id='mains".$mainNO."' value='".$mainNO."' $checked />";
}
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 !
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have to make a tic tac toe game in php, this is what i have done so far:
<html>
<body>
<h1>Tic Tac Toe</h1>
<?php
//vars
if (!ISSET ($_POST['submit'])){
$_POST ['value'] = '';
}
//players
if (!ISSET($_POST ['turn'])){
$turn=1;
$ul='';
$um='';
$ur='';
$ml='';
$mm='';
$mr='';
$ll='';
$lm='';
$lr='';
}
else{
$turn=$_POST['turn'];
if ($turn==1){
$turn=2;
}
else{
$turn=1;
}
}
//boxes
$ul='ul';
$um='um';
$ur='ur';
$ml='ml';
$mm='mm';
$mr='mr';
$ll='ll';
$lm='lm';
$lr='lr';
//forms
$box= $_POST ['box'];
//Calculations
//player 1
if ($turn==2){
if ($box == $ul){
$ul = "X";
}
if ($box == $um){
$um = "X";
}
if ($box == $ur){
$ur = "X";
}
if ($box == $ml){
$ml = "X";
}
if ($box == $mm){
$mm = "X";
}
if ($box == $mr){
$mr = "X";
}
if ($box == $ll){
$ll = "X";
}
if ($box == $lm){
$lm = "X";
}
if ($box == $lr){
$lr = "X";
}
}
//player 2
else{
if ($box == $ul){
$ul = "O";
}
if ($box == $um){
$um = "O";
}
if ($box == $ur){
$ur = "O";
}
if ($box == $ml){
$ml = "O";
}
if ($box == $mm){
$mm = "O";
}
if ($box == $mr){
$mr = "O";
}
if ($box == $ll){
$ll = "O";
}
if ($box == $lm){
$lm = "O";
}
if ($box == $lr){
$lr = "O";
}
}
?>
<table width='100pX' height='100pX' border='1'>
<tr>
<td><?php echo $ul;?></td>
<td><?php echo $um;?></td>
<td><?php echo $ur;?></td>
</tr>
<tr>
<td><?php echo $ml;?></td>
<td><?php echo $mm;?></td>
<td><?php echo $mr;?></td>
<tr>
<td><?php echo $ll;?></td>
<td><?php echo $lm;?></td>
<td><?php echo $lr;?></td>
</tr>
</table>
<?php echo 'Player ' . $turn . ", it's your turn"?>
<form method='POST' action='<?php echo $_SERVER['PHP_SELF'];?>'>
<input type='text' name='box'>
<input type='hidden' value= "<?php echo $turn;?>" name='turn'>
<input type='hidden' value= "<?php echo $ul;?>" name='ul'>
<input type='hidden' value= "<?php echo $um;?>" name='um'>
<input type='hidden' value= "<?php echo $ur;?>" name='ur'>
<input type='hidden' value= "<?php echo $ml;?>" name='ml'>
<input type='hidden' value= "<?php echo $mm;?>" name='mm'>
<input type='hidden' value= "<?php echo $mr;?>" name='mr'>
<input type='hidden' value= "<?php echo $ll;?>" name='ll'>
<input type='hidden' value= "<?php echo $lm;?>" name='lm'>
<input type='hidden' value= "<?php echo $lr;?>" name='lr'>
<input type= 'submit' name= 'submit' value= 'Go'>
</form>
</html>
</body>
When i enter player 1's answer in the form, the desired box turns X, but then when its player 2's turn the x goes away and player 2's O shows up. How do I fix this??
You need to pass the value to the next form. You're doing a hidden input for each of the pieces in your form, but you're not getting the values.
Right below the $ul = 'ul', you need to put
$ul = $_POST['ul'];
etc for all the values. This will let you pull the previously set values into the current form.
I have a result(string) of 1,1,0,0 - These come from $sub_array['state']
Currently all of my check boxes are checked. How can I code the code below so that if its 1 its checked else its not? as the current code gives them all 'checked'
<?php
foreach($assoc_categories as $sub_array)
{
if($sub_array['state'] == 1)
{
$checked_state = " checked='checked'";
}
?>
<div>
<input
class="checkbox"
type="checkbox"
name="product_category"
class="product_category_selector"
id="product_category_<?php echo $sub_array['cat_id']; ?>"
data-id="<?php echo $sub_array['cat_id']; ?>"
<?php echo $checked_state; ?>
/>
<?php echo $sub_array['name']; ?>
</div>
<input
class="order"
type="input"
value="<?php echo $sub_array['sorder']; ?>"
/>
<?php
}
?>
Change:
if($sub_array['state'] == 1)
{
$checked_state = " checked='checked'";
}
To:
if($sub_array['state'] == 1)
{
$checked_state = " checked='checked'";
} else
{
$checked_state = "";
}
Basically, you are not clearing the previous value as the loop continues.
Alternatively, you could use:
$checked_state = ($sub_array['state'] == 1) ? " checked='checked'" : "" ;
You forget to reset checked_state or reset it to '' if $sub_array['state'] is equal to 0.
<?php
$assoc_categories = array(
array('state'=>1, 'cat_id'=>1, 'name'=>'one', 'sorder'=>1),
array('state'=>1, 'cat_id'=>2, 'name'=>'three', 'sorder'=>2),
array('state'=>0, 'cat_id'=>3, 'name'=>'four', 'sorder'=>3),
array('state'=>0, 'cat_id'=>4, 'name'=>'five', 'sorder'=>4),
);
foreach($assoc_categories as $sub_array)
{
$checked_state = $sub_array['state'] == 1 ? " checked='checked'" : '';
?>
<div>
<input
class="checkbox"
type="checkbox"
name="product_category"
class="product_category_selector"
id="product_category_<?php echo $sub_array['cat_id']; ?>"
data-id="<?php echo $sub_array['cat_id']; ?>"
<?php echo $checked_state; ?>
/>
<?php echo $sub_array['name']; ?>
</div>
<input
class="order"
type="input"
value="<?php echo $sub_array['sorder']; ?>"
/>
<?php
}