windjohn Posted September 25, 2008 Share Posted September 25, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/125736-display-rand-number-in-textfield/ Share on other sites More sharing options...
F1Fan Posted September 25, 2008 Share Posted September 25, 2008 rand(); rand(1,10); rand(1,100); http://www.w3schools.com/php/func_math_rand.asp Quote Link to comment https://forums.phpfreaks.com/topic/125736-display-rand-number-in-textfield/#findComment-650172 Share on other sites More sharing options...
windjohn Posted September 25, 2008 Author Share Posted September 25, 2008 I understand how to use the rand function, just cannot get the number to display in the textfield. Quote Link to comment https://forums.phpfreaks.com/topic/125736-display-rand-number-in-textfield/#findComment-650178 Share on other sites More sharing options...
F1Fan Posted September 25, 2008 Share Posted September 25, 2008 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)); ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/125736-display-rand-number-in-textfield/#findComment-650181 Share on other sites More sharing options...
windjohn Posted September 25, 2008 Author Share Posted September 25, 2008 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; ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/125736-display-rand-number-in-textfield/#findComment-650188 Share on other sites More sharing options...
F1Fan Posted September 25, 2008 Share Posted September 25, 2008 Dunno. Re-post all of your code. Quote Link to comment https://forums.phpfreaks.com/topic/125736-display-rand-number-in-textfield/#findComment-650191 Share on other sites More sharing options...
windjohn Posted September 25, 2008 Author Share Posted September 25, 2008 <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> Quote Link to comment https://forums.phpfreaks.com/topic/125736-display-rand-number-in-textfield/#findComment-650195 Share on other sites More sharing options...
kenrbnsn Posted September 25, 2008 Share Posted September 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/125736-display-rand-number-in-textfield/#findComment-650196 Share on other sites More sharing options...
F1Fan Posted September 25, 2008 Share Posted September 25, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/125736-display-rand-number-in-textfield/#findComment-650200 Share on other sites More sharing options...
windjohn Posted September 25, 2008 Author Share Posted September 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/125736-display-rand-number-in-textfield/#findComment-650211 Share on other sites More sharing options...
F1Fan Posted September 25, 2008 Share Posted September 25, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/125736-display-rand-number-in-textfield/#findComment-650218 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.