Sunday, June 23, 2013

php form example - php forms

php form example
What is an form in php? actually the forms are in html not in php but when we put a form in html the php handle all the operations about that form
For example you want to enter your facebook account what you should do?
Of course you must enter your email and your password into the form and hit Log in, but who tell's facebook that you are the right person, well it's the php language, you want to know how? just keep reading
In Our level we will make a form and receive values from it and them them on the screen.
Let's do this!
<form action="" method="post" >
<input name="user" type="text" />
<input name="password" type="password" />
<input name="submit" type="submit" value="Log in"  />
 </form>
This is the form and you will see this on the screen
php form example

If we write it like this
<form action="" method="post" >
<input name="user" type="text" value="usernam" />
<input name="password" type="password" value="password" />
<input name="submit" type="submit" value="Log in"  />
 </form>
we will see this 
php form example

So the difference as you see is the "username" and the "........." and we add this to let the user know where to put his username and password, Now let's write the php code

<?php
$username= $_POST['user']
$password= $_POST['password']
echo "My username is $username and my password is $password";
?>
<form action="<?php echo $PHP_SELF; ?>" method="post" >
<input name="user" type="text" value="usernam" />
<input name="password" type="password" value="password" />
<input name="submit" type="submit" value="Log in"  />
 </form>

In the first line we ask the php to get the value from the form which named user
In the second line we ask the php to get the value from the form which named password
In the third line we ask the php to print the expression with the values of the two variables $username and $password
Always write this <?php echo $PHP_SELF; ?> in the action if you are writing the php code in the same file of the form
Always use the post method because it's safe and we will talk about the difference between post and get later
After this big explanation let's see the result 
php form example
This was an php form example I hope you learn something useful today
See you next time


Share This!



No comments :

Post a Comment