php_novice2007 Posted March 9, 2007 Share Posted March 9, 2007 Hi all, I have just started learning php 2 hours ago and I'm stuck already! I'm learning with the book "Professional PHP Programming", its the one written by 5 authors. I have typed in one of their first programs into Notepad and it doesn't work when I try to run it The code is: <html><body> <Form> Whats your name?<br> <input type=text name=usename><br><br> <input type=submit value="submit name"> </Form> <br><br> How are you today <?php echo($usename); ?> </body></html> When I try to open the page in IE, I get the notice "Notice: Undefined variable: usename in I:\Cathy\webpages\index.php on line 23" And according to the book I should get whatever I typed in the text box out but I don't... Is there something really obvious I'm missing here? Thanks for helping Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/ Share on other sites More sharing options...
interpim Posted March 9, 2007 Share Posted March 9, 2007 your calling echo like a function with $usename as a variable in that function... do it like so... echo $usename; Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203085 Share on other sites More sharing options...
Archadian Posted March 9, 2007 Share Posted March 9, 2007 >.> that doesn't explain why he/she is getting the error. try this. your <form> is supposed to be: <form action="yourpage.php" method="POST"> and the undefined Username variable: echo $_POST['username']; or: $username = $_POST['username']; //defines $username echo $username; //correctly defined variable and prints out $username you use $_POST because of the METHOD in your <form> and the username inside $_POST because the name of your form where you type the username is name="username" I hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203087 Share on other sites More sharing options...
interpim Posted March 9, 2007 Share Posted March 9, 2007 it will work in the same page.. the echo is written wrong, but otherwise the code does work Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203088 Share on other sites More sharing options...
Archadian Posted March 9, 2007 Share Posted March 9, 2007 he wasn't defining $username to begin with, i recommend PHP Bible 2nd Edition if you are wanting to learn PHP, its a good starter manual also if you want to include the PHP part in with the form on the same page read up on $_SERVER['PHP_SELF'] global variable Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203091 Share on other sites More sharing options...
interpim Posted March 9, 2007 Share Posted March 9, 2007 but Archadian does have a point... it is best to run through a seperate script instead of on the same file... to write the program you have, you would need 2 seperate files... one lets call name_entry.php then another called name_hello.php name_entry.php will have the following code... <Form action='name_hello.php' method='post'> Whats your name? <input type=text name=usename> <input type=submit value="submit name"> </Form> Next page will actually parse the input from that page and display the result... <?php $name=$_POST['usename']; //$_POST[] basically pulls the value from the input named inside the brackets, and now you are assigning that value to the variable $name echo "How are you today $usename"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203094 Share on other sites More sharing options...
Archadian Posted March 9, 2007 Share Posted March 9, 2007 2 pages for a form is good, it separates the code but $_SERVER['PHP_SELF'] runs the script again and then you can use something like this: if (isset($_POST['username'])) { // your code to process the form info here } isset() checks to see if the variable is actually set to anything before the code in the { } is ran. If its not then the if() returns false and does nothing, which is good when someone loads the form for the first time. Hopefully this isn't too confusing Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203098 Share on other sites More sharing options...
php_novice2007 Posted March 9, 2007 Author Share Posted March 9, 2007 Wow, so many replies so quickly! Thanks guys!! well... the first thing I tried was just getting rid of the brackets around my echo command, so the only thing I changed from the code I had in my first post was the echo had no brackets.. But it doesn't work I still got the same notice "Notice: Undefined variable: usename in I:\Cathy\webpages\index.php on line 25" Next I tried just changing two lines in my code so that it reads <Form> Whats your name?<br> <input type=text name=usename><br><br> <input type=submit value="submit name"> </Form> <br><br> How are you today <?php $usename = $_POST['usename']; // defines $usename echo $usename; ?> And this doesn't work either.. I think I must have misunderstood the code.. The third way I tried was adding in action="yourpage.php" method="POST" into my FORM brackets, and then having echo $_POST['usename'] and that worked Whats wrong with my echo command? Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203326 Share on other sites More sharing options...
willpower Posted March 9, 2007 Share Posted March 9, 2007 Learning PHP eh...good on you. Ok heres my small input. Forget the books...as you never really learn real life examples. a couple of years ago I was in exactly the same position. I have 2 premises to work from. 1) If it involves a computer...then it CAN be done. 2) If i can articulate my question...then the teaching I need is on this forum. Live your programming life by these principles....and you'll go far. Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203340 Share on other sites More sharing options...
magnetica Posted March 9, 2007 Share Posted March 9, 2007 Are you running this on a hosting service or on a local server or just through internet explorer... Because you need a WAMP server to run PHP Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203344 Share on other sites More sharing options...
php_novice2007 Posted March 9, 2007 Author Share Posted March 9, 2007 I'm using a local server, Apache. I read another tutorial, downloaded that, downloaded php, and then configure things according to the instructions and now I'm up to writing my first php page My plan is to learn php, and then learn MySQL. I'll be given a database with time and the GPS position of a tracker, and I have to write a website to plot the positions onto Google map. Oh and allow people with passwords to change the database online. Yep, I've just described my major project for the year. All this must be done by September this year. I am so dead. Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203970 Share on other sites More sharing options...
Glyde Posted March 10, 2007 Share Posted March 10, 2007 Reason: by default, browsers go to the GET method. Instead of posting data, it would have gone to yourpage.php?usename=test, which means he would have had to access it via $_GET. Overall, the book is somewhat bad because it relies on having register_globals on. Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203973 Share on other sites More sharing options...
per1os Posted March 10, 2007 Share Posted March 10, 2007 Your greatest resource is php.net They have every single function you could imagine. As for the books, especially if the book tells you to just call $username without doing $username = $_POST['username']; You know that book is a piece of junk. I think you wasted your money on a book on what could of been found easily on www.php.net --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203980 Share on other sites More sharing options...
tauchai83 Posted March 10, 2007 Share Posted March 10, 2007 LOL.. i'm agree php.net manual is the fastest way to learn because they come along with coding which help you understand better. But some book is good as well which have a lot of example and CD bundled with it, like the PHP book published by Sam Publishing(if not mistaken). Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-203988 Share on other sites More sharing options...
php_novice2007 Posted March 10, 2007 Author Share Posted March 10, 2007 Haha its ok, I got this book from the library I've researved about 5 other books on PHP from the library as well, waiting for them to come... Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-204095 Share on other sites More sharing options...
chronister Posted March 10, 2007 Share Posted March 10, 2007 Here is my tidbit for quick help.. If you are using firefox create a bookmark with the following url http://php.net/manual-lookup.php?pattern=%s Give it a nice simple keyword, and then anytime you need to find something in the manual just type your keyword with a space and then your search term. e.g. I use the keyword p for mine sooo...... when I type p fopen in the address bar, it takes me right to the page for fopen. if I type p oepn it takes me to a results page with all options for that typo. On to your issue... I routinely use only 1 page for form and processing with no problems. Here is how I do it. <?php if(!isset($_POST['Submit'])){ /* if the $_POST['Submit'] var does not exist, then the form has not been submitted, so we need to show the form */ ?> <form method="post" action="<?=$_SERVER['PHP_SELF'] ?>" > Whats your name? <input type="text" name="usename"> <input type="submit" value="submit name" name="Submit"> </form> <?php }else{ // it does exist so we need to process the form. ?> How are you today <?php $usename = $_POST['usename']; // defines $usename echo $usename; }; ?> This may look a little confusing. I prefer to keep the php and html elements seperate. I see people who use echo " loads of messy escaped html code here" and I just don't like that. I like to keep my html as html and the php as php, not html echoed out of php Basically the code above is saying ... if the submit variable is not set, then show the form else process the form. I like doing this for short forms and such. If the processing is more than 50-80 lines, then I would use a separate page, but this works very well. Quote Link to comment https://forums.phpfreaks.com/topic/41887-my-first-php-page/#findComment-204132 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.