HTML Action attribute and PHP



Form ACTION attribute with PHP

In php when we submit the form the form where should be sent it depend on Action attribute.
The Action attribute is used to give link to another form.
We can send the form data to another PHP script page, or the same PHP page or any other form or script.

If we send the form data on same page or we can say send data to itself, then we have to define proper Action attribute for it.

Here we have two way for sending data one is send data to itself and other one is send data to another PHP script.


lets start with first method send data to same page.

In previous php script firstexample.php write ACTION=”” attribute like:

<FORM name=”form1″ action=”firstexample.php” method=”GET”>

Here we set the action=”firstexample.php” means the data send to itself or we can say data send to same page when page loaded.

<html>
<head>
<title>My First HTML web page</title>
</head>
<body>

<FORM name="form1" action="firstexample.php" method="GET">

Name : <input type="text" name="name"><br>
Password : <input type="text" name="city"><br>
<input type="submit" name="Submit1" value="Login">

</FORM>

</body>
</html>

When run above script the page send textbox information to same page as we write the same page name in action attribute.

If we wan to handle the data which is submitted to form then we have to do some PHP programming.

PHP Example to get form data on same page.

<html>
<head>
<title>My First HTML web page</title>
<?php
echo $_GET["name"];
?>
</head>
<body>

<FORM name="form1" action="firstexample.php" method="GET">

Name : <input type="text" name="name"><br>
Password : <input type="text" name="city"><br>
<input type="submit" name="Submit1" value="Login">

</FORM>

</body>
</html>

The PHP script out put is:

HTML Action attribute and PHP
HTML Action attribute and PHP

Above PHP script we use GET method to send data and we write some PHP script in head tag for display name on web form.
we write here action=firstexample.php” same script name so we write PHP script on same page for handle submitted data.


Now lets learn how to send data to another PHP script page.

For send data to another PHP form, first create another php script file with result.php name.
Here we write PHP script code on result.php page. We send the data to result.php script.

write Action attribute like:

<FORM name=”form1″ action=”result.php” method=”GET”>

Example of send data to another form in php.

<html>
<head>
<title>
My First HTML web page
</title>
</head>
<body>

<FORM method="GET" action="result.php">

Name : <input type="text" name="name"><br>
Password : <input type="text" name="password"><br>
<input type="submit" name="Submit1" value="Login">

</FORM>

</body>
</html>

create result.php page with below php code:

<?php
echo "Your Name = ". $_GET["name"];
?>

When we run the script it should look like:

HTML Action attribute and PHP
HTML Action attribute and PHP

In above php example we enter username in form firstexample.php  and click login button then the username will displayed on second result.php page. In this example we set action=”result.php” in form tag.

 

Leave a Reply

Your email address will not be published. Required fields are marked *