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.

Related Posts:

  • Sorting PHP Array values There may be times when you want to sort the values inside of an array. For example, suppose your array values are not in alphabetical order. Like this one: $full_name = array(); $full_name["Roger"] = "Wate… Read More
  • How to Set up a PHP Array First you type out what you want your array to be called ($Order_Number, in the array above) and, after an equals sign, you type this: array( ); So setting up an array just involves typing the word array followed by a p… Read More
  • PHP Functions and Arguments Arguments Functions can be handed variables, so that you can do something with what's inside of them. You pass the variable over to your functions by typing them inside of the round brackets of the function name… Read More
  • PHP Functions What is a Function? A function is just a segment of code, separate from the rest of your code. You separate it because it's nice and handy, and you want to use it not once but over and over. It's a chunk of code that … Read More
  • Geting Values from Arrays Here's an example for you to try: <?php $seasons = array("Autumn", "Winter", "Spring", "Summer"); print $seasons[0]; ?> The array is the same one we set up before To get at what is inside of an array, just … Read More

0 comments:

Post a Comment