sarahspiteri Posted March 15, 2010 Share Posted March 15, 2010 So I am trying to display the value of a variable in a text box. The value is an integer. So for example : $number1= 1+2; the above is written between the php tags. The following is found in the <form> tags: <input type="text" name="number" id="number" value="<?php echo $number1; ?>" /> when I run the page the textbox is just empty. I have no idea what I am doing wrong. Any help??? Quote Link to comment Share on other sites More sharing options...
trq Posted March 15, 2010 Share Posted March 15, 2010 Can you post your actual code? Quote Link to comment Share on other sites More sharing options...
frobak Posted March 15, 2010 Share Posted March 15, 2010 try putting the variable in quotes, like below? <? echo "$number1"; ?> Quote Link to comment Share on other sites More sharing options...
sarahspiteri Posted March 15, 2010 Author Share Posted March 15, 2010 The actual code: This is not ALL of the code ... but the previous code is not important for this problem. This code is part of a function called MySlot() if($reel1 != $reel2 && $reel1 != $reel3 && $reel2 != $reel3){ if(isset($_POST['submit'])){ $amount= $_POST['amount']; $NoMatch = ($rand1/65 + $rand2/65 + $rand3/65)*$amount; echo $rand1; echo("space"); echo $NoMatch; } } } ?> </head> <body> <?php MySlot(); ?> <form id="form1" name="form1" method="post" action="index2.php"> <table align="center" width="74%"> <tr> <td width="100%" colspan="4" bgcolor="#28AD82"> <div align="center"> <input type="submit" id="submit" value="Spin!" name="submit"> </div> </td> </tr> </table> <table width="1005"> <tr align="center"> <td align="center">Amount bet: <input type="text" name="txt1" id="txt1" /> </tr> <tr align="center"> <td>You Win: <input type="text" name="txt2" id="txt2" value="<?php echo $NoMatch; ?>" /> <input type="text" name="amount" id="amount" /> </td> </tr> </table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
sarahspiteri Posted March 15, 2010 Author Share Posted March 15, 2010 Still doesn't work with the quotes Quote Link to comment Share on other sites More sharing options...
Orionsbelter Posted March 15, 2010 Share Posted March 15, 2010 Where is the code for $rand1,2 and 3? Quote Link to comment Share on other sites More sharing options...
trq Posted March 15, 2010 Share Posted March 15, 2010 try putting the variable in quotes, like below? <? echo "$number1"; ?> Variables do not need to be surrounded by quotes. There is nothing visibly wrong with the code. Are you sure $NoMatch has a value? Quote Link to comment Share on other sites More sharing options...
sarahspiteri Posted March 15, 2010 Author Share Posted March 15, 2010 the code for rand1 rand2 and rand 3 is not important .. I just simply want to display a variable value in a textbox... this value is an integer and it is the result of a calculation. I want to display this result in a textbox ... I tried " " and it still doesnt work Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 15, 2010 Share Posted March 15, 2010 This code is part of a function called MySlot() I'll guess you mean the php code that is setting the variable (leaving out relevant code almost always wastes time, same as posting a minimal example that has no resemblance to what the actual code is doing.) If so, that variable does not exist outside of the function. You either need to return the value from the function or the code you are putting inside a function definition is actually your main program logic and does not belong inside a function. Quote Link to comment Share on other sites More sharing options...
Orionsbelter Posted March 15, 2010 Share Posted March 15, 2010 It is important as you are wanting to display the variable $NoMatch and the $rand1,2,3 are are part of the calculation. $NoMatch = ($rand1/65 + $rand2/65 + $rand3/65)*$amount; We need to see all the code in order to find your problem. Quote Link to comment Share on other sites More sharing options...
sarahspiteri Posted March 15, 2010 Author Share Posted March 15, 2010 OK here is all the code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <?php function MySlot() { //my images $face="<p><img src=\"face.jpg\" width=\"250\" height=\"250\" alt=\"Face\" /></p>"; $hat="<p><img src=\"hat.jpg\" width=\"250\" height=\"250\" alt=\"Hat\" /></p>"; $cherry="<p><img src=\"cherry.jpg\" width=\"250\" height=\"250\" alt=\"Cherry\" /></p>"; $banana="<p><img src=\"cisk.jpg\" width=\"250\" height=\"250\" alt=\"Banana\" /></p>"; $faris="<p><img src=\"faris.jpg\" width=\"250\" height=\"250\" alt=\"Banana\" /></p>"; $jose="<p><img src=\"jose.jpg\" width=\"250\" height=\"250\" alt=\"Banana\" /></p>"; $iphone="<p><img src=\"iphone.jpg\" width=\"250\" height=\"250\" alt=\"Banana\" /></p>"; $edward="<p><img src=\"edward.jpg\" width=\"250\" height=\"250\" alt=\"Banana\" /></p>"; $twilight="<p><img src=\"twilight.jpg\" width=\"250\" height=\"250\" alt=\"Banana\" /></p>"; $duke="<p><img src=\"duke.jpg\" width=\"250\" height=\"250\" alt=\"Banana\" /></p>"; $englandcup="<p><img src=\"englandcup.jpg\" width=\"250\" height=\"250\" alt=\"Banana\" /></p>"; //randomly choosing 1 of the four images for each of the three reels //for ($i=0; $i<10; $i++) { //Generate a number between 0 and 19 //$rand = rand(0, 65); $rand1= rand(0,65); $rand2= rand(0,65); $rand3= rand(0,65); //Assign image based on generated weight //Weights are: Face .4, Hat .3, Cherry .8, Banana .2, megan .6, faris .1, .twilight .2, .edward .5, duke . if ($rand1 >= 0 && $rand1<=4) { $reel1= $face; echo $rand; } elseif ($rand1 >4 && $rand1<= { $reel1 = $hat; echo $rand1; } elseif ($rand1 >8 && $rand1 <= 13) { $reel1 = $banana; echo $rand1; } elseif ($rand1 >13 && $rand1 <= 19) { $reel1 = $cherry; echo $rand1; } elseif ($rand1 >19 && $rand1 <= 21) { $reel1 = $englandcup; echo $rand1; } elseif ($rand1 >21 && $rand1 <= 30) { $reel1 = $faris; echo $rand1; } elseif ($rand1 >30 && $rand1 <= 38) { $reel1 = $duke; echo $rand1; } elseif ($rand1 >38 && $rand1 <= 45) { $reel1 = $twilight; echo $rand1; } elseif ($rand1 >45 && $rand1 <= 55) { $reel1 = $edward; echo $rand1; } elseif ($rand1 >55 && $rand1 <= 56) { $reel1 = $iphone; echo $rand1; } elseif ($rand1 >56 && $rand1 <= 65) { $reel1 = $jose; echo $rand1; } if ($rand2 >= 0 && $rand2<=4) { $reel2= $face; echo $rand2; } elseif ($rand2 >4 && $rand2 <= { $reel2 = $hat; echo $rand2; } elseif ($rand2 >8 && $rand2 <= 13) { $reel2 = $banana; echo $rand2; } elseif ($rand2 >13 && $rand2 <= 19) { $reel2 = $cherry; echo $rand2; } elseif ($rand2 >19 && $rand2 <= 21) { $reel2 = $englandcup; echo $rand2; } elseif ($rand2 >21 && $rand2 <= 30) { $reel2 = $faris; echo $rand2; } elseif ($rand2 >30 && $rand2 <= 38) { $reel2 = $duke; echo $rand2; } elseif ($rand2 >38 && $rand2 <= 45) { $reel2= $twilight; echo $rand2; } elseif ($rand2 >45 && $rand2 <= 55) { $reel2= $edward; echo $rand2; } elseif ($rand2 >55 && $rand2 <= 56) { $reel2 = $iphone; echo $rand2; } elseif ($rand2 >56 && $rand2 <= 65) { $reel2 = $jose; echo $rand2; } if ($rand3 >= 0 && $rand3<=4) { $reel3= $face; echo $rand3; } elseif ($rand3 >4 && $rand3 <= { $reel3 = $hat; echo $rand3; } elseif ($rand3 >8 && $rand3 <= 13) { $reel3 = $banana; echo $rand3; } elseif ($rand3 >13 && $rand3 <= 19) { $reel3 = $cherry; echo $rand3; } elseif ($rand3 >19 && $rand3 <= 21) { $reel3 = $englandcup; echo $rand3; } elseif ($rand3 >21 && $rand3 <= 30) { $reel3 = $faris; echo $rand3; } elseif ($rand3 >30 && $rand3 <= 38) { $reel3 = $duke; echo $rand3; } elseif ($rand3 >38 && $rand3 <= 45) { $reel3 = $twilight; echo $rand3; } elseif ($rand3 >45 && $rand3 <= 55) { $reel3 = $edward; echo $rand3; } elseif ($rand3 >55 && $rand3 <= 56) { $reel3 = $iphone; echo $rand3; } elseif ($rand3 >56 && $rand3 <= 65) { $reel3 = $jose; echo $rand3; } print("<table align=\"center\">"); print("<th colspan=\"3\" align=\"center\" style=\"color:#0C0; font-size:14px; font-family:'Comic Sans MS', cursive\"> The Sloth</th>"); print("<tr>"); print("<td width=\"33%\"><center>$reel1</td>"); print("<td width=\"33%\"><center>$reel2</td>"); print("<td width=\"33%\"><center>$reel3</td>"); print("</tr>"); print("</table>"); if($reel1 != $reel2 && $reel1 != $reel3 && $reel2 != $reel3){ if(isset($_POST['submit'])){ $amount= $_POST['amount']; $NoMatch = ($rand1/65 + $rand2/65 + $rand3/65)*$amount; echo $slothWin; echo $rand1; echo("space"); echo $NoMatch; } } } ?> </head> <body> <?php MySlot(); ?> <form id="form1" name="form1" method="post" action="index2.php"> <table align="center" width="74%"> <tr> <td width="100%" colspan="4" bgcolor="#28AD82"> <div align="center"> <input type="submit" id="submit" value="Spin!" name="submit"> </div> </td> </tr> </table> <table width="1005"> <tr align="center"> <td align="center">Amount bet: <input type="text" name="txt1" id="txt1" /> </tr> <tr align="center"> <td>You Win: <input type="text" name="txt2" id="txt2" value="<?php echo "$NoMatch"; ?>" /> <input type="text" name="amount" id="amount" /> </td> </tr> </table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
trq Posted March 15, 2010 Share Posted March 15, 2010 AS PFMaBiSmAd suggested, $NoMatch does not exist where you call it. It only exists within your MySlot() function. Quote Link to comment Share on other sites More sharing options...
Rustywolf Posted March 16, 2010 Share Posted March 16, 2010 Has anyone thought of the fact hes trying to use a string as an int? if($reel1 != $reel2 && $reel1 != $reel3 && $reel2 != $reel3){ if(isset($_POST['submit'])){ if(is_numeric($_POST['amount'])) { $amount= intval($_POST['amount']); $NoMatch = ($rand1/65 + $rand2/65 + $rand3/65)*$amount; } should work Quote Link to comment Share on other sites More sharing options...
trq Posted March 16, 2010 Share Posted March 16, 2010 Has anyone thought of the fact hes trying to use a string as an int? if($reel1 != $reel2 && $reel1 != $reel3 && $reel2 != $reel3){ if(isset($_POST['submit'])){ if(is_numeric($_POST['amount'])) { $amount= intval($_POST['amount']); $NoMatch = ($rand1/65 + $rand2/65 + $rand3/65)*$amount; } should work PHP is smart (or stupid) enough to figure that out. The problem still remains that $NoMatch does not exist outside of the function. Quote Link to comment Share on other sites More sharing options...
Rustywolf Posted March 16, 2010 Share Posted March 16, 2010 Try declaring it public. Worse comes to worse, try delcaring it a session> Quote Link to comment Share on other sites More sharing options...
trq Posted March 16, 2010 Share Posted March 16, 2010 Try declaring it public. Worse comes to worse, try delcaring it a session> Its not within a class. All that the op needs to do is return it from his function. Quote Link to comment 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.