SlideShare a Scribd company logo
Sending Values
         Manually


Pengaturcaraan PHP




Pengaturcaraan PHP
In the examples so far, all of the data received in the PHP script came from
what the user entered in a form. There are, however, two different ways
you can pass variables and values to a PHP script, both worth knowing.

The first method is to make use of HTML's hidden input type:




                                                                               1
Pengaturcaraan PHP

The second method is to append a value to the PHP script's URL:




This technique emulates the GET method of an HTML form. With this
specific example, page.php receives a variable called $_GET['name'] with a
value of Brian.

To demonstrate this GET method trick, a new version of the view_users.php
script will be written. This one will provide links to edit or delete an existing
user. The links will pass the user's ID to the handling pages, both of which
will be written subsequently.




                                                                                    2
Pengaturcaraan PHP

Step 2
Change the SQL query as follows.




 We have changed this query in a couple of ways. First, we select the first
 and last names as separate values, instead of as one concatenated value.
 Second, we now also select the user_id value, which will be necessary in
 creating the links.




 Pengaturcaraan PHP
 Step 3
 Add three more columns to the
 main table.

 In the previous version of the
 script, there were only two
 columns: one for the name and
 another for the date the user
 registered. We've separated
 out the name column into its
 two parts and created one
 column for the Edit link and
 another for the Delete link.




                                                                              3
Pengaturcaraan PHP
Step 4
Change the echo statement within
the while loop to match the table's
new structure.


For each record returned from
the database, this line will print
out a row with five columns. The
last three columns are obvious
and easy to create: just refer to
the returned column name.




 Pengaturcaraan PHP

For the first two columns,
which provide links to edit or
delete the user, the syntax is
slightly more complicated. The
desired end result is HTML
code like <a
href="edit_user.php?id=X">Edi
t</a>, where X is the user's ID.
Having established this, all we
have to do is print
$row['user_id'] for X, being
mindful of the quotation marks
to avoid parse errors.




                                      4
Pengaturcaraan PHP




    Using Hidden Form
    Inputs


Pengaturcaraan PHP




                        5
Pengaturcaraan PHP

  Step 1
  Create a new PHP document in your text editor or IDE.




Pengaturcaraan PHP
Step 2
Include the page header. This document will use the same template system
as the other pages in the application.




                                                                           6
Pengaturcaraan PHP
Step 3
Check for a valid user ID value.




Pengaturcaraan PHP

Step 4
Include the MySQL connection script.




                                       7
Pengaturcaraan PHP

Step 5
Begin the main submit conditional.




Pengaturcaraan PHP

Step 6
Delete the user, if appropriate.




                                     8
Pengaturcaraan PHP




Pengaturcaraan PHP

Step 7
Check if the deletion worked and respond accordingly.




                                                        9
Pengaturcaraan PHP
Step 11
Display the form. First, the database information is retrieved using the
mysql_fetch_array() function. Then the form is printed, showing the name
value retrieved from the database at the top. An important step here is
that the user ID ($id) is stored as a hidden form input so that the
handling process can also access this value.




 Pengaturcaraan PHP




                                                                           10
Editing Existing
          Records


 Pengaturcaraan PHP




Pengaturcaraan PHP
Step 1
Create a new PHP document in your text editor or IDE.




                                                        11
Pengaturcaraan PHP
Step 3
Include the MySQL connection script and begin the main submit conditional.




Pengaturcaraan PHP
Step 4
Validate the form data.




                                                                             12
Pengaturcaraan PHP




Pengaturcaraan PHP
Step 6
Update the database.




                       13
Pengaturcaraan PHP
Display the form. The form has but three text inputs, each of which is made
sticky using the data retrieved from the database. The user ID ($id) is stored
as a hidden form input so that the handling process can also access this
value.




            Paginating Query
            Results


   Pengaturcaraan PHP




                                                                                 14
Pengaturcaraan PHP




Pengaturcaraan PHP
Step 2
After including the database connection, set the number of
records to display per page.




                                                             15
Pengaturcaraan PHP

Step 3
Check if the number of required pages has been determined.




Pengaturcaraan PHP
Step 4
Count the number of records in the database.




                                                             16
Pengaturcaraan PHP

Step 5
Mathematically calculate how many pages are required.




Pengaturcaraan PHP

Step 6
Determine the starting point in the database.




                                                        17
Pengaturcaraan PHP

Step 7
Change the query so that it uses the LIMIT clause.




Pengaturcaraan PHP
Step 12
After completing the HTML table, begin a section for displaying links
to other pages, if necessary.




                                                                        18
Pengaturcaraan PHP




Pengaturcaraan PHP




                     19
Pengaturcaraan PHP
Step 13
Finish making the links.




Pengaturcaraan PHP




                           20
Making Sortable
           Displays


  Pengaturcaraan PHP




Pengaturcaraan PHP

Step 1
Open or create view_users.php in your text editor or IDE.




                                                            21
Pengaturcaraan PHP

Step 3
Check if a sorting order has already been determined.




Pengaturcaraan PHP
Step 4
Begin defining a switch conditional that determines how the
results should be sorted.




                                                              22
Pengaturcaraan PHP
Step 5
Complete the switch
conditional. There are six
total conditions to check
against, plus the default
(just in case). For each the
$order_by variable is
defined as it will be used in
the query and the
appropriate link is
redefined. Since each link
has already been given a
default value (Step 2), we
only need to change a
single link's value for each
case.




Pengaturcaraan PHP

 Step 6
 Complete the isset() conditional.




                                     23
Pengaturcaraan PHP
Step 7
Modify the query to use the new $order_by variable.




Pengaturcaraan PHP
Step 8
Modify the table header echo statement to create links out of the column
headings.




                                                                           24
Pengaturcaraan PHP

Step 9
Modify the echo statement that creates the Previous link so that the sort
value is also passed.




Pengaturcaraan PHP

 Step 10
 Repeat Step 9 for the numbered pages and the Next link.




                                                                            25
Pengaturcaraan PHP




    Understanding HTTP
    Headers


Pengaturcaraan PHP




                         26
Pengaturcaraan PHP
HTTP (Hypertext Transfer Protocol) is the technology at the heart of the World
Wide Web because it defines the way clients and servers communicate (in
layman's terms).

PHP's built-in header() function can be used to take advantage of this protocol.
The most common example of this will be demonstrated here, when the
header() function will be used to redirect the Web browser from the current
page to another.

To use header() to redirect the Web browser, type the following:




Pengaturcaraan PHP




                                                                                   27
Pengaturcaraan PHP
You can avoid this problem using the headers_sent() function, which
checks whether or not data has been sent to the Web browser.




Pengaturcaraan PHP




                                                                      28
Pengaturcaraan PHP
Step 6
Dynamically determine the redirection URL and then call the header() function.




 header ('Location: http://' . $_SERVER['HTTP_HOST'] .
  dirname($_SERVER['PHP_SELF']) . '/newpage.php');




Pengaturcaraan PHP

Passing values
You can add name=value pairs to the URL in a header() call to pass
values to the target page. In this example, if you added this line to the
script, prior to redirection:

$url .= '?name=' . urlencode ("$fn $ln");

then the thanks.php page could greet the user by $_GET['name'].




                                                                                 29
End



Pengaturcaraan PHP




                     30

More Related Content

PPSX
Php NotesBeginner
PDF
&lt;img src="../i/r_14.png" />
PPT
9780538745840 ppt ch10
PDF
RicoAjaxEngine
PPT
9780538745840 ppt ch04
PPT
9780538745840 ppt ch09
PPT
Handling User Input and Processing Form Data
Php NotesBeginner
&lt;img src="../i/r_14.png" />
9780538745840 ppt ch10
RicoAjaxEngine
9780538745840 ppt ch04
9780538745840 ppt ch09
Handling User Input and Processing Form Data

What's hot (20)

PPT
Php MySql For Beginners
PPT
9780538745840 ppt ch07
PDF
PHP and Mysql
PDF
Word Press Help Sheet
PDF
A linux mac os x command line interface
DOC
Library Project
PDF
Final Presentation
PDF
Basic Crud In Django
PDF
PHP And Web Services: Perfect Partners
PPT
9780538745840 ppt ch05
PDF
NYCCAMP 2015 Demystifying Drupal AJAX Callback Commands
PPT
CRUD with Dojo
PDF
Mule caching strategy with redis cache
PPT
Lecture7 form processing by okello erick
DOC
235689260 oracle-forms-10g-tutorial
PDF
JSON-RPC Proxy Generation with PHP 5
PPT
Rail3 intro 29th_sep_surendran
PPTX
Using SP Metal for faster share point development
DOC
Php MySql For Beginners
9780538745840 ppt ch07
PHP and Mysql
Word Press Help Sheet
A linux mac os x command line interface
Library Project
Final Presentation
Basic Crud In Django
PHP And Web Services: Perfect Partners
9780538745840 ppt ch05
NYCCAMP 2015 Demystifying Drupal AJAX Callback Commands
CRUD with Dojo
Mule caching strategy with redis cache
Lecture7 form processing by okello erick
235689260 oracle-forms-10g-tutorial
JSON-RPC Proxy Generation with PHP 5
Rail3 intro 29th_sep_surendran
Using SP Metal for faster share point development
Ad

Viewers also liked (16)

PDF
Dynamic website
PDF
Wells Fargo HAFA Guidelines
PDF
My sql
PDF
Web application security
PDF
Error handling and debugging
PDF
Equator Short Sale Manual
PDF
Basic php
PPT
Pfextinguisher
PDF
Hcg foods
PDF
RMA - Request for mortgage assistance
PDF
Equator Short Sale Manual
PDF
bank of america short sale check list
DOCX
List of Internet Acronyms
PDF
Using php with my sql
PDF
ชุดกิจกรรมที่ 1
Dynamic website
Wells Fargo HAFA Guidelines
My sql
Web application security
Error handling and debugging
Equator Short Sale Manual
Basic php
Pfextinguisher
Hcg foods
RMA - Request for mortgage assistance
Equator Short Sale Manual
bank of america short sale check list
List of Internet Acronyms
Using php with my sql
ชุดกิจกรรมที่ 1
Ad

Similar to Developing web applications (20)

PDF
Programming with php
PDF
Php tutorial handout
PDF
Pagination in PHP
PDF
P mysql training in bangalore
PDF
PHP & mySQL Training in Bangalore at myTectra
PDF
Phpbasics
PPTX
PDF
SULTHAN's - PHP MySQL programs
PDF
php-mysql-tutorial-part-3
PDF
php-mysql-tutorial-part-3
PDF
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
KEY
Introduction to php
PPTX
Lecture 8 PHP and MYSQL part 2.ppType Classificationtx
PPTX
php.pptx
PPTX
php is the most important programming language
PDF
Cookies and sessions
DOC
PHP training in chennai
DOC
Php Training in chennai with excellent placement
DOC
HTML and CSS Certification in Chennai
PPT
10_introduction_php.ppt
Programming with php
Php tutorial handout
Pagination in PHP
P mysql training in bangalore
PHP & mySQL Training in Bangalore at myTectra
Phpbasics
SULTHAN's - PHP MySQL programs
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
Introduction to php
Lecture 8 PHP and MYSQL part 2.ppType Classificationtx
php.pptx
php is the most important programming language
Cookies and sessions
PHP training in chennai
Php Training in chennai with excellent placement
HTML and CSS Certification in Chennai
10_introduction_php.ppt

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Cell Types and Its function , kingdom of life
human mycosis Human fungal infections are called human mycosis..pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Chinmaya Tiranga quiz Grand Finale.pdf
Orientation - ARALprogram of Deped to the Parents.pptx
O7-L3 Supply Chain Operations - ICLT Program
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Weekly quiz Compilation Jan -July 25.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
A systematic review of self-coping strategies used by university students to ...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf

Developing web applications

  • 1. Sending Values Manually Pengaturcaraan PHP Pengaturcaraan PHP In the examples so far, all of the data received in the PHP script came from what the user entered in a form. There are, however, two different ways you can pass variables and values to a PHP script, both worth knowing. The first method is to make use of HTML's hidden input type: 1
  • 2. Pengaturcaraan PHP The second method is to append a value to the PHP script's URL: This technique emulates the GET method of an HTML form. With this specific example, page.php receives a variable called $_GET['name'] with a value of Brian. To demonstrate this GET method trick, a new version of the view_users.php script will be written. This one will provide links to edit or delete an existing user. The links will pass the user's ID to the handling pages, both of which will be written subsequently. 2
  • 3. Pengaturcaraan PHP Step 2 Change the SQL query as follows. We have changed this query in a couple of ways. First, we select the first and last names as separate values, instead of as one concatenated value. Second, we now also select the user_id value, which will be necessary in creating the links. Pengaturcaraan PHP Step 3 Add three more columns to the main table. In the previous version of the script, there were only two columns: one for the name and another for the date the user registered. We've separated out the name column into its two parts and created one column for the Edit link and another for the Delete link. 3
  • 4. Pengaturcaraan PHP Step 4 Change the echo statement within the while loop to match the table's new structure. For each record returned from the database, this line will print out a row with five columns. The last three columns are obvious and easy to create: just refer to the returned column name. Pengaturcaraan PHP For the first two columns, which provide links to edit or delete the user, the syntax is slightly more complicated. The desired end result is HTML code like <a href="edit_user.php?id=X">Edi t</a>, where X is the user's ID. Having established this, all we have to do is print $row['user_id'] for X, being mindful of the quotation marks to avoid parse errors. 4
  • 5. Pengaturcaraan PHP Using Hidden Form Inputs Pengaturcaraan PHP 5
  • 6. Pengaturcaraan PHP Step 1 Create a new PHP document in your text editor or IDE. Pengaturcaraan PHP Step 2 Include the page header. This document will use the same template system as the other pages in the application. 6
  • 7. Pengaturcaraan PHP Step 3 Check for a valid user ID value. Pengaturcaraan PHP Step 4 Include the MySQL connection script. 7
  • 8. Pengaturcaraan PHP Step 5 Begin the main submit conditional. Pengaturcaraan PHP Step 6 Delete the user, if appropriate. 8
  • 9. Pengaturcaraan PHP Pengaturcaraan PHP Step 7 Check if the deletion worked and respond accordingly. 9
  • 10. Pengaturcaraan PHP Step 11 Display the form. First, the database information is retrieved using the mysql_fetch_array() function. Then the form is printed, showing the name value retrieved from the database at the top. An important step here is that the user ID ($id) is stored as a hidden form input so that the handling process can also access this value. Pengaturcaraan PHP 10
  • 11. Editing Existing Records Pengaturcaraan PHP Pengaturcaraan PHP Step 1 Create a new PHP document in your text editor or IDE. 11
  • 12. Pengaturcaraan PHP Step 3 Include the MySQL connection script and begin the main submit conditional. Pengaturcaraan PHP Step 4 Validate the form data. 12
  • 13. Pengaturcaraan PHP Pengaturcaraan PHP Step 6 Update the database. 13
  • 14. Pengaturcaraan PHP Display the form. The form has but three text inputs, each of which is made sticky using the data retrieved from the database. The user ID ($id) is stored as a hidden form input so that the handling process can also access this value. Paginating Query Results Pengaturcaraan PHP 14
  • 15. Pengaturcaraan PHP Pengaturcaraan PHP Step 2 After including the database connection, set the number of records to display per page. 15
  • 16. Pengaturcaraan PHP Step 3 Check if the number of required pages has been determined. Pengaturcaraan PHP Step 4 Count the number of records in the database. 16
  • 17. Pengaturcaraan PHP Step 5 Mathematically calculate how many pages are required. Pengaturcaraan PHP Step 6 Determine the starting point in the database. 17
  • 18. Pengaturcaraan PHP Step 7 Change the query so that it uses the LIMIT clause. Pengaturcaraan PHP Step 12 After completing the HTML table, begin a section for displaying links to other pages, if necessary. 18
  • 20. Pengaturcaraan PHP Step 13 Finish making the links. Pengaturcaraan PHP 20
  • 21. Making Sortable Displays Pengaturcaraan PHP Pengaturcaraan PHP Step 1 Open or create view_users.php in your text editor or IDE. 21
  • 22. Pengaturcaraan PHP Step 3 Check if a sorting order has already been determined. Pengaturcaraan PHP Step 4 Begin defining a switch conditional that determines how the results should be sorted. 22
  • 23. Pengaturcaraan PHP Step 5 Complete the switch conditional. There are six total conditions to check against, plus the default (just in case). For each the $order_by variable is defined as it will be used in the query and the appropriate link is redefined. Since each link has already been given a default value (Step 2), we only need to change a single link's value for each case. Pengaturcaraan PHP Step 6 Complete the isset() conditional. 23
  • 24. Pengaturcaraan PHP Step 7 Modify the query to use the new $order_by variable. Pengaturcaraan PHP Step 8 Modify the table header echo statement to create links out of the column headings. 24
  • 25. Pengaturcaraan PHP Step 9 Modify the echo statement that creates the Previous link so that the sort value is also passed. Pengaturcaraan PHP Step 10 Repeat Step 9 for the numbered pages and the Next link. 25
  • 26. Pengaturcaraan PHP Understanding HTTP Headers Pengaturcaraan PHP 26
  • 27. Pengaturcaraan PHP HTTP (Hypertext Transfer Protocol) is the technology at the heart of the World Wide Web because it defines the way clients and servers communicate (in layman's terms). PHP's built-in header() function can be used to take advantage of this protocol. The most common example of this will be demonstrated here, when the header() function will be used to redirect the Web browser from the current page to another. To use header() to redirect the Web browser, type the following: Pengaturcaraan PHP 27
  • 28. Pengaturcaraan PHP You can avoid this problem using the headers_sent() function, which checks whether or not data has been sent to the Web browser. Pengaturcaraan PHP 28
  • 29. Pengaturcaraan PHP Step 6 Dynamically determine the redirection URL and then call the header() function. header ('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/newpage.php'); Pengaturcaraan PHP Passing values You can add name=value pairs to the URL in a header() call to pass values to the target page. In this example, if you added this line to the script, prior to redirection: $url .= '?name=' . urlencode ("$fn $ln"); then the thanks.php page could greet the user by $_GET['name']. 29