Get latest news and Web Designing tutoria

Friday, 29 May 2015

The HTML ACTION attribute and PHP

You don't have to submit your form data to the same PHP page, as we've been doing. You can send it to an entirely different PHP page. To see how it works, try this:
Create the following page, and call it basicForm2.php. This is your HTML. Notice the ACTION attribue.
<html>
<head>
<title>A BASIC HTML FORM</title>
</head>

<body>
<Form name ="form1" Method ="POST" Action ="submitForm.php">
<INPUT TYPE = "TEXT" VALUE ="username" Name ="username">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Login">
</FORM>
</body>
</html>

Now create the following page, and call it submitForm.php:
<?PHP
$username = $_POST['username'];
if ($username == "letmein") {
print ("Welcome back, friend!");
}
else {
print ("You're not a member of this site");
}
?>
In the PHP script above, notice how there's no HTML tags. And we've left out the code that checks if the Submit button was clicked. That's because there's no PHP left in the first page. The code only gets executed IF the Submit is clicked.
Posting form data to a different PHP script is a way to keep the HTML and PHP separate. But there is a problem with it, which you will have noticed: the script gets executed on a new page. That means your form will disappear!
We'll keep the PHP and HTML together. But there will be times when you do want to send form data to a different PHP page, as we'll see in later sections.

0 comments:

Post a Comment