PHP Introduction
PHP Introduction
and AJAX
PHP
HTML Response
Web Server PHP Results
User
PHP
PHP is similar to C
All scripts start with <?php and with with ?>
Line separator: ; (semi-colon)
Code block: { //code here } (brace brackets)
White space is generally ignored (not in strings)
Comments are created using:
// single line quote
/* Multiple line block quote */
Precedence
Enforced using parentheses
E.g. $sum = 5 + 3 * 6; // would equal 23
$sum = (5 + 3) * 6; // would equal 48
Variables in PHP
What is Form?
When you login into a website or into your mail box, you are interacting
with a form.
Forms are used to get input from the user and submit it to the web
server for processing.
<html>
<head>
<title>Registration Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h2>Registration Form</h2>
The array variable can be accessed from any script in the program; it
has a global scope.
This method is ideal when you do not want to display the form post
values in the URL.
<?php
$_POST['variable_name'];
?>
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email,FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
PHP Arrays
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Close the Connection
mysqli_close($conn);
The PHP functions for use with MySQL have the following
general format −
mysql_function(value,value,...);
mysqli_connect($connect); mysqli_query($connect,"SQL
statement");
MySQL Connection Using PHP Script
Syntax
connection mysql_connect(server,user,passwd,new_link,client_flag);
server
Optional − The host name running the database server. If not specified,
then the default value will be localhost:3306.
user
Optional − The username accessing the database. If not specified, then
the default will be the name of the user that owns the server process.
passwd
Optional − The password of the user accessing the database. If not
specified, then the default will be an empty password.
new_link
Optional − If a second call is made to mysql_connect() with the same
arguments, no new connection will be established; instead, the
identifier of the already opened connection will be returned.
client_flags
Optional − A combination of the following constants −
MYSQL_CLIENT_SSL − Use SSL encryption.
MYSQL_CLIENT_COMPRESS − Use compression protocol.
MYSQL_CLIENT_IGNORE_SPACE − Allow space after function
names.
MYSQL_CLIENT_INTERACTIVE − Allow interactive timeout seconds
of inactivity before closing the connection.
Create a Database using PHP Script
sql
Required − SQL query to create or delete a MySQL database
connection
Optional − if not specified, then the last opened connection by
mysql_connect will be used.
AJAX Introduction
The file can be any kind of file, like .txt and .xml, or server scripting
files like .asp and .php (which can perform actions on the server before
sending the response back).
The onreadystatechange Property
GET is simpler and faster than POST, and can be used in most cases.
However, always use POST requests when:
A cached file is not an option (update a file or database on the server).
Sending a large amount of data to the server (POST has no size
limitations).
Sending user input (which can contain unknown characters), POST is
more robust and secure than GET.
GET Requests
A simple GET request:
Example
xhttp.open("GET", "demo_get.asp", true);
xhttp.send();
POST Requests
A simple POST request:
Example
xhttp.open("POST", "demo_post.asp", true);
xhttp.send();
Synchronous Request
To execute a synchronous request, change the third
parameter in the open() method to false:
xhttp.open("GET", "ajax_info.txt", false);
Sometimes async = false are used for quick testing. You will
also find synchronous requests in older JavaScript code.