Jump to content

display rand number in textfield


windjohn

Recommended Posts

Trying to display a random number in the text field.

 

 

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="post">

<legend><h2>test</h2></legend>
<div width="175" align="left">
<p><b>rand</b>
<input type="text" size="10" name="rand" title="rand 1 to 6" value="<?php echo $rand; ?>" />

</p>

<p>
<input type="hidden" name="status" value="" />
   <div align="left">
<input type="submit" name="submit" value="submit" />
</b>
<?php

if( isset($_POST['submit'] ) ) 


$rand = $_POST['rand'];



function randnumber()
{
                
                $rand = rand(1,6);

                                                
   }

randnumber();

?>
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/125736-display-rand-number-in-textfield/
Share on other sites

In the code you're listing here, you are echoing a "$rand" variable, not the "rand()" function. Are you declaring this "$rand" variable somewhere that you're not posting?

 

If not, either add:

<?php
$rand = rand(1,6); // or
$rand = round(rand(1,6)); // if you only want whole numbers
?>

 

or just echo the function directly by replacing your text input line with this:

<input type="text" size="10" name="rand" title="rand 1 to 6" value="<?php echo round(rand(1,6)); ?>" />

curious why if I echo $rand;  below the randnumber();  it displays but if I enter

 

the $rand in the text area will not display?

 

 

            <input type="text" size="10" name="rand" title="rand 1 to 6" value="<?php echo $rand; ?>"  /> 

 

 

 

 

 

 

 

<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="post">

<legend><h2>test</h2></legend>
<div width="175" align="left">
<p><b>rand</b>
<input type="text" size="10" name="rand" title="rand 1 to 6" value="<?php echo $rand; ?>"  />

</p>

<p>
<input type="hidden" name="status" value="" />
   <div align="left">
<input type="submit" name="submit" value="submit" />
</b>
<?php

if( isset($_POST['submit'] ) ) 


$rand = $_POST['rand'];



function randnumber()
{
                
                $rand = rand(1,6);

                                                
   }

randnumber();


echo $rand;

?>
</form>
</body>
</html>

You're using the $rand variable before you define it.

 

Do something like this:

<form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="post">

<legend><h2>test</h2></legend>
<div width="175" align="left">
<p><b>rand</b>
<input type="text" size="10" name="rand" title="rand 1 to 6" value="<?php echo rand(1,6); ?>" />

</p>

<p>
<input type="hidden" name="status" value="" />
   <div align="left">
<input type="submit" name="submit" value="submit" />
</b>
<?php

if( isset($_POST['submit'] ) ) 


$rand = $_POST['rand'];



?>
</form>
</body>
</html>

 

Ken

 

 

Yes, what ken said. But also, you're defining it in a function, which raises issues of variable scope. Why are you doing this?

 

Why not do this:

<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="post">

<legend><h2>test</h2></legend>
<div width="175" align="left">
<p><b>rand</b>
<input type="text" size="10" name="rand" title="rand 1 to 6" value="<?php echo $rand; ?>"  />

</p>
<?php
$rand = rand(1,6);
randnumber();
echo $rand;
?> 
<p>
<input type="hidden" name="status" value="" />
   <div align="left">
<input type="submit" name="submit" value="submit" />
</b>

</form>
</body>
</html>

New at php, and having a problem with how to display variables in text fields.

 

This code was meant to help me understand how to display with minimal code.  I will be using funtions and displaying variables on a larger scale and displaying in a textfield.

 

Thanks for your help

 

 

I saw you had post code in there, too. Another option, if you really wanted to use a function, you could do this:

 

<?php
function randnumber(){
  return rand(1,6);
}
$rand = randnumber();
?>

 

That would work for learning purposes, but it's a lot more work than is necessary. Especially since this would do the same thing:

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.