andypeat Posted February 8, 2005 Posted February 8, 2005 I've started to look at the basics of PHP and have got stuck at the first book I found. I have installed PHP onto my iBook so I can test the scripts on the train. At first it all seemed simple, I started with something I found on the web: ><html> <head> <title>PHP Test</title> </head> <body> <p><h3>Hi <?php echo $_POST['name']; ?>.</h3> You are <?php echo $_POST['age']; ?> years old.</p> </body> </html> This worked fine. So I thought I would progress to a book. I then tried the following: ><html> <head> <title>Hi User</title> </head> <body> <h1>Hi User</h1> <h3>PHP program that receives a value from "whatsName"</h3> <? print "<h3>Hi there,<? $userName! </h3>"; ?> </body> </html> but this failed. I saw the rest of the text but it didn't show me the UserName. There was an html file linked to this to input the UserName. I've basically copied the code from the book. Can anyone tell me why this would happen? Is it my version of PHP (which is 5)? thanks in advance. now it's time to sleep. Andy Quote
btrfld Posted February 8, 2005 Posted February 8, 2005 Hi Andy. The problem is the extra opening tag in the print statement: change print "<h3>Hi there,<? $userName! </h3>"; to print "<h3>Hi there, $userName! </h3>"; and it works fine. Good start. Keep it up, and have fun. PHP is great! Quote
stevevan Posted February 9, 2005 Posted February 9, 2005 PHP is a pretty powerful language to make website creation and maintenance easier. Books and practice are how most of us learned it! I've dabbled a bit myself and really enjoy it! Good luck and if you have any questions, ask away here in the forums. We have some really good "guru's" out there! Quote
TCH-Don Posted February 9, 2005 Posted February 9, 2005 One thing I would suggest is to stick with the long php tag <?php as the short tag <? may be a problem when trying to use XML php can be very helpful, even if you just use includes to make your site modular. And better when you pass variable data to the included file. Quote
borfast Posted February 9, 2005 Posted February 9, 2005 I completely agree with Don about the short vs. normal open tags. Use <?php instead of just <? Jim already said what's wrong with your script, so I'll just add two suggestions: - search google for "php tutorial", you'll find TONS of reading; - search amazon books for "PHP" (on the programming section), on the search results page select the "sort by average consumer rating" option and then start reading the reviews for the ones with good ratings (4 stars and above). Hope this helps Quote
andypeat Posted February 9, 2005 Author Posted February 9, 2005 I think there must be something wrong with my PHP configuration. I have been trying this code ><html> <head> <title>Hi User</title> </head> <body> <h1>Hi User</h1> <h3>PHP program that receives a value from "whatsName"</h3> <?php print "<h3>Hi there, $userName</h3>"; ?> </body> </html> but it still doesn't show the value of userName. I have a feeling I have two versions of PHP installed - would this cause this type of problem? and how can I uninstall PHP from a mac? thanks. Andy Quote
btrfld Posted February 9, 2005 Posted February 9, 2005 Your script doesn't show how $userName is being set. Can you tell us more? Quote
andypeat Posted February 9, 2005 Author Posted February 9, 2005 There is an html file that you enter your name: ><html> <head> <title>What's your name?</title> </head> <body> <h1>What's your name</h1> <h3>Writing a form for user input</h3> <form method = "get" action = "hiUser.php"> Please type your name: <input type = "text" name = "userName" value = ""> <br> <input type = "submit"> </form> </body> </html> I've also tried "POST" instead of "GET". I've just tried to reinstall PHP 5.0.3 but it's still the same. I hope there is a light somewhere, this tunnel is turning into a maze thanks. Andy Quote
Jaxx Posted February 9, 2005 Posted February 9, 2005 First, change the form method back to post. Next, assuming the two code snippets you've shared is the only code, you're missing the assignment of $userName from the $_POST variable. It's not an automatic assignment. In the php block in your original code you need a statement like >$userName = $_POST['userName']; before $userName will be defined. Cheers, Chris Quote
owatagal Posted February 9, 2005 Posted February 9, 2005 One thing to keep in mind as you're learning, especially if you happen to pick up any older books/tutorials: prior to PHP 4.2.something, register_globals was automatically set to 'on', which allowed you to process variables without defining them. Because it's a security risk, register_globals was then automatically set to off, which is where it is now for most sites. This means that any time you process a form, you have to define your variables. So for every variable on your form, at the top of the results page you need something like: $my_variable_one = $_POST['my_variable_one']; $my_second_var = $_POST['my_second_var']; (which is what Jaxx is saying) Then later on you can call echo $my_second_var; And it will work -- but only because you defined it early on. You're going to see a lot of tutorials that don't define the variables, because they were written prior to the 4.2.whatever switch, but if you get into the habit of always defining them, you should be ok. And POST is almost always better than GET, but if you have to use GET for some reason, you define your variable as $my_variable = $_GET['my_variable']; This threw me when I was first learning--if you google "php register global", you'll get a bunch of sites explaining the switch and the security and what not. Better background than I can explain, certainly! Quote
andypeat Posted February 9, 2005 Author Posted February 9, 2005 Thank you to all for helping. Okay, so I think I'm solving something here but it is leaving me with a big question about the book I'm refering to. To give you a better account of my problem, this is my first html file that gives the userName: ----- whatsName.html ----- <html> <head> <title>What's your name?</title> </head> <body> <h1>What's your name</h1> <h3>Writing a form for user input</h3> <form method = "POST" action = "hiUser.php"> Please type your name: <input type = "text" name = "userName" value = ""> <br> <input type = "submit"> </form> </body> </html> ----- end ----- This is the php file that it looks for (this one doesn't work): ----- hiUser.php ----- <html> <head> <title>Hi User</title> </head> <body> <h1>Hi User</h1> <h3>PHP program that receives a value from "whatsName"</h3> <?php print "$userName = $_POST['userName']"; ?> </body> </html> ----- end ----- This one does work: ---- hiUser.php ----- <html> <head> <title>Hi User</title> </head> <body> <h1>Hi User</h1> <h3>PHP program that receives a value from "whatsName"</h3> <?php print $userName = $_POST['userName']; ?> </body> </html> ----- end ----- Look, no quotes. So does this mean that the book I have refered to is wrong? thanks. Andy Quote
btrfld Posted February 9, 2005 Posted February 9, 2005 I don't know about the book (which book is it, by the way?), but I can tell you this from my experience: Inside double quotes (") variables are evaluated. So if you had a script <?php $userName = "Fred"; print "Hello $userName"; ?> the resulting output would be Hello Fred. However. The evaluation of variables inside quotes seems to stop at simple (or scalar) variables. It won't evaluate $_POST['userName'], for instance. That's why taking the quotes away made it work. The script would work if it said <?php $userName = $_POST['userName']; print "Hello $userName"; ?> Does that help? Quote
andypeat Posted February 9, 2005 Author Posted February 9, 2005 I can understand what you are saying, but when I try this I just get a blank screen. When I look at the source code given by the browser it looks as though everything between the <body> tag has been deleted. ><html> <head> <title>Hi User</title> </head> <body> <h1>Hi User</h1> <h3>PHP program that receives a value from "whatsName"</h3> <?php $userName = $_POST['userName'] print "$userName"; ?> </body> </html> What does this mean? Quote
btrfld Posted February 10, 2005 Posted February 10, 2005 There is a syntax error: $userName = $_POST['userName'] should have a semicolon after it. Maybe that's causing it to fail? Quote
andypeat Posted February 10, 2005 Author Posted February 10, 2005 You are a genius! "It's alive" Thanks for that, I think I've learnt quite a bit about php in the last 24hrs. thanks. Andy Quote
btrfld Posted February 10, 2005 Posted February 10, 2005 Excellent! I'm glad it's working. You're off and running. Have Fun. Quote
andypeat Posted February 11, 2005 Author Posted February 11, 2005 I've realised that the book does say to include the >$name = $_POST['name']; at the beginning of the script, but they only say it in a small box because they try to get you to turn on some setting on the server to recognise the items automatically. Thebook is called something like; PHP5 / MySQL for absolute beginners - that's me so onto the next page Andy Quote
TCH-Bruce Posted February 11, 2005 Posted February 11, 2005 Thebook is called something like;PHP5 / MySQL for absolute beginners - that's me The server here are running PHP 4.x.x So there will be some differences. Next question will be when is TCH upgrading to PHP5? That is still undetermined as it could break scripts already running on the servers. Quote
stevevan Posted February 11, 2005 Posted February 11, 2005 Don't ya just hate those small semi-colons? They seem to ALWAYS find me somehow...usually just when I get to the point of starting to pull out my hair! Glad you got it working! Quote
andypeat Posted February 11, 2005 Author Posted February 11, 2005 "oh dear" "oh dear" So, When... Are the two (4 and 5) radically different? Quote
snipe Posted February 17, 2005 Posted February 17, 2005 "oh dear" "oh dear" So, When... Are the two (4 and 5) radically different? <{POST_SNAPBACK}> Well.. yes and no.... But getting into the specific details might be a little rough on you at this stage of your learning process. 90% of what you will learn about PHP4 will work for PHP5 - but that 10% could be a real pain. There is more info available here: http://www.php.net/manual/en/faq.migration5.php http://www.php.net/manual/en/migration5.php Quote
owatagal Posted February 17, 2005 Posted February 17, 2005 I've realised that the book does say to include the >$name = $_POST['name']; at the beginning of the script, but they only say it in a small box because they try to get you to turn on some setting on the server to recognise the items automatically. Thebook is called something like; PHP5 / MySQL for absolute beginners - that's me so onto the next page Andy <{POST_SNAPBACK}> They're probably trying to get you to turn register_globals on. Actually, that's the default setting at TCH. It is more convenient, but it means you have no idea where the information is coming from-- if you have a form with method=POST and a field called email, and on your results page you just call the variable $email, you can't be sure the information came from your form--it could have come from anywhere. Nothing can replace initializing and verifying your variable data (if your form has a drop down list with four options, you should ALWAYS check to make sure that the variable actually equals one of those four options--even if the variable comes through POST.) Never assume a variable is safe, whether register_globals are on or off. PHP.net has a good explanation of how register_globals affects things: http://uk2.php.net/manual/en/security.globals.php I'm a fan of register_globals off just because it gives you more control over where the variables are coming from--it's not a cure-all solution, but every bit of control is a good thing, I think. If you want to turn them off for your site (since TCH has them on by default) just add >php_flag register_globals off to your .htaccess file. And then while you're working your way through the book, remember to call all your variables with $_GET or $_POST -- less convenient, but better practice in the long run. Quote
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.