27
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries and extraction of results. Description and exemplification of forms processing to insert and amend data. Description of structure of HTML forms including: the <form> element and its action and method attributes the <input> element and its type, name and value attributes the <button> element and its type, name and value attributes

Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Embed Size (px)

Citation preview

Page 1: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development

Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries and extraction of results.

Description and exemplification of forms processing to insert and amend data.

Description of structure of HTML forms including: – the <form> element and its action and method attributes – the <input> element and its type, name and value

attributes – the <button> element and its type, name and value

attributes

Page 2: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description and exemplification of server-side scripting language for server connection

Server-side scripting is a web server technology in which a user's request is fulfilled by running a script directly on the web server to generate dynamic HTML pages.

It is usually used to provide interactive web sites that interface to databases or other data stores. This is different from client-side scripting where scripts are run by the viewing web browser.

The primary advantage to server-side scripting is the ability to highly customize the response based on the user's requirements, access rights, or queries into data stores.

Page 3: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description and exemplification of server-side scripting language for server connection

You have to connect to the database server and select the database to be used before you can execute MySQL statements. PHP has a number of commands which are used for this purpose.

The following PHP function call establishes the connection:

– mysql_connect(address, username, password);

The mysql_connect function shown above, for example, returns a number that identifies the connection that has been established. We need to store this value so that we can make use of it later to select the database we wish to use.

– $varconnect = mysql_connect("localhost", "root", "password");

What's important to see here is that the value returned by mysql_connect (which we'll call a connection identifier) is stored in a variable named $varconnect.

Page 4: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development- Description and exemplification of server-side scripting language for database selection

Selecting that database in PHP uses another function call:

– mysql_select_db(database_name, database_connection);

For example:– mysql_select_db("freedomofmidnight", $varDBconnect);

The $varDBconnect variable that contains the database connection identifier is used to tell the function which database connection to use. This parameter is actually optional. When it's omitted, the function will automatically use the details of the last connection opened. This function returns true when it's successful and false if an error occurs.

Page 5: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development- Description and exemplification of server-side scripting language execution of SQL queries and extraction of results

All SQL queries are executed using the mysql_query() function. This executes the query and returns the results set as an array of values.

– $result_array=mysql_query($query_string);

This can be used with the @ symbol, as before, to suppress error messages as @mysql_query($query_string). This allows you to use the mysql_error function to access the last error set by the MySQL server.

MySQL also keeps track of the number of rows affected by INSERT, DELETE and UPDATE queries and the number of rows in the results of a SELECT query. This number can be accessed using the mysql_affected_rows() function.

Page 6: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development- Description and exemplification of server-side scripting language execution of SQL queries and extraction of results

For most SQL queries, the mysql_query() function returns either true (success) or false (failure) but for SELECT queries the function returns the result set, the answer table, generated by the query. False is still returned if the query fails for any reason.

– $result = @mysql_query("SELECT name FROM publisher");if (!$result) {echo("<p>Error performing query: " . mysql_error() . "</p>");exit();}

As long as no error is encountered in processing the query, the above code places a result set that contains the names of all the publishers stored in the publisher table into the variable $result. There is no practical limit on the number of publishers in the database so the result set generated can be very large.

Page 7: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development- Description and exemplification of forms processing to insert and amend data.

For many applications of PHP, the ability to interact with users who view the Web page is essential. Server-side scripting languages such as PHP have a more limited scope when it comes to user interaction. As PHP code is activated when a page is requested from the server, user interaction can occur only in a back-and-forth fashion: the user sends requests to the server, and the server replies with dynamically generated pages.

The key to creating interactivity with PHP is to understand the techniques used to send information about a user's interaction along with his or her request for a new Web page.

Page 8: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development- Description and exemplification of forms processing to insert and amend data.

The GET Method:– The simplest method that can be used to send information along

with a page request uses the URL query string. If you've ever seen a URL with a question mark following the file name, you've seen this technique in use.

<html><head> </head><body><a href="welcome1.php?name=Charlie">Hi, I'm Charlie!</a> </body></html>

– The above code is a link created in an HTML document,– This is a link to a file called welcome1.php, but as well as linking

to the file, we're also passing a variable along with the page request. The variable is passed as part of the query string, which is the portion of the URL that follows the question mark. The variable is called name and its value is Charlie. To restate, we have created a link that loads welcome1.php, and informs the PHP code contained in the file that name equals Charlie.

Page 9: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development- Description and exemplification of forms processing to insert and amend data.

The GET Method:

– To really understand the results of this process, we need to look at welcome1.php

<html><head> </head><body><?php$name = $_GET['name'];echo( "Welcome to our Website, $name!" ); ?> </body></html>

– Now, if you use the link in the first file to load this second file, you'll see that the page says "Welcome to our Website, Charlie!"

– PHP automatically creates an array variable called $_GET that contains any values passed in the query string. $_GET is an associative array, so the value of the name variable passed in the query string can be accessed as $_GET['name']. Our script assigns this value to an ordinary PHP variable ($name) and then displays it as part of a text string using the echo function.

Page 10: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development- Description and exemplification of forms processing to insert and amend data.

The POST Method:

– It's not always desirable - or even technically feasible - to have the values appear in the query string. What if we included a <textarea> tag in the form, to let the user enter a large amount of text? A URL that contained several paragraphs of text in the query string would be ridiculously long, and would exceed by far the maximum length of the URL accepted by browsers.

– The alternative is for the browser to pass the information invisibly, behind the scenes. The code for this looks exactly the same, but we use the POST method instead of the GET method used in the last example.

Page 11: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development- Description and exemplification of forms processing to insert and amend data.

The POST Method: This HTML code would be entered in the link

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form action="welcome4.php" method="post">

First Name: <input type="text" name="firstname" /><br /> Last Name: <input type="text" name="lastname" /><br />

<input type="submit" value="Submit" /> </form> </body> </html>

Page 12: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development- Description and exemplification of forms processing to insert and amend data.

The POST Method: As we're no longer sending the variables as part of the query string, they no

longer appear in PHP's $_GET array. Instead, they are placed in another array reserved especially for 'posted' form variables: $_POST.

The php would be as follows:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php

$firstname = $_POST['firstname']; $lastname = $_POST['lastname']; echo( "Welcome to my Website, $firstname $lastname!" );

?> </body> </html>

Page 13: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development- Description and exemplification of forms processing to insert and amend data.

The IF control structure allows decisions to be made: The php would be as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php

$firstname = $_GET['firstname']; $lastname = $_GET['lastname']; if ( $firstname == 'Charlie' and $lastname == 'Burton' ) {

echo( 'Hi, How can I help you, Charlie' ); } else {

echo( "Welcome to my Website, $firstname $lastname!" ); }

?> </body> </html>

Page 14: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development- Description and exemplification of forms processing to insert and amend data.

The while loop uses a condition to determine how many times a piece of code is executed (conditional loop):

The php would be as follows:

<html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php

$count = 1; while ($count <= 10) {

echo( "$count " ); $count++;

} ?> </body> </html>

Page 15: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development- Description and exemplification of forms processing to insert and amend data.

The For loop is used when a set number of repetitions are required. Often this is when a value is being counted. (Fixed Loop)

The php would be as follows:

<html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php

for ($count = 1; $count <= 10; $count++) {echo( "$count " );

?> </body> </html>

Page 16: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description of structure of HTML forms

HTML documents are text files made up of HTML elements.

HTML elements are defined using HTML tags.

HTML Tags– HTML tags are used to mark-up HTML elements – HTML tags are surrounded by the two characters < and > – The surrounding characters are called angle brackets – HTML tags normally come in pairs like <b> and </b> – The first tag in a pair is the start tag, the second tag is the

end tag – The text between the start and end tags is the element

content – HTML tags are not case sensitive, <b> means the same as

<B>

Page 17: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description of structure of HTML forms

Basic HTML Tags<html> Defines an HTML document<body> Defines the document's body<h1> to <h6> Defines header 1 to header 6<p> Defines a paragraph<br> Inserts a single line break<hr> Defines a horizontal rule<!--> Defines a comment

Page 18: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description of structure of HTML forms

Tables are defined with the <table> tag. A table is divided into rows (with the <tr> tag), and each row is divided into data cells (with the <td> tag). The letters td stands for "table data," which is the content of a data cell. A data cell can contain text, images, lists, paragraphs, forms, horizontal rules, tables, etc.

<table border="1"> <tr> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td>row 2, cell 1</td> <td>row 2, cell 2</td> </tr> </table>

Page 19: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description of structure of HTML forms

Basic Table HTML Tags<table> Defines a table<th> Defines a table header<tr> Defines a table row<td> Defines a table cell<caption> Defines a table caption<colgroup> Defines groups of table columns<col> Defines the attribute values for one or more columns in a table<thead> Defines a table head<tbody> Defines a table body <tfoot> Defines a table footer

Page 20: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description of structure of HTML forms

HTML <option> tag The option element defines an option in

the drop-down list. Example

<select><option value ="volvo">Volvo</option><option value ="saab">Saab</option><option value ="opel" selected="selected">Opel</option><option value ="audi">Audi</option></select>

Page 21: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description of structure of HTML forms

HTML Elements Here is an example:

<html> <head> <title>Title of page</title> </head> <body> This is my first homepage. <b>This text is bold</b> </body> </html>

This is an HTML element:– <b>This text is bold</b>

The HTML element starts with a start tag: <b>The content of the HTML element is: This text is boldThe HTML element ends with an end tag: </b>

The purpose of the <b> tag is to define an HTML element that should be displayed as bold.

Page 22: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description of structure of HTML forms

Forms– A form is an area that can contain form elements.– Form elements are elements that allow the user to

enter information (like text fields, textarea fields, drop-down menus, radio buttons, checkboxes, etc.) in a form.

A form is defined with the <form> tag.<form> <input> <input> </form>

eg– <form action="form_action.asp"method="get">

Page 23: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description of structure of HTML forms

Input– The most used form tag is the <input> tag. The

type of input is specified with the type attribute. The most commonly used input types are explained below.

– Text Fields Text fields are used when you want the user to type

letters, numbers, etc. in a form.<form> First name: <input type="text" name="firstname“ size =“20”> <br> Last name: <input type="text" name="lastname"> </form>

Page 24: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description of structure of HTML forms

Input– The most used form tag is the <input> tag. The

type of input is specified with the type attribute. The most commonly used input types are explained below.

– Password Fields Password fields are used when you want the users

entry to be hidden with *.<form> User ID: <input type="text" name=“userID"> <br> Password: <input type=“text" name=“password"> </form>

Page 25: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description of structure of HTML forms

Radio Buttons Radio Buttons are used when you want the

user to select one of a limited number of choices.

<form>

<input type="radio" name="sex" value="male"> Male <br>

<input type="radio" name="sex" value="female"> Female

</form> Note that only one option can be chosen.

Page 26: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description of structure of HTML forms

Checkboxes – Checkboxes are used when you want the user to select one or more

options of a limited number of choices.

<form> I have a bike: <input type="checkbox" name="vehicle"

value="Bike" /> <br /> I have a car: <input type="checkbox" name="vehicle" value="Car" /> <br /> I have an airplane: <input type="checkbox" name="vehicle"

value="Airplane" /> </form>

Page 27: Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries

Application Development - Description of structure of HTML forms

The Form's Action Attribute and the Submit Button– When the user clicks on the "Submit" button, the content of the

form is sent to another file. The form's action attribute defines the name of the file to send the content to. The file defined in the action attribute usually does something with the received input.

<form name="input" action="html_form_action.asp" method="get">

Username:<input type="text" name="user"> <input type="submit" value="Submit"> </form>

– Username: If you type some characters in the text field above, and click the "Submit" button, you will send your input to a page called "html_form_action.asp". That page will show you the received input.