-1

So im trying to build a simple game where a user tries to guess a random number.

The problem i am having is that each time i submit the form the page is refreshing so the random number is changing.

From reading other posts on $_SERVER['PHP_SELF'] i thought it should not refresh the page solely the input value.

But each time i submit the form, my random number changes.

Is there a way to submit the form and to not refresh the page.

<?php

function form(){
 echo "<form method='post' action=" . $_SERVER['PHP_SELF'] . ">";
    echo "<input type='text' name='user' /><br/>";
    echo "<input type='submit' value='submit' />";

}

form();

$userinput = $_POST['user'];

$random = rand (  1 , 4);

echo "input is " . $userinput . "  " . "random number is " . $random;
?> 
Dropfreeski
  • 45
  • 1
  • 2
  • You could reset the random number only if `$_POST['user']` is empty and echo its value in the hidden input then add another conditional to compare the user submitted number and the random number - which will only be reset if the user input is submitted blank - or perhaps when it is guessed correctly... – Steve May 08 '16 at 03:20
  • Some usefule snippets of code for what you want on this page http://stackoverflow.com/questions/33816772/get-and-echo-inputs-from-textarea-repeatedly/33816873#33816873 – Steve May 08 '16 at 04:31
  • Thanks steve, this is exactly what i was looking for. – Dropfreeski May 09 '16 at 22:16
  • Glad it was of use to somebody even though it is now closed as "too broad". – Steve May 14 '16 at 02:48

2 Answers2

1

$_SERVER['PHP_SELF'] is the URL of the page, minus the domain. http://php.net/manual/en/reserved.variables.server.php

If you don't want to "load" another page, then use AJAX.

Sometimes people also use the same page by putting if $_POST['variable'] in the script. Check if $_POST exists

You could also add the random number to the form in a hidden input, like below:

<?php

function form(){
    echo "<form method='post' action=" . $_SERVER['PHP_SELF'] . ">";
    echo "<input type='text' name='user' /><br/>";
    echo "<input type='submit' value='submit' />";
    echo "<input type='hidden' name='random' value=".rand(1, 4)."/><br/>";
}

if (!empty($_POST['user'])) {
    $userinput = $_POST['user'];
    $random = $_POST['random'];
    echo "input is " . $userinput . "  " . "random number is " . $random;
} else {
    form();
}

?>
Community
  • 1
  • 1
William
  • 11
  • 3
  • thank you for your help. But i dont think this solves the problem because i want the user to be able to guess the random number that they can keep guessing until they get the correct number. Again this will give one instance of the random number and one instance of the inputted number which will reset rather than allowing the random to stay assigned until it is is guessed. Does that make sense? – Dropfreeski May 07 '16 at 09:59
0

You seem to be confused. The random number changes because that's what happens when you generate a new random number, and that's what you do every time this script runs (i.e., every time you load the page). Your form submission reloads the page because that's how submitting a <form> without any JavaScript works.

It sounds like you're trying to submit the form without reloading. In that case, you need to use AJAX. That is, submit the form using JavaScript, but without redirecting the browser.

To be very clear: $_SERVER['PHP_SELF'] is totally irrelevant to your issue. Using that as the value of the action attribute of your form tag just tells the browser to post to the same page, as opposed to some other page. It still does a full POST request and thus reloads the page. In other words, it doesn't matter what you use as the action attribute if your goal is to stay on the same page; you're going to have to use JavaScript no matter what you put there.

elixenide
  • 44,308
  • 16
  • 74
  • 100