Home Tutorial Php Phpbeginning PHP Form Part-5

 
 

Share on Google+Share on Google+
PHP Form Part-5
Posted on: February 18, 2010 at 12:00 AM
Advertisement
In the previous parts of this tutorial, we covered form attributes, input type attributes and Submit button attributes. Now, this is the time to use all these properties into one form and gets the information that we are inserting into the textbox.

Topic : HTML FORM

Part - 5

In the previous parts of this tutorial, we covered form attributes, input type attributes and Submit button attributes. 

Now, this is the time to use all these properties into one form and gets the information that we are inserting into the textbox.  The action attributes tells the user where the information is being sent and the method attributes tells you how the form information is going to display in the browser.

In the previous tutorial, if you noticed we used name attribute in the input type. This attribute will help to get the information that is fill out into the text box. Look below how to mentioned  name attribute in the input type :

<input type="text" name="userid" value="username" size="30" />

Now, look at the above coding after input type = "text" we used the name attribute and given the name  userid and this name will be used in the Php Script. Let's see the syntax how to return information from the html form.

$_POST['name_attribute'];

And, then we will assign this into one variable.

$anyVariable = $_POST['name_attribute'];

Now, use this syntax into our php form to get the result. Let's see the following code :

Page : 1 (Loginpage.php)

<html>

<head>

<title>Login Page</title>

</head>

<body>

<form action="detail_page.php" method="post">

Username : <input type="text" name="userid" value="" size="30" /><br/><br/>

Password : <input type="password" name="pass" value="" size="30" /><br/><br/>

<input type="submit" name="submit" value="Submit Form" />

</form>

</body>

</html>

The output of the above code is : 

In the above code, We designed a login page.

Let's display the information into the next page with the help of php. 

Page : 2 (detailedpage.php)

<?php

if(isset($_POST['submit']))

{

$userid = $_POST['userid'];

$password = $_POST['pass'];

if($userid == 'admin' && $password == 'admin')

{

echo "Welcome , ".$userid."and your password is ".$password."!"."<br/>";

}

else

{

echo "You are not a member of this site!";

}

}

?>

 

Let's see how this code works?

Here, we used the inbuilt function called $_POST[] to get POST data from a form. We can also use GET method to get the information from the form. Just look the syntax :

$_GET['userid'];

Here, we start with ($) and an underscore sign (_). After underscore sign we used the method POST with the pair of square bracket and within square bracket, we type the Name of HTML form that is userid and close the with the semi-colon. It's up to you which method you want to use.

$_POST['userid'];

So, you can get the value from the text box whatever the users inserted and then assign into one variable that is :

$userid = $_POST['userid'];

Whatever the VALUE was for your HTML element is what gets returned. You can then assign this to a variable:

$username = $_POST['username'];

The next step is to return the value entered by the user and print it to next page. For this we need to use a little bit of logic to see what is inside of the variable. This example will change your PHP to this :

$userid = $_POST['userid'];

$password = $_POST['pass'];

if($userid == 'admin' && $password == 'admin'){

echo "Welcome , ".$userid."and your password is ".$password."!"."<br/>";

}

else {

echo "You are not a member of this site!";

}

Here, we check the condition, if the user entered the username or password correct then the message will displayed Welcome admin and your password is admin.

If incase the user entered the wrong username or password. Then the message on the screen will be You are not a member of this site!.

Before checking the condition we should checked weather the submit button is set or not. If its not set then it shouldn't print anything or it will not return anything.

In the next part of this tutorial, we will learn how to deal with radio button in php form.

 

Advertisement

Related Tags for PHP Form Part-5:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 18, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com