Another week.-ajax (iii)

Source: Internet
Author: User

Hi

I broke my promise again, but I came back with the audacity ...

1. AJAX (iii)

Third, AJAX A simple example of

3.1 Introduction

Completion: Query employee information, enter employee number to inquire employee's basic information + new employee's information, including employee name, number, gender, position;

Implementation: Pure HTML page +php page, used to implement query staff and new employees in the background interface;

3.2 Server-side implementations

I use the wamp at the end of the collection, a lot of things do not change.

Then write the program at Adobe DREAMWAVERCS6

--dw

installed, online there.

Set up a site first, put in the WWW folder, and then the server and so on themselves to the whole.

Specifically, the site is a folder, in the Apache folder on the line, and then the server address remember to write clearly. Before the last save, the remote cancel, check the test. Too lazy to put the picture, the basic steps is very simple.

The rest is the content of PHP.

--php Code

No more step-by-step, give directly.

<?php
Set page content is HTML encoded format is UTF-8
Header ("Content-type:text/plain;charset=utf-8");
Header ("Content-type:application/json;charset=utf-8");
Header ("Content-type:text/xml;charset=utf-8");
Header ("Content-type:text/html;charset=utf-8");
Header ("Content-type:application/javascript;charset=utf-8");

Defines a multidimensional array that contains information about employees, each employee information as an array
$staff = array
(
Array ("name" = "Hong Qi", "number" = "101", "sex" = "male", "job" and "General manager"),
Array ("name" = "Guo Jing", "number" = "102", "sex" = "male", "job" and "Development Engineer"),
Array ("name" = "Huang Rong", "number" = "103", "sex" = "female", "job" and "Product Manager")
);

Determine if a GET request is being searched, and if it is a POST request, create a new
$_server is a hyper-global variable that is available in all scopes of a script without using the Global keyword
$_server["Request_method"] returns the request method used by the access page
if ($_server["request_method"] = = "GET") {
Search ();
} elseif ($_server["request_method"] = = "POST") {
Create ();
}

//Search employees by Employee number
Function search () {
//check for an employee number parameter
//isset detect if the variable is set; empty value is no null
//Hyper global variable $_get and $_post Used to collect form data
if (!isset ($_get["number"]) | | empty ($_get["number"]) {
echo "parameter error";
Return Variables declared outside of the
}
//function have Global scope and can only be accessed outside of the function.
//global keywords are used to access global variables within functions
Global $staff;
Gets the number parameter
$number = $_get["number"];
$result = "No employees found. ";

//traverse the $staff multidimensional array to find out whether an employee with a key value of number exists and, if present, modify the return result
foreach ($staff as $value) {
if ($value ["number"] = = $ Number) {
$result = "Find Employee: Employee ID:". $value ["No."]. ", Employee Name:". $value ["Name"].
, Employee Gender:. $value ["Sex"]. ", Employee Position:". $value ["Job"];
break;
}
}
echo $result;
}

Create an employee
function Create () {
Determine if the information is fully filled
if (!isset ($_post["name"]) | | empty ($_post["name"])
|| !isset ($_post["number"]) | | Empty ($_post["number"])
|| !isset ($_post["Sex"]) | | Empty ($_post["sex"])
|| !isset ($_post["job"]) | | Empty ($_post["job"])) {
echo "parameter error, employee information is not complete";
Return
}
TODO: Get post form data and save to database

Prompt to save successfully
echo "Employees:". $_post["Name"]. "Information saved successfully!" ";
}

--Server-side testing

Implement--fiddler with tools.

On the Fiddler Main Page, there is a composer tab on the right;

Then choose Get Method , write the address of our file--localhost/ajaxdemo/server.php, click Execute/execute;

The parameter is wrong on return because we do not get what data; number=101, after the address, to execute again, should be able to see, 101 of the details, the file is correct;

Then select the post mode again, then the Address column can not fill in the parameters, but the following to write on content-type:application/x-www-form-urlencoded, tell the server how to do. Write the data at the bottom, execute it, and it will be saved successfully.

After the test, if there is a problem, go where to change it.

The lesson here is that in the web development, the background development of the test, do not rely on the development of the foreground, independent completion first.

3.3 Implementation of the client

Say it tomorrow. (and ran God to recall the Phantom World record ....) )

Another week.-ajax (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.