Ajax asynchronous submission for PHP7 message board development
PHP7 TutorialIntroducing Ajax asynchronous submission about message board development
php7 Tutorial,ajax tutorial
PrefaceThis tutorial is the most important part of this album. With the continuous iterative updates of front-end technology, the implementation of many functions of the website has been transferred to the front-end. Judging from the programming language rankings in the first quarter of 2018, JavaScript is still the most popular programming language. So if you want to learn js in depth, Ajax is essential.I have talked about js verification in the previous course. The courseware code is modified based on the PHP7 message board development (JS verification) of Friends. It can be easily completed by just adding the ajax asynchronous operation part. It should be noted that here we will not explain in detail what the XMLHttpRequest object is, why we should use it, etc. I believe that if you can use it again, you will be more impressed if you learn more about it. I will try my best here. Less text-based test-oriented teaching can achieve learning results. Open the editor and get started! Explanation of the core part of Ajax asynchronous
// 第一步 创建 XMLHttpRequest 对象,为了更容易理解,变量就用ajax
var ajax = new XMLHttpRequest();
// 第二步 初始化一个Ajax请求,url参数是远程请求地址ajax.php
ajax.open('POST', url, true); // 这里用到post提交留言,所以用post方式提交给服务器
// ajax.open('GET', url, true); // get方式请求服务器
// 第三步 发送请求;如果该请求是异步模式(默认),该方法会立刻返回。
ajax.send(inputdata);
// 第四步 发送请求总该要知道有没收到吧,这里就需要用到Ajax的事件监听器onreadystatechange
ajax.onreadystatechange = function(){
// 这里判断服务器是否有数据响应,如果有则做些你要处理的逻辑,比如提示用户操作成功
}
Copy after login
// 第一步 创建 XMLHttpRequest 对象,为了更容易理解,变量就用ajax var ajax = new XMLHttpRequest(); // 第二步 初始化一个Ajax请求,url参数是远程请求地址ajax.php ajax.open('POST', url, true); // 这里用到post提交留言,所以用post方式提交给服务器 // ajax.open('GET', url, true); // get方式请求服务器 // 第三步 发送请求;如果该请求是异步模式(默认),该方法会立刻返回。 ajax.send(inputdata); // 第四步 发送请求总该要知道有没收到吧,这里就需要用到Ajax的事件监听器onreadystatechange ajax.onreadystatechange = function(){ // 这里判断服务器是否有数据响应,如果有则做些你要处理的逻辑,比如提示用户操作成功 }
If you still can’t understand the above four steps, you can think of it as the first step to find a friend A to send an email, and the second step to send an email to this A The friend wrote the content of the letter and encapsulated it with the address and affixed a stamp. The third step was to deliver the mail. After a while (of course there was an immediate response if the Internet connection was available), the fourth step was to receive a reply from friend A. I was very happy. Next, you can do things based on the content of the reply, such as meeting (online) or making an appointment...Okay, that’s it. Here is the complete code. HTML JS page code
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>留言板_科科分享</title>
<!-- 2.新建css样式文件并根据效果图编写css代码 -->
<link rel="stylesheet" href="feedback.css">
<!-- 3.js表单验证 -->
<script type="text/javascript">
function checkform(){
var nickname = document.getElementsByTagName('input')[0].value; // 获取用户输入的姓名
var tel = document.getElementsByTagName('input')[1].value; // 获取用户输入的联系方式
var content = document.getElementsByTagName('textarea')[0].value; // 获取用户输入的留言内容
// 如果没有输入姓名 则提示
if(nickname == ''){
alert('请输入您的姓名');
document.getElementsByTagName('input')[0].focus(); // 将光标定位到姓名输入框
return false; // 阻止冒泡 输入姓名后才能通过
}
// 如果没有输入联系方式 则提示
if(tel == ''){
alert('请输入您的联系方式');
document.getElementsByTagName('input')[1].focus(); // 将光标定位到联系方式输入框
return false; // 阻止冒泡 输入联系方式才能通过
}
// 如果没有输入留言内容 则提示
if(content == ''){
alert('请输入您的联系方式');
document.getElementsByTagName('textarea')[0].focus(); // 将光标定位到留言内容输入框
return false; // 阻止冒泡 输入留言内容才能通过
}
// js已经拿到了用户提交的数据,那接下来就是AJAX(页面无刷新提交数据到服务器-异步通信)
// 异步请求 start
var ajax, url, inputdata;
// 创建 XMLHttpRequest 对象
if(window.XMLHttpRequest){
ajax = new XMLHttpRequest();
}else{
// 兼容Internet Explorer(IE5 和 IE6)使用 ActiveX 对象
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
url = 'ajax.php';
ajax.open('POST', url, true);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); // 用POST的时候一定要有这句
inputdata = 'nickname='+nickname+'&tel='+tel+'&content='+content;
ajax.send(inputdata);
// 接收服务器返回的数据
ajax.onreadystatechange = function(){
// 获取服务器响应状态码
if(ajax.readyState == 4 && ajax.status==200){
// 获取服务器返回的响应返回的数据
var retdata = ajax.responseText;
if(retdata == 1){
alert('留言信息已提交成功!感谢您的宝贵意见。');
}
}
}
// 异步请求 end
return false; // 这里是为了方式submit点击后表单自动提交
// document.feedback_form.submit(); // 提交用户数据到后端action中的地址
}
</script>
</head>
<body>
<!-- 工作区,呈现给用户看的 -->
<!-- 1.开始搭建脚手架 -->
<p class="container_box">
<p class="up">
<h3 class="title">留言板</h3>
<h5 class="subtitle">FEEDBACK</h5>
</p>
<p class="down">
<form name="feedback_form" action="/#" method="post" onsubmit="return false;">
<p class="input">
<input type="text" class="fl" name="name" placeholder="输入您的姓名" />
<input type="text" class="fr" name="tel" placeholder="输入您的联系方式" />
</p>
<textarea class="content" cols="30" rows="10" name="nr"></textarea>
<input type="submit" onclick="checkform()" value="提交您的留言" class="sub" />
</form>
</p>
</p>
</body>
</html>
Copy after login
PHP code (ajax.php)<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>留言板_科科分享</title> <!-- 2.新建css样式文件并根据效果图编写css代码 --> <link rel="stylesheet" href="feedback.css"> <!-- 3.js表单验证 --> <script type="text/javascript"> function checkform(){ var nickname = document.getElementsByTagName('input')[0].value; // 获取用户输入的姓名 var tel = document.getElementsByTagName('input')[1].value; // 获取用户输入的联系方式 var content = document.getElementsByTagName('textarea')[0].value; // 获取用户输入的留言内容 // 如果没有输入姓名 则提示 if(nickname == ''){ alert('请输入您的姓名'); document.getElementsByTagName('input')[0].focus(); // 将光标定位到姓名输入框 return false; // 阻止冒泡 输入姓名后才能通过 } // 如果没有输入联系方式 则提示 if(tel == ''){ alert('请输入您的联系方式'); document.getElementsByTagName('input')[1].focus(); // 将光标定位到联系方式输入框 return false; // 阻止冒泡 输入联系方式才能通过 } // 如果没有输入留言内容 则提示 if(content == ''){ alert('请输入您的联系方式'); document.getElementsByTagName('textarea')[0].focus(); // 将光标定位到留言内容输入框 return false; // 阻止冒泡 输入留言内容才能通过 } // js已经拿到了用户提交的数据,那接下来就是AJAX(页面无刷新提交数据到服务器-异步通信) // 异步请求 start var ajax, url, inputdata; // 创建 XMLHttpRequest 对象 if(window.XMLHttpRequest){ ajax = new XMLHttpRequest(); }else{ // 兼容Internet Explorer(IE5 和 IE6)使用 ActiveX 对象 ajax = new ActiveXObject("Microsoft.XMLHTTP"); } url = 'ajax.php'; ajax.open('POST', url, true); ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); // 用POST的时候一定要有这句 inputdata = 'nickname='+nickname+'&tel='+tel+'&content='+content; ajax.send(inputdata); // 接收服务器返回的数据 ajax.onreadystatechange = function(){ // 获取服务器响应状态码 if(ajax.readyState == 4 && ajax.status==200){ // 获取服务器返回的响应返回的数据 var retdata = ajax.responseText; if(retdata == 1){ alert('留言信息已提交成功!感谢您的宝贵意见。'); } } } // 异步请求 end return false; // 这里是为了方式submit点击后表单自动提交 // document.feedback_form.submit(); // 提交用户数据到后端action中的地址 } </script> </head> <body> <!-- 工作区,呈现给用户看的 --> <!-- 1.开始搭建脚手架 --> <p class="container_box"> <p class="up"> <h3 class="title">留言板</h3> <h5 class="subtitle">FEEDBACK</h5> </p> <p class="down"> <form name="feedback_form" action="/#" method="post" onsubmit="return false;"> <p class="input"> <input type="text" class="fl" name="name" placeholder="输入您的姓名" /> <input type="text" class="fr" name="tel" placeholder="输入您的联系方式" /> </p> <textarea class="content" cols="30" rows="10" name="nr"></textarea> <input type="submit" onclick="checkform()" value="提交您的留言" class="sub" /> </form> </p> </p> </body> </html>
<?php
include 'config.php';
// POST接收用户提交的数据
$nickname = !empty($_POST['nickname'])? addslashes(strip_tags($_POST['nickname'])):''; // 留言人名称
$tel = !empty($_POST['tel'])?addslashes(strip_tags($_POST['tel'])):''; // 留言人的联系方式
$content = !empty($_POST['content'])?addslashes(strip_tags($_POST['content'])):''; // 留言内容
$time = time(); // 当前系统时间,即用户留言时间
// 插入mysql语句
$sql = "INSERT INTO feedback (name, contact, content, addtime) VALUES ('{$nickname}', '{$tel}', '{$content}', '{$time}')";
// 立即执行mysql语句
$result = mysqli_query($mysqli, $sql); // 返回一个资源标识符,通常是数字
$insert_id = mysqli_insert_id($mysqli); // 返回数据表的自增长ID,比如新用户注册返回用户ID
// echo $insert_id; // 当你在调试的时候,你会发现echo是很好的帮手。
if($insert_id > 0){
// 如果入库成功,这里可以处理其他想要的逻辑
echo 1;
exit; // 退出程序,使其不再往下执行代码
}
// 这是错误的时候返回0
echo 0;
exit;
Copy after login
Remember to practice! Welcome to doodle in the comment area below! ~<?php include 'config.php'; // POST接收用户提交的数据 $nickname = !empty($_POST['nickname'])? addslashes(strip_tags($_POST['nickname'])):''; // 留言人名称 $tel = !empty($_POST['tel'])?addslashes(strip_tags($_POST['tel'])):''; // 留言人的联系方式 $content = !empty($_POST['content'])?addslashes(strip_tags($_POST['content'])):''; // 留言内容 $time = time(); // 当前系统时间,即用户留言时间 // 插入mysql语句 $sql = "INSERT INTO feedback (name, contact, content, addtime) VALUES ('{$nickname}', '{$tel}', '{$content}', '{$time}')"; // 立即执行mysql语句 $result = mysqli_query($mysqli, $sql); // 返回一个资源标识符,通常是数字 $insert_id = mysqli_insert_id($mysqli); // 返回数据表的自增长ID,比如新用户注册返回用户ID // echo $insert_id; // 当你在调试的时候,你会发现echo是很好的帮手。 if($insert_id > 0){ // 如果入库成功,这里可以处理其他想要的逻辑 echo 1; exit; // 退出程序,使其不再往下执行代码 } // 这是错误的时候返回0 echo 0; exit;
The above is the detailed content of Ajax asynchronous submission for PHP7 message board development. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

Compared with PHP7, PHP8 has some advantages and improvements in terms of performance, new features and syntax improvements, type system, error handling and extensions. However, choosing which version to use depends on your specific needs and project circumstances. Detailed introduction: 1. Performance improvement, PHP8 introduces the Just-in-Time (JIT) compiler, which can improve the execution speed of the code; 2. New features and syntax improvements, PHP8 supports the declaration of named parameters and optional parameters, making functions Calling is more flexible; anonymous classes, type declarations of properties, etc. are introduced.
