Technotron
Technotron

Reputation: 19

Rand() returns same value; in a hidden input

For some reason if I try to assign a value to my input type hidden then I always get the same value (1), I can assign a value to it with javascript but not php..:

<?php
 $num = rand(1, 4);                     
?>  

 <?php echo $num; ?><?php echo $num; ?><?php echo $num; ?>

<input type="hidden" value="<?php echo $num; ?>" name="catch_num1">

If rand gave 4 this would give 444 but in input type it would always give value="1"??

It seems like a bug to me but I'm checking all my code in between.

Upvotes: 0

Views: 3485

Answers (2)

Mikulas Dite
Mikulas Dite

Reputation: 7941

Might be you set constant value to https://www.php.net/srand in prior to calling rand.

Upvotes: 1

M. Suleiman
M. Suleiman

Reputation: 848

It can't be. rand(min, max) always gives value between the min and the max.

Have you tried rand(4, 14)? does it give you 1 too? are you sure you echo the $_POST value that actually IS this one and not another?

Try using mt_rand()

Edit: well, I have found lots of results of the same problem. But each got its own reason.

If you're using Firefox for example, refresh using CTRL+F5 to omit cache. The random number might have been stored in cache or something?!

Upvotes: 0

Related Questions