Jump to content

my first php page


php_novice2007

Recommended Posts

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 :)

 

Link to comment
Share on other sites

>.> 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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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";
?>

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

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 :D

 

Whats wrong with my echo command? :(

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.