Basic rand( ) script question

Hi all,

I got a basic php script where a person enters min and max numbers then clicks a button to generate a random number. It works but I am not so sure about the method. First here is the code:

[php]

<?php error_reporting(0); $min = $_POST['min']; $max = $_POST['max']; ?> Min No:

Max No:

<?php $rand = rand($min,$max); print "The value is: $rand"; ?> [/php]

Is there any way besides using the error_reporting(0) to get the script to work. If I leave this line out the script gives errors. It seems like there should be a better option.

Also, to get the form fields to remember the numbers on refreshing the min and max variables have been echoed in the value attributes of the form - is this best practice?

Thanks,

Andy :wink:

Hi Andy,

Firstly error_reporting doesnt cause errors in the script setting it to 0 just hides them. Ill paste the code below as to what i would do with this script.

[php]
error_reporting(0);
// check that the user has clicked the submit button
if(isset($_POST[‘submit’]))
{
// always check the inputted data as it can never be trusted(more checks can be added)
$min = mysql_real_escape_string($_POST[‘min’]);
$max = mysql_real_escape_string($_POST[‘max’]);
}
?>

Min No:

Max No:

<?php $rand = rand($min,$max); print "The value is: $rand"; [/php]

Posting the values in is a good idea in my opinion if you don’t want the user to keep re adding the data.

HI Zammit1234, thanks for taking the time to read through the code and respond., it makes sense, Andy :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service