Hi, I'm teaching myself PHP and am trying to make this really simple game of just who can guess a randomized number between 0-10 first, the user or the computer. Though I can't seem to get it to work, it just shows the text and form and when you click submit, seemingly nothing happens. Here's the code: <html> <head> </head> <body> Let's Play A Game! You have to guess the number 1-10 before the computer does, you go first: <?php $random = rand(0,10); $win = 0 do { ?> <form action="guessnumber.html" method="post"> Enter a number 0-10: <input name="number" type="text"> <input name="submit" type="submit"> <?php if (isset($_POST['submit'])) { $number = $_POST['number']; if ($number != $random) { $comp = rand(0,10); $win = ($comp == $random); echo "The computer choose: $comp"; } else { $mewin = 2; } } while ($win = 0); if ($win = 2) { echo "You Win!!"; } else { echo "The Computer Won " } ?> </body> </html> Any help please?
Seems to me you are a bit confused about the logic of the game in general. Let me show an example similar to yours, though I'll need to change some of the code to make it functional, and I'm just going to make it one round, since that's much simpler than making a continuous game like you're trying to do. If you want a more advanced example, let me know and I'll reply with one . <html> <head> </head> <body> Let's Play A Game! You have to guess the number 1-10 before the computer does, you go first: <?php $random = rand(0,10); $win = 0; ?> <form method="post"> Enter a number 0-10: <input name="number" type="text"> <input name="submit" type="submit"> <?php if (isset($_POST['submit'])) { $number = $_POST['number']; if ($number != $random) { echo "You lost! You guessed $number while the generated number was $random!"; } } else { echo "You won! The correct number was $random!" } ?> </body> </html> PHP:
Try this. Also some hosting packages don't parse php if your extension is .html <html> <head> </head> <body> <p>Let's Play A Game! You have to guess the number 1-10 before the computer does, you go first:</p> <?php $random = rand(0,10); $win = 0; ?> <form action="guessnumber.html" method="post"> Enter a number 0-10: <input name="number" id="number" type="text"> <input name="submit" type="submit"> <?php if (isset($_POST['submit'])) { $number = $_POST['number']; if ($number != $random) { $comp = rand(0,10); if($comp == $random){ $win = 2; } echo "The computer choose: $comp"; } else { $win = 1; } if ($win == 1) { echo "You Win!!"; } if($win == 2) { echo "The Computer Won " } } ?> </body> </html> Code (markup):
Also some hosting packages don't parse php if your extension is .html I have never heard that Every server have apache installed will works atmost independent to extension
I'm using godaddy servers and the php code doesn't seem to be working when I try to run the file with a .html extension. I think they said I have to do something in my .htaccess file (addHandler)
Just checking, but first thing I saw was the form sends it to guessnumber.html, change the file to .php and try again, or as you said use htaccess to change .php file to .html if that's what you really want.
hey godaddy is waste friends......., i have faced lot of problems.., regarding script execution... it is unfit i am very confident about this as i faced lot of troubles.
Hey thanks everyone for helping, I'm really trying to get the looping part down. I went back through the code and edited some stuff and came up with this below. However now it automatically outputs the form over and over rather than stopping and waiting for me to input a number. How do I get the screen to clear and just show the form once? <?php $random = rand(0,10); $win = 0; do { ?> <form action="guessnumber.php" method="post"> Enter a number 0-10: <input name="number" type="text"> <input name="submit" type="submit"> </form> <?php $number = $_POST['number']; if ($number == $random) { $win = 2; } else { $comp = rand(0,10); $win = ($comp == $random); echo "The computer choose: $comp"; } } while ($win == 0); if ($win = 2) { echo "You Win!!"; } else { echo "The Computer Won "; } ?> Thanks again so much.
Ok, so I found this page: http://bytes.com/topic/php/answers/...op-need-wait-till-submit-activated#post625749 and pretty much understand now that what I'm trying to do isn't exactly possible the way I'm doing it. I guess the elegant way of doing it would be with AJAX?
Godaddy is only suited for Domain registration Better go with HostGator or Dreamhost (I love DH's Supoport)
Depends how server is configured. I far as i know, by default .html is .html and .php is .php. you can play with htaccess to solve this issue ...
Yeah htaccess just help to change the links that appear And they wont play with SHELL/Binary Packages or PHP inside that