Pages

Saturday, March 30, 2013

php tutorial



      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


  
   Simple HTML Form


Insert the initial form tag. Since the action attribute dictates to which script the form data will go, you should give it an appropriate name (handle_form to correspond with this script — form.html) and the .php extension (since a PHP page will handle this form's data).

Begin the HTML form. We're using the fieldset and legend HTML tags because we like the way they make the HTML form look (they add a box around the form with a title at top). This isn't pertinent to the form itself, though.

Enter your information in the form below:
Add two text inputs. These are just simple text inputs, allowing the user to enter their name and email address. In case you are wondering, the extra space and slash at the end of each input's tag is valid XHTML. With standard HTML, these tags would conclude, for instance, with maxlength="40"> or maxlength="60"> instead.
Name:
     maxlength="40" />


   Email Address:
     maxlength="60" />
Add a pair of radio buttons. The radio buttons both have the same name, meaning that only one of the two can be selected. They have different values, though.
Gender: Male
   type="radio" name="gender" value="F" /> Female

Add a pull-down menu. The select tag starts the pull-down menu, and then each option tag will create another line in the list of choices.

Age:
  
      
      
      
  
Add a text box for comments. Textareas are different from text inputs; they are presented as a box, not as a single line. They allow for much more information to be typed and are useful for taking user comments

Comments:

Complete the form. The first tag closes the fieldset that was opened in Step 3. Then a submit button is created and centered using a div tag. Finally, the form is closed.



  


  



Complete the HTML page






Exercises for
PHP and MySQL: Programming with PHP
Lesson 2, Exercise 1


Try creating an HTML form in a text editor.




Step
Action
1
Open a text editor and begin a new HTML document. Include a comment indicating the file's name.
2
Insert the initial form tag. Make the form data go to a script called handle_form.php.
3
Using the fieldset and legend tags, include the following fields in the form:

1. name
name type text, size 20, maxlength 40
2. name
email type text, size 40, maxlength 60
3. name
gender type radio value F and M
4. name
age option value 0-29, 30-60, 60+

Add a text box for comments, using textarea name
comments, with 3 rows and 40 columns.

Click here to view the form layout.
4
Complete the form by closing the fieldset and form tags. Before closing the form, include a submit button and center it.
5
Complete the HTML page and save the file as form.html.
6
Upload the file to your Web server. Finally, use a Web browser to view the file.


Lesson 3, Exercise 2


Try handling an HTML form in a text editor.




Step
Action
1
Open a text editor and begin a new PHP document. Add the standard HTML code in the beginning.
2
Add the opening PHP tag including the file's name and number.
3
Create a shorthand version of the form data variables name, email, and comments.

[Hint: Assign the value of the special variable to a shorthand version of the form data variable.]
4
Use an echo() statement to print out the received name, email, and comments values. Click here to view the layout of the feedback.
5
Complete the HTML page and save the file as handle_form.php. Upload the file to your Web server.

[Note: Make sure that
form.html is available in the appropriate directory on your Web server. Click here to view the code, if you need to create a new file.]
6
First view the form.html file in your Web browser. Fill in all the required information.
7
Next, test the handle_form.php file in the Web browser. It should display information based on your input into the form.


Lesson 4, Exercise 3


Try changing a PHP script to adjust for Magic Quotes.




Step
Action
1
Test the form.html file in your Web browser.

[Note: Make sure that
form.html is available in the appropriate directory on your Web server. Click here to view the code, if you need to create a new file.]
2
Fill in all the required information. Make sure to add an apostrophe in the comments box.
3
Test the handle_form.php file in the Web browser. Note how the apostrophe in the comments section is interpreted.

[Note: Make sure that
handle_form.php is available in the appropriate directory on your Web server. Click here to view the code, if you need to create a new file.]
4
Next, open handle_form.php in your text editor.
5
Use the stripslashes() function in the first and third variable assignment lines.
6
Save the file as handle2_form.php and upload to your Web server.
7
Finally, test the file in your Web browser. Notice how the apostrophes are interpreted this time around.


Lesson 5, Exercise 4


Try using conditionals in a PHP script.




Step
Action
1
Open handle_form.php in your text editor. Click here to view the code, if you need to create a new file.
2
Add a conditional to create a variable called $gender before the echo() statement. The variable should either have a value based on the user input or have a value of NULL.
3
Use another conditional after the echo() statement to print a message based on the value of the variable $gender.
4
Save the file as handle2_form.php and upload to your Web server.
5
Open form.html in your text editor. Click here to view the code, if you need to create a new file.
6
Make the form data go to the script handle2_form.php.
7
Save the file as form2.html and upload to your Web server.
8
Finally, test the handle2_form.php file in your Web browser.


Lesson 6, Exercise 5


Try validating form data in PHP scripts.




Step
Action
1
Begin a new PHP document in a text editor.
2
Check if values are entered for form data variables $name, $email, and $comments.
3
Check whether or not a value for variable $gender is entered. If a value other than M or F is entered, assign the value of NULL to the variable.
4
Print out the received name, email, and comments values, if all tests have been passed. Click here to view the layout of the feedback.
5
Save the file as handle3_form.php and upload to your Web server.
6
Open form.html in your text editor. Click here to view the code, if you need to create a new file.
7
Make the form data go to the script handle3_form.php.
8
Save the file as form3.html and upload to your Web server.
9
Finally, test the handle3_form.php file in your Web browser. Try filling out the form to different levels of completion and examine the output.


Lesson 7, Exercise 6


Try using arrays in PHP scripts.




Step
Action
1
Open a text editor and begin a new PHP document.
2
Use the superglobal variable $_POST to create a shorthand version of the form data variables $name and $comments.
3
Use an echo() statement to print out the received name and comments values. Use the value of variable $email using the superglobal variable $_POST. Click here to view the layout of the feedback.
4
Save the file as handle4_form.php and upload to your Web server.
5
Open form.html in your text editor. Click here to view the code, if you need to create a new file.
6
Make the form data go to the script handle4_form.php.
7
Save the file as form4.html and upload to your Web server.
8
Finally, test the handle4_form.php file in your Web browser.


Lesson 8, Exercise 7


Try creating and accessing arrays in PHP scripts.




Step
Action
1
Open a text editor and begin a new PHP document. Add the standard HTML code and PHP tag including the file name.
2
Create arrays for months, days of the month, and years.

[Hint: The
range() function can be used to create an array containing numbers.]
3
Generate pull-down menus for the months, days, and years. Click here to view the layout of the calendar.
4
Complete the page and save the file as calendar.php.
5
Upload the file to your Web server and test it in your Web browser. Make sure to check the value ranges for each of the pull-down menus.


Lesson 9, Exercise 8


Try using multidimensional arrays in PHP scripts.




Step
Action
1
Create a new HTML document in your text editor.
2
Insert the initial form tag. Make the form data go to a script called handle_about.php using the post method.
3
Using the fieldset and legend tags, include the following field in the form:

name
name type text, size 20, maxlength 40
4
Create the following check boxes with the name interests and the following values:

1.
Music
2.
Movies
3.
Books
4.
Skiing
5.
Napping

Click here to view the form layout.
5
Complete the form by closing the fieldset and form tags. Before closing the form, include a submit button and center it.
6
Complete the HTML page and save the file as about.html.
7
Create a new PHP document in your text editor.
8
Validate if a name was entered for variable $name using the superglobal variable $_POST.
9
Using variable $interests and superglobal $_POST, validate the interests.
10
Use a conditional to print a message if name and interests are filled out properly. Click here to view the form layout.
11
Print out all the selected interests.
12
Using a final conditional, print an error message if either name or interests are not filled. Click here to view the form layout.
13
Complete the PHP and HTML tags. Save the file as handle_about.php.
14
Upload the about.html and handle_about.php files to your Web server and test it in your Web browser.


Lesson 10, Exercise 9


Try converting arrays to strings in PHP scripts.




Step
Action
1
Open handle_about.php in your text editor. Click here to view the code.

[Note: Make sure that
about.html is available in the appropriate directory on your Web server. Click here to view the code, if you need to create a new file.]
2
Delete the if-else clause used for defining the $interests variable. Use the $_POST superglobal variable and assign its value as a string of comma separated values to the $interests variable.
3
Remove the foreach loop and the echo() statement from the main conditional.
4
Change the first main conditional echo() statement. Click here to view the layout of the feedback.
5
Save the file as handle_about2.php.
6
Upload the file to your Web server and test in your Web browser.


Lesson 11, Exercise 10


Try sorting arrays in PHP scripts.




Step
Action
1
Open a text editor and begin a new PHP document. Add the standard HTML code and PHP tag including the file name.
2
Create a new array called $movies with the following keys and values.

1.
10 Casablanca
2.
9 To Kill a Mockingbird
3.
2 The English Patient
4.
8 Sideways
5.
7 Donnie Darko
3
Print the array in the original order. Click here to view the layout of the list.
4
Sort the array alphabetically by title and print the array again using the same layout as in Step 3.
5
Finally, sort the array in the descending order of the key and print the array again. Use the same layout as in Steps 3 and 4.
6
Complete the page and save the file as sorting.php.
7
Upload the file to your Web server and test it in your Web browser.


Lesson 12, Exercise 11


Try using
for and while loops in PHP scripts.




Step
Action
1
Open calendar.php in your text editor. Click here to view the code.
2
Delete the lines that create the $days and $years arrays.
3
Rewrite the $days foreach loop as a for loop.
4
Rewrite the $years foreach loop as a while loop.
5
Save the file as calendar2.php.
6
Upload the file to your Web server and test in your Web browser.

 
Once you have an HTML form, the next step is to write a bare-bones PHP script to handle it. When we say that this script will be handling the form, we mean that it will do something with the data it receives; that is it will reiterate the data back to the Web browser.

The beauty of PHP — and what makes it so easy to learn and use — is how well it interacts with HTML forms. Rather than requiring a parsing routine — as CGI scripts do — to access form data, your PHP scripts store the information in special variables. For example, say you have a form with an input defined like this:


     




The PHP page that receives the form data will assign what the user entered into this form element to a special variable called $_REQUEST['weight']. It is very important that the spelling and capitalization match exactly, as PHP is case-sensitive when it comes to variable names. $_REQUEST['weight'] can then be used like any other variable: printed, used in mathematical computations, concatenated, and so on.
Registering Globals
In earlier versions of PHP, the
register_globals setting was turned on by default. This feature gave PHP an ease of use by automatically turning form inputs into similarly named variables, like $name or $email (as opposed to having to refer to $_REQUEST['name']and $_REQUEST['email'] first).

As of version 4.2 of PHP, the developers behind PHP opted to turn this setting off by default because not relying on this feature improves the security of your scripts. Unfortunately, this also had the side effect that a lot of existing scripts no longer worked and many beginning programmers were stymied when they saw blank values in their form results, error messages, or just blank pages.
Description: http://central.mindleaders.com/dpec/courses/phps03/ph3c005.gif

To work around this, there are two options. First, you could turn register_globals back on, assuming that you have administrative control over your PHP installation. Second, you could start using the superglobal variables, such as $_REQUEST, $_GET, and $_POST.

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


   
   Simple HTML Form






   
Enter your information in the form below:


   Name: 
     maxlength="40" />

   Email Address: 
     maxlength="60" />

   Gender: Male 
   type="radio" name="gender" value="F" /> Female

   Age:
   
      
      
      
   

   Comments: 

   

   

   






Add the opening PHP tag and create a shorthand version of the form data variables.



// Create a shorthand for the form data.
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$comments = $_REQUEST['comments'];

No comments:

Post a Comment