Ok, so you can get information from a variable and show it to the player with
echo, but how do you get information from the player? After all, this is a guessing game, so you need some way to collect their guesses!
You're going to use a combination of HTML and PHP to do this. The HTML you'll be using is going to be new, even if you've already done the HTML Sushi Cards, since they don't use many forms and your PHP programs will probably use a lot of them. You can see them on almost every website you use the internet and the code for them is pretty easy!
<form method="get"> Your guess: <input type="text" name="guess"/> </form>
A HTML form is a simple idea: controls—like text boxes, drop-down menus, check boxes or buttons—are used to collect information from users and send that information to your PHP program. Sometimes, responses are sent to the users. You'll be sending responses in your games.
Now, time to add a form to your page! So you can use PHP variables and other code in creating your form, you're going to use
echostatements to create it, instead of just typing the HTML into the file. So, add the following below your two existing
echo "<form method="get">"; echo "Your guess: <input type="text" name="guess"/>"; echo "</form>";
Run this code and see what happens!
Well, that didn't quite work right, did it? Any idea why? It's because of the double quotes in
<form method="get">. PHP isn't smart enough to recognise that they are part of the HTML tag; it reads them as the end of the string of text that
echois trying to insert into the page. After that, it's looking for a semicolon (
;) but instead it finds
gand gets very confused! This kind of problem can come up quite often either because you've forgotten which kind of quotes to use or you're copying from another of your programs and used quotes differently there. Luckily, it's very easy to fix. Just put a backslash (
\) in front of your all your double quotes (don't forget the ones in the input tag!) like this, to escape the normal rule of ending the string!
echo "<form method=\"get\">";
- Run the code again! Now you've got somewhere for your user to put their text! Type something in and press the enter key. Watch the page URL in the browser and notice what changes!