Basically I'm trying to display a maths equation with 2 random variables between 0,100.
I need to store 2 different variables which would be: amount of questions correct, and total questions answered.
Only by displaying one question at a time and on submit, prompting the user if they want to continue answering questions or to stop if continuing then refreshing the random values. upon stopping displaying the 3 stored variables.
in the format of " " correct and " " total questions answered.
<html>
<head>
<title></title>
<?php
function displayQuestion(){
$firstVal = rand(0,100);
$secondVal = rand(0,100);
$valTotal = $firstVal + $secondVal;
echo $firstVal . "+" . $secondVal . "=";
}
function answersCorrect(){
$answer = 0;
$firstVal = rand(0,100);
$secondVal = rand(0,100);
$valTotal = $firstVal + $secondVal;
do {$valTotal;
$answer ++;
function displayAnswer() {
if ($answer == $valTotal){
$correct ++;
$answer ++;
}
echo "<p>You got {$correct} answers correct out of {$answer}.</p>";
}
}
while ($answer == $valTotal);
$correct = 0;
}
?>
<script>
function query(){
prompt("More questions (y/n)?");
if (prompt != y || Y){
<?php
echo "answersCorrect();";
?>
} else {
function reloadPage(){
location.reload();
}
}
}
</script>
</head>
<body>
<form action="calculate_randMath.php" method="post" id="mathsForm" name="answer">
<p>Question: <?php echo displayQuestion(); ?> </p>
<p>Your answer: <input type="text" name="answer" id="$answer"/></p>
<p><input type="submit" value="answer" name="Submit" onClick="query()" /></p>
</form>
</body>
</html>
Related
I am trying to create an adding game PHP and HTML, where the user guesses the sum of two random integers. However, when the user submits the answer, the integers randomize again, changing the sum and making the user's guess incorrect.
<?php
$x = (rand(1,10));
$y = (rand(1,10));
$sum = $x + $y;
if (isset($_POST['submit']))
{
$guess = $_POST['guess'];
if ($sum == $guess)
{
echo "nice, you got it right.";
}
else {
echo "aw, too bad. <br>";
}
}
echo "<br>What is " . $x . " + " . $y . "? <br>";?>
<form action="" method="post">
<input name="guess" type="text" />
<input name="submit" type="submit" />
</form>
I am expecting the output to be "nice, you got it right" when the $guess==$sum, but $sum changes when the form is submitted, making the 2 variables unequal.
Try to use session and store the two random number. PHP Session
just like this.
<?php
session_start();
if (isset($_POST['submit']))
{
$sum = $_SESSION['y'] + $_SESSION['x'];
$guess = $_POST['guess'];
if ($sum == $guess)
{
echo "nice, you got it right.";
}
else {
echo "aw, too bad. <br>";
}
}
$_SESSION['x']= (rand(1,10));
$_SESSION['y']= (rand(1,10));
echo "<br>What is " . $_SESSION['x'] . " + " . $_SESSION['y'] . "? <br>";
?>
<form action="" method="post">
<input name="guess" type="text" />
<input name="submit" type="submit" />
</form>
I hope I answer your problem. just read the PHP session it helps you a lot.
You can create a hidden input field to post the sum value. Then compare that value with the guessed value.
<?php
$x = (rand(1,10));
$y = (rand(1,10));
$sum = $x + $y;
if (isset($_POST['submit'])) {
$guess = $_POST['guess'];
$value = $_POST['sum'];
if ($value == $guess) {
echo "nice, you got it right.";
} else {
echo "aw, too bad. <br>";
}
}
echo "<br>What is " . $x . " + " . $y . "? <br>";
?>
<form action="" method="post">
<input name="guess" type="text" />
<input type="hidden" name="sum" value="<?=$sum?>" />
<input name="submit" type="submit" />
</form>
You should store your $x and $y in a hidden input.
Calculate the sum by using the post value instead of generating new value.
I'm new to php and here's my code for a number guessing game. Since I don't know how to define a random number, I chose the number myself, 80. I'm trying to store all the guesses prior to the right one, and after the right guess, print them out on the screen. But I can't seem to be able to get it right since it only prints only the last guess before the right one.
Any help is appreciated!
<html>
<head>
</head>
<body>
<?php
$allguesses = array();
if($_SERVER["REQUEST_METHOD"] == "POST"){
$t = $_POST["guess"];
$sayi = 80;
if($sayi >$t){
echo 'Guess higher';
}elseif($sayi == $t){
echo "You've guessed it right!<br>";
echo 'Guessed numbers: <br>';
foreach($_POST["tmn"] as $y){
echo $y . ',';
}
}else{
echo 'Guess lower';
}
array_push($allguesses,$t);
}
?>
<form method="post">
Guess the number:
<input type="number" name="guess" min ="1" max = "100"><br>
<input type="submit" name="submit">
<?php
foreach($allguesses as $x){
echo "<input type ='hidden' name = 'tmn[]' value=' ".$x . " '>";
}
?>
</form>
</body>
</html>
Sessions seem the best for where you are in your learning curve.
The session allows the normal stateless http protocol to remember things between each submission of a form or forms. So we will save each guess in an array of guesses in the SESSION, which in PHP is an array as well.
<?php
session_start(); // create a session, or reconnect to an existing one
if( $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["guess"]) ) {
$_SESSION['guesses'][] = $_POST["guess"]; // keep an array of guesses
// $t = $_POST["guess"]; no need for extra variables on the stack
$sayi = 80;
if($sayi > $_POST["guess"]){
echo 'Guess higher';
}elseif($sayi == $_POST["guess"]){
echo "You've guessed it right!<br>";
echo 'Guessed numbers: <br>';
foreach($_SESSION['guesses'] as $guess){
echo $guess . ',';
}
$_SESSION['guesses'] = array(); // clear the old guesses out
}else{
echo 'Guess lower';
}
}
?>
<html>
<head>
</head>
<body>
<form method="post">
Guess the number:
<input type="number" name="guess" min ="1" max = "100"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
Removed uneeded codes.
Changed method of showing previous guesses with the below code.
echo implode( ", ", $_POST["tmn"] ); // cleaner
This block handles storing previous guesses into an array that is used for displaying previous guesses.
if( isset( $_POST ) ) {
$_POST["tmn"][] = $t;
}
Fixed previous version of below block of code so that hidden <inputs> of previous guesses are outputted properly..
<?php
if( isset( $_POST["tmn"] ) ) {
foreach($_POST["tmn"] as $x){
echo "\n<input type ='hidden' name = 'tmn[]' value='$x'>";
}
}
?>
Updated Code:
<html>
<head>
</head>
<body>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$t = $_POST["guess"];
$sayi = 80;
if($sayi >$t){
echo 'Guess higher';
}elseif($sayi == $t){
echo "You've guessed it right!<br>";
echo 'Guessed numbers: <br>';
echo implode( ", ", $_POST["tmn"] );
}else{
echo 'Guess lower';
}
if( isset( $_POST ) ) {
$_POST["tmn"][] = $t;
}
}
?>
<form method="post">
Guess the number:
<input type="number" name="guess" min ="1" max = "100"><br>
<input type="submit" name="submit">
<?php
if( isset( $_POST["tmn"] ) ) {
foreach($_POST["tmn"] as $x){
echo "\n<input type ='hidden' name = 'tmn[]' value='$x'>";
}
}
?>
</form>
</body>
</html>
For random numbers you can use rand($min, $max). to store the guesses you can use the global variable $_SESSION.
<html>
<head>
</head>
<body>
<?php
//start session (needed to use the $_SESSION variable)
start_session();
if($_SERVER["REQUEST_METHOD"] == "POST"){
//if empty -> initalize array
if (empty ($_SESSION['allguesses']){
$_SESSION['allguesses'] = array ()
}
$t = $_POST["guess"];
$sayi = 80;
if($sayi >$t){
echo 'Guess higher';
}elseif($sayi == $t){
echo "You've guessed it right!<br>";
echo 'Guessed numbers: <br>';
//echo all guesses from $_SESSION variable
foreach($_SESSION['allguesses'] as $y){
echo $y . ',';
}
}else{
echo 'Guess lower';
}
//push in $_SESSION variable
array_push($_SESSION['allguesses'],$t);
}
?>
<form method="post">
Guess the number:
<input type="number" name="guess" min ="1" max = "100"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
I am new to PHP and trying to count all the uppercase letters in the text area, thought I am not able get anything when I hit the 'submit' button. Here is my code :
<!DOCTYPE html>
<html>
<body>
<?php
if(isset($_POST['submit'])) {
function caps($s) {
$u = 0;
$d = 0;
$n = strlen($s);
for ($x=0; $x<$n; $x++) {
$d = ord($s[$x]);
if ($d > 64 && $d < 91) {
$u++;
}
}
return $u;
}
$n1=$_POST['n1'];
echo 'caps: ' . caps($n1) . "\n";
}
?>
<form><textarea rows="4" cols="50" name="n1" value="<?php if(isset($_POST['n1'])){echo htmlspecialchars($_POST['n1']);}?>"></textarea>
<br><input type="submit" name="submit" value="Submit"></form>
</body>
</html>
This example will help you.
preg_match_all("/[A-Z]$/", $s, $matches);
$all_upper_cases = count($matches);
Use this function:
function count_capitals($s) {
return strlen(preg_replace('![^A-Z]+!', '', $s));
}
ex.
$n1=$_POST['n1'];
echo 'caps: ' . count_capitals($n1) . "\n";
textbox:
<textarea rows="4" cols="50" name="n1" value="<?php count_capitals($n1) ?>"></textarea>
You forgot to set the form method to post your code should be something like:
<!DOCTYPE html>
<html>
<body>
<?php
if(isset($_POST['submit'])) {
function caps($s) {
$u = 0;
$d = 0;
$n = strlen($s);
for ($x=0; $x<$n; $x++) {
$d = ord($s[$x]);
if ($d > 64 && $d < 91) {
$u++;
}
}
return $u;
}
$n1=$_POST['n1'];
echo 'caps: ' . caps($n1) . "\n";
}
?>
<form method="post"><textarea rows="4" cols="50" name="n1" value="<?php if(isset($_POST['n1'])){echo htmlspecialchars($_POST['n1']);}?>"></textarea>
<br><input type="submit" name="submit" value="Submit"></form>
</body>
</html>
Make sure you set the form method to post.
If you don't provide method the form uses get method instead of post.
I'm using PHP's Rand() function to generate two random numbers that I can compare against the user's, and echo out a success message if the user's answer equals (1st random number + 2nd random number.) However, I'm running into problems.
I suspected that the form was re-generating the numbers every time the form POSTED and the input was collected, so I tried using sessions instead to keep those numbers persistent. It's a mess to say the least.
I found this existing post: Problems with guess a number game , but the solution didn't remedy my problem.
<?php
if(empty($_POST))
{
$_SESSION['$number1'] = Rand(0, 100);
$_SESSION['$number2'] = Rand(0, 100);
}
if($_POST["submit"])
{
$input = $_POST['input'];
if($input == ($_SESSION['$number1'] + $_SESSION['$number2']))
{
echo "Correct! ";
}
else
{
echo "Incorrect! ";
}
}
echo "<hr><br> What is... <b>" . $_SESSION['$number1'] . " + " . $_SESSION['$number2'] . "</b>";
?>
<form action="" method="post">
<input type="text" name="input">
<input type="submit" name="submit">
</form>
<?php
echo "<b>DEBUG ANSWER: </b> " . ($_SESSION['$number1'] + $_SESSION['$number2']);
?>
Any help would be appreciated!
I changed a few things, personally, I wouldn't use the session, rather user hidden inputs. (if you're worried about security.. you shouldn't be.. numbers game, not banking site)
<?php
//Create a function to generate the random numbers (So we can re-use it)
function generateNumbers()
{
$one = Rand(0, 100);
$two = Rand(0, 100);
//Now return the random numbers
return array('number1' => $one, 'number2' => $two);
}
//Check if the submit button has been clicked
if($_POST["submit"])
{
$input = $_POST['input'];
if($input == $_POST['number1'] + $_POST['number2'])
{
echo "Correct! ";
}
else
{
echo "Incorrect! ";
}
}
//Now we create the numbers
$numbers = generateNumbers();
echo "<hr><br> What is... <b>" . $numbers['number1'] . " + " . $numbers['number2'] . "</b>";
?>
<form action="" method="post">
<input type="text" name="input">
<input type="submit" name="submit">
<!-- Personally I would rather use hidden inputs, than use the session -->
<input type="hidden" name="number1" value="<?php echo $numbers['number1'] ?>" />
<input type="hidden" name="number2" value="<?php echo $numbers['number2'] ?>" />
</form>
<?php
echo "<b>DEBUG ANSWER: </b> " . ($numbers['number1'] + $numbers['number2']);
?>
PHP Level = beginner
I am trying to write a simple program that displays the number-times of a particular
value when placed in the input box. I have tried to use the post method to do this but each time the program is up running and the submit button is selected, it displays the whole code of the php file 'timescalc.php'. I'll like to know what I am doing wrong, although I know that
the calculations with the if statements might be wrong.
Heres the code
<DOCTYPE html>
<html>
<head>
<title>Number Times</title>
</head>
<body>
<h1>Number Times Table Calculator</h1>
<form method="post" action="timescalc.php">
Enter Number : <input type="text" name="number"> <br>
<input type="submit" value="submit"/>
</form>
<?php
$number = $_POST['number'];
if ($number == 2, $number ++2)
{
echo $number . ;
}
else if ($number == 3, $number ++3)
{
echo $number . ;
}
else if ($number == 4, $number ++4)
{
echo $number . ;
}
else if ($number == 5, $number ++5)
{
echo $number . ;
}
else
{
echo "pick numbers from 2 to 5 only" ;
}
?>
</body>
</html>
You have numerous syntax errors in your php code but they are corrected here, you also need a webserver like WAMP SERVER to run php code. Good luck coding!
<DOCTYPE html>
<html>
<head>
<title>Number Times</title>
</head>
<body>
<h1>Number Times Table Calculator</h1>
<form method="post" action="test.php">
Enter Number : <input type="text" name="number"> <br>
<input type="submit" value="submit"/>
</form>
<?php
$number = $_POST['number'];
if ($number == 2)
{
$number = $number * 2;
echo $number ."." ;
}
else if ($number == 3)
{
$number = $number * 3;
echo $number ."." ;
}
else if ($number == 4)
{
$number = $number * 4;
echo $number ."." ;
}
else if ($number == 5)
{
$number = $number * 5
echo $number ."." ;
}
else
{
echo "pick numbers from 2 to 5 only" ;
}
?>
</body>
</html>