Jump to content

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);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.