natttan
natttan

Reputation: 129

how to stop reloading page with every new form input (submit) in php?

I want to make script that allows user guessing numbers until hits the right one. The idea is that php "imagine" one number using rand().User has unlimited number of hits. But the problem is that user has to find the very same number as php "imagine" at the beginning of his quest, and my script reloads every time I input new number and always outputs new random number. What should I do in order to fix this problem? Thanks in advance.

$r= rand("1","10");

if (isset($_POST['unos'])){

$unos = $_POST['unos'];
if ($unos==$r){
echo "Congratulations you did not miss the center";
}
elseif ($unos<$r){
echo "your number is less then quested number</br>try again";

}
elseif ($unos>$r){
echo "your number is higher then quested number</br>try again"; 
}

}
else{
echo "Go!";
}

html

<form action="?" method="post">
<input type="number" name="unos" id="unos">
<input type="submit" value="Start">
</form>

Upvotes: 0

Views: 257

Answers (3)

Ynhockey
Ynhockey

Reputation: 3932

In general PHP is not really the right thing to use to solve this problem. Instead, you should use JavaScript. However, if you want it to be completely secure you might need to use PHP after all, but you will need to call the number from a static source, such as a database or file. For instance, when the user enters the page, write the number to a file file[session id], where session id is the session ID which you can get using session_id(). Then write the random number to that file and check against that. At the end delete the file, with unlink() combined with ignore_user_abort() in case the user exits the page. Even better to do the above with a database, but I don't assume any additional skills.

You can also store the random number in a session, if you never want to keep it.

Upvotes: 1

lelloman
lelloman

Reputation: 14183

you can use php sessions

<?php
    session_start();
    if(!isset($_SESSION["numbertoguess"])){
        $_SESSION["numbertoguess"] = generate_randomNumber();
    }

and then check if the user input equals the $_SESSION["numbertoguess"] If you also don't want to reload the page, think about using ajax. with jquery is very convenient

here an introduction to php sessions

Upvotes: 1

mvbrakel
mvbrakel

Reputation: 936

You could put the random value in a session. This way the number will remain the same throughout the entire quest. After the quest you could unset the value. The value within the session would not be visible to the client. Other options like setting the value in a hidden field in the form could still be located in the source.

An example;

session_start();
$r= isset($_SESSION['random']) ? $_SESSION['random'] : rand("1","10");

if (isset($_POST['unos'])){

$unos = $_POST['unos'];
if ($unos==$r){
echo "Congratulations you did not miss the center";
}
elseif ($unos<$r){
echo "your number is less then quested number</br>try again";

}
elseif ($unos>$r){
echo "your number is higher then quested number</br>try again"; 
}

}
else{
unset($_SESSION['random']); //or session_destroy();
echo "Go!";
}

Upvotes: 1

Related Questions