Học làm web (x1) - PHP: crash course 1

1         Using PHP

1.1       PHP Crash Course


[Luke Welling, Laura Thomson, PHP and MySQL web development 5th edition, Packt, 2017, page 11] [Download Ebook]

1.1.1       Embedding PHP in HTML


Creating the Order Form

Creating the Order Form in HTML, named orderform.html,

<form action="processorder.php" method="post">
    <table style="border: 0px;">
        <tr style="background:#cccccc;">
            <td style="width: 150px; text-align: center;">Item</td>
            <td style="width: 15px; text-align: center;">Quantity</td>
        </tr>
        <tr>
            <td>Tires</td>
            <td><input type="text" name="tireqty" size="3" maxlength="3" /></td>
        </tr>
        <tr>
            <td>Oil</td>
            <td><input type="text" name="oilqty" size="3" maxlength="3" /></td>
        </tr>
        <tr>
            <td>Spark Plugs</td>
            <td><input type="text" name="sparkqty" size="3" maxlength="3" /></td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center;"><input type="submit" value="Submit
    Order" /></td>
        </tr>
    </table>
</form>

Show the result on the browser,



Notice that the form’s action is set to the name of the PHP script that will process the customer’s order. (You’ll write this script next.) In general, the value of the action attribute is the URL that will be loaded when the user clicks the Submit button. The data the user has typed in the form will be sent to this URL via the HTTP method specified in the method attribute, either get (appended to the end of the URL) or post (sent as a separate message).

Also note the names of the form fields: tireqty, oilqty, and sparkqty. You’ll use these names again in the PHP script. Because the names will be reused, it’s important to give your form fields meaningful names that you can easily remember when you begin writing the PHP script.

Processing the Form

To process the form, you need to create the script mentioned in the action attribute of the form tag called processorder.php. Open your text editor and create this file. Then type in the following code:

<!DOCTYPE html>
<html>
  <head>
    <title>Bob's Auto Parts - Order Results</title>
  </head>
  <body>
    <h1>Bob's Auto Parts</h1>
    <h2>Order Results</h2>
  </body>
</html>

Notice how everything you’ve typed so far is just plain HTML. It’s now time to add some simple PHP code to the script.

Embedding PHP in HTML

Under the <h2> heading in your file, add the following lines:

<?php
echo '<p>Order processed.</p>';
?>

Save the file and load it in your browser by filling out Bob’s form and clicking the Submit Order button. You should see something similar to the output shown in below picture,



Try viewing the source from your browser. You should see this code

<!DOCTYPE html>
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<p>Order processed.</p>
</body>
</html>

None of the raw PHP is visible because the PHP interpreter has run through the script and replaced it with the output from the script. This means that from PHP you can produce clean HTML viewable with any browser; in other words, the user’s browser does not need to understand PHP.

This example illustrates the concept of server-side scripting in a nutshell. The PHP has been interpreted and executed on the web server, as distinct from JavaScript and other clientside technologies interpreted and executed within a web browser on a user’s machine.

The code that you now have in this file consists of four types of text:

– HTML

– PHP tags

– PHP statements

– Whitespace

Lab 1. Make the Order Form for Bob’s Auto Parts.

– Create a project (a website) in your computer, avoid creating in C:\ drive, so you will not losing data when OS being corrupted, example named bobauto

– Embed Git into the project, so you can commit and revert code in the future

– Add the orderform.html into the project

– Add the processorder.php into the project

– Commit the project, message is “make the Order Form”

– Config to access website by bob.local/orderform.html


– Put project on the Internet by using free hosting, test to make sure everyone can access your website

PHP tags (page 16)

PHP statements (page 16)

Whitespace (page 17)

Comments (page 18)

1.1.2       Adding dynamic content


Calling functions (p19)

Using the date() function (p19)

1.1.3       Accessing form variables (p20)


<?php
// create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
?>

This code creates three new variables—$tireqty, $oilqty, and $sparkqty—and sets them to
contain the data sent via the POST method from the form. You can output the values of these variables to the browser by doing, for example:

echo $tireqty.' tires<br />';

However, this approach is not recommended. Because it is not security for website.
For now, it’s enough to know that you should echo out user data to the browser after passing it through a function called htmlspecialchars(). For example, in this case, we would do the following:
echo htmlspecialchars($tireqty).' tires<br />';

To make the script start doing something visible, add the following lines to the bottom of your
PHP script:

echo '<p>Your order is as follows: </p>';
echo htmlspecialchars($tireqty).' tires<br />';
echo htmlspecialchars($oilqty).' bottles of oil<br />';
echo htmlspecialchars($sparkqty).' spark plugs<br />';

Lab 2. Accessing form variables.


Add some code into processorder.php so that when customer fills all fields, then click Submit button at OrderForm, the results is showing as following:



-----------
Cập nhật [4/4/2019]
-----------
Xem thêm: Tổng hợp các bài viết về  Học làm web
Xem thêm: Học làm web (x2) - PHP: Crash course 2