I'm a beginner at coding, and I have to make a small game in php for school. I have to use a hidden input, like this:
<form action="process.php" method="post">
<input type="hidden" name="rand" value="<?php rand(1,10); ?>" />
</form>
The value stands for a random number between 1 and 10 (I hope the value is correct). Now, in process.php I want to retrieve the random number by using post, so what I tried to do is the following:
<?php $random = $_POST['rand'];
echo $random; ?>
In my browser (Firefox), I'm getting the following error:
Notice: Undefined index: rand in G:\xampp\htdocs\process.php on line 2
Does anyone know how I can echo the hidden value without using complex techniques?
Thanks in advance, Maxime
You haven't echoed your rand function within the hidden input tag.
Also, there should be a submit button. Only then you can access the POST parameters.
<input type="hidden" name="rand" value="<?php echo rand(1,10); ?>" />
<input type="submit" name="submit" value="submit">
Try something like this:
if (isset($_POST['submit'])) {
echo "<pre>";
print_r($_POST); // See your POST array
$random = $_POST['rand'];
echo $random;
}
Hope this helps.
Peace! xD