Web Business Web Business

Paos Build Web Business Et Tag Mp3 Format Web Business PHP Tutorial for Beginners - 国外php教程 - 站长中国

Paos Build Web Business Et Tag Mp3 Format Web Business



Web Format r Format n Web search&searchu Business t Tag Mp3 &q Tag o Format ; Build searchm Tag t Web e Mp3 C Paos I Tag Build agsearchN Format t1 Business asearchC%C Tag %search9 Tag B Build % Paos D1BB%B Business %B Business Tag s Paos arc%searchear Business hsearch searcho Web m Business t2%C8%CB%B9%B7%BD%BB%BB%B6wsearchbr Build n Build i Format g Business esearchssearch.http%3A%2F%2Fweb.renxingbense.org%2Frsearch%2 Paos e Web rc%C8%CB%B9%B7%BD%BB%BB%B6MN& Paos u1t Format & Business usearchtsearch Business ;



 



HTML Forms and PHP


In our examples so far, we have set variables and then used them all in the same code. This doesn't make much sense because in those instances, we could have just hard-coded the values instead of using variables.


Let's get some real mileage by creating HTML forms to gather user input, turning that input into variables, and then doing various things with the information that we just collected.


No sense in sitting around waiting - let's go ahead and make a Web page that collects your favorite dirty word and displays it on another page that tells you what a pervert you are. All of this gives a page that looks a lot like this.


First, we make the form page, which is regular HTML with a form in it. I'm calling mine "badwords_form.html," but call yours whatever you like. (If you want a good primer on HTML forms, read Jay's How To Add HTML Forms to Your Site tutorial.)











1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.



<html>
<head>
<title>My Form</title>
</head>
<body>

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

My name is:
<br> <input type="text" name="YourName">

My favorite dirty word is:
<br /><input type="text" name="FavoriteWord">


<input type="submit" name="submit" value="That's Right!">
</form>

</body>
</html>


This is a regular HTML form. The important pieces are as follows:


Line 7: the HTML that reads action="bad_words.php" tells the browser which PHP document will process the results of the form. That is to say, in a minute you'll create a document called "bad_words.php" which will be the little engine that makes the result page happen. (We'll get to the method=post part later on.)


Line 10: input type="text" determines that the form element which we want here is "text" or a text box (we could also have a radio button, check box, etc.); name="YourName" determines that whatever the user types into the text box will become a variable that we have called "YourName." This is what ties together forms and variables - each form field can set a variable to be used however you want.


Line 13: here you have another text input that sets a variable called "FavoriteWord" which is the user's favorite dirty word.


Line 16, 17: This code makes a submit button with the text "That's Right!" and ends the form.


So this form will collect the unassuming user's name and favorite bad word, but now what do we do with it? Let's take the variables she set and echo them back in another context on another page.


On line 7 of the HTML above, we told the form to head on over to bad_words.php once the submit button was hit. This is what bad_words.php looks like:











1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.



<html>
<head>
<title>Perv!</title>
</head>

<?php
// Capture the values posted to this php program from the text fields
// which were named 'YourName' and 'FavoriteWord' respectively

$YourName = $_REQUEST['YourName'] ;
$FavoriteWord = $_REQUEST['FavoriteWord'] ;
?>

<body bgcolor="#FFFFFF" text="#000000">
<p>

Hi <?php print $YourName; ?>

<p>

You like the word <b> <?php print $FavoriteWord; ?> !?! </b>

<p>You oughta be ashamed of yourself!

</body>

</html>


See how this form passed a variable along from the form page to the PHP file? You have not seen the last of this.




 



Get versus Post


So far, we've used the "Post" method of passing form data as opposed to the other method, "Get." This is the part of the form code that reads  form action="bad_words.php" method=post¢.


The difference between these two is that the "post" method transparently passes along all the information the page has gathered, whereas the "get" method will pass all that info along as part of the URL (in the form above, this would look like: webmonkey_article/bad_words.php?YourName=bob&FavoriteWord=crikey%21&submit=Enter+My+Data%21 - see how the info the user entered about his name and his favorite word get added to the URL?)



Arrays


One of your best tools now that you've mastered variables - you have, haven't you? - are arrays.


Arrays give you the ability to store not just one value inside a variable, but a whole bunch of values in a single variable.


If I wanted to catalog all of the animals in my house, I could set each one as a regular variable. I've got two dogs, Phoebe and Ruby, and a squirrel that died in the attic last year, whom we'll call Rotty. Setting each one as a variable looks like this:



$dog1 = "Phoebe";
$dog2 = "Ruby";
$squirrel1 = "Rotty";

But an array will let us store all these inside one single variable, which I'll call $critters. Each element of the variable has its own "key" that is used to access that part of the array, which can either be a string of letters or numbers.


Let me explain the "key" concept another way: If we're storing three different values inside one variable (like storing Phoebe, Ruby, and Rotty inside $critters), we need some way to be able to suck out any individual part of the array to use it. An array will automatically number each element that comprises it, so the key can be element 1, element 2, and element 3. Or, as we'll see later on, we can name each part of the array with text. In this case I could make the keys "fat dog," "skinny dog," and "squirrel" and use those to identify each array member.


Let's make a simple array and then use it. The easiest way to create an array is to use the array() function, which assigns a bunch of values to your array at once and looks like this:



$critters = array ( "Phoebe", "Ruby", "Rotty" );

This stores all my animal names into one variable ($critters) in an array, and automatically assigns a numbered "key" to each element starting in order and giving the first element the number 0. So Phoebe is element [0], Ruby is [1], Rotty is [2], etc. I make up the name of the array myself (here it's $critters).


You can now get at any of the array elements by referring to the variable followed by the element number in square brackets: $critters[0], for example. Here it is in action:



<?php

print "$critters[2]";

 ?>

This will simply print the third element in the array, which is Rotty (don't forget that array numbers start at 0, so $critters[2] is third after $critters[0] and $critters[1]).


There's another way to set an array, or even to add to an existing array, by setting each element individually:



$critters[] = "Phoebe";
$critters[] = "Ruby";
$critters[] = "Rotty";

This'll have the same effect as using the array() function, giving the first element the key [0] and so on. But wait! I forgot about Opie the cat. Hmmm. Regardless of how we made the array in the first place, I can easily add Opie like this:



$critters[] = "Opie";

PHP is smart enough to count the number of elements and give Opie the next available one, which in this case (after Phoebe, Ruby, and Rotty) is [3].


To recap this concept, I can set an array to include the animals in my house either this way:



$critters[] = "Phoebe";
$critters[] = "Ruby";
$critters[] = "Rotty";
$critters[] = "Opie";

Or this way:



$critters = array ( "Phoebe", "Ruby", "Rotty", "Opie" );

Both will be indexed in the computer brain with the values:



$critters[0] = "Phoebe";
$critters[1] = "Ruby";
$critters[2] = "Rotty";
$critters[3] = "Opie";

And in both cases, you could get at any element in the array by describing its number ...



<?php

print "$critters[3]";

 ?>

... which would print the string Opie to the window of your browser.


Arrays can be made to do all kinds of things, like being incremented by number, sorted in alphabetical order, printed by different types of categorization, and many more.




 



Associative Arrays


Ready to get more complicated? The associative array indexes the contained elements not by numbers, but by names that you determine. Inside the array() function, you set up pairs where you name the key and its value using the combo of the "=" and the ">", like: key=>"value". Here's what it looks like in action:



$PhoebeDog = array (
name=>"Phoebe",
description=>"fat dog",
color=>"grey and white",
age=>7
);

Here we're telling the array to create the keys "name," "description," "color," and "age"; and we give each of those keys a value (name is "Phoebe", description is "fat dog," and so on).


We can get at any part of the array through the "key" names that we set, for example:



print $PhoebeDog[color];

will give us grey and white. We can also set each key individually, like so:



$animals[name] = "Phoebe";
$animals[description] = "fat dog";
$animals[color] = "grey and white";
$animals[age] = 7;

Finally, let's make it hurt. We're going to get some serious power out of this arrays business by creating a "multi-dimensional" array. A multi-dimensional array is an array (say the animals in my house) that is made up of other arrays (for each animal, an array that contains the critter's name, color, description, and age).


We make multi-dimensional arrays by creating one array:



$animals = array
(
);

...and then we fill that array with an array of animals in which we've defined the keys, like this:



$animals = array (
array ( name=>"Phoebe",
type=>"dog",
color=>"grey and white",
age=>7 ),
array ( name=>"Ruby",
type=>"dog",
color=>"brown and white",
age=>7 ),
array ( name=>"Rotty",
type=>"squirrel",
color=>"grey",
age=>2 ),
array ( name=>"Opie",
type=>"cat",
color=>"grey tabby",
age=>5 )
);

To use this now, we can get any part of the information contained in there by naming the overall array ($animals), naming the number of the sub-array that we want to find out about (Phoebe is [0], Ruby is [1], etc.) and then naming the key for the attribute we want to get at (name, type, color, or age).


To find out the age of the cat, we'd write:



print $animals[3][age];

Here's what it all looks like together. This is all in one page, but remember that you can set arrays in one place (in code or in form fields from another page or in a database, say) and get at the info contained within from somewhere else. Here I'm putting it all together on one page so you can see it all at once.



<html>
<head>
<title>Pet Arrays</title>
</head>
<body>

<?php

$animals = array (
array ( "name" => "Phoebe",
"type" => "dog",
"color" => "grey and white",
"age" => 7 ),
array ( "name" => "Ruby",
"type" => "dog",
"color" => "brown and white",
"age" =>7 ),
array ( "name" => "Rotty",
"type" => "squirrel",
"color" => "grey",
"age" =>2 ),
array ( "name" => "Opie", jPaos Build Web Business Et Tag Mp3 Format Web Business PHP Tutorial for Beginners - 国外php教程 - 站长中国r k Web Business yPaos Build Web Business Et Tag Mp3 Format Web Business PHP Tutorial for Beginners - 国外php教程 - 站长中国t z Http%3A%2F%2Fweb.renxingbense.org%2F Web Business Web Business