Home > Backend Development > PHP Tutorial > Code example to simply implement asynchronous page refresh through php+JQuery+Ajax

Code example to simply implement asynchronous page refresh through php+JQuery+Ajax

jacklove
Release: 2023-04-01 10:32:01
Original
1566 people have browsed it

The page is displayed as follows:

The code in JQueryAjax.html is as follows (using the simpler $.post)


<html>
<head>
<meta charset="UTF-8">
<title>JQueryAjax+PHP</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
</head>
<body>
    用户名:<input type="text" id="username" name="username" /><br>
    密码:<input type="password" id="password" name="password" /><br>
    <button type="button" class="butn">ajax提交</button><br>
    <span class="con"></span>
<script type="text/javascript">
$(document).ready(function(){
    $(".butn").click(function(){
        var username = $("#username").val();
        var password = $("#password").val();
        $.post(&#39;ajax.php&#39;,{name:username,pwd:password},function(data) {
            alert(data);
            $(".con").html(data);
        })
    })
})
</script>
</body>
</html>
Copy after login

ajax.php:

ajax.php
<?php 
echo &#39;用户名:&#39;,$_POST[&#39;name&#39;],&#39;,密码:&#39;,$_POST[&#39;pwd&#39;]."<br>";
//这里可以进行一些操作,比如数据库交互


echo "操作完毕";
?>
Copy after login

In non-json format, the background can only return strings. If you want to return an array in the background, you can use json format

For example, in JQueryAjax The code is modified to the following form:

<html>
<head>
<meta charset="UTF-8">
<title>JQueryAjax+PHP</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
</head>
<body>
    用户名:<input type="text" id="username" name="username" /><br>
    密码:<input type="password" id="password" name="password" /><br>
    <button type="button" class="butn">ajax提交</button><br>
    <span class="con"></span>
<script type="text/javascript">
$(document).ready(function(){
    $(".butn").click(function(){
        var username = $("#username").val();
        var password = $("#password").val();
        $.ajax({
             url: "ajax.php",  
             type: "POST",
             data:{name:username,pwd:password},
             dataType: "json",
             error: function(){  
                 alert(&#39;Error loading XML document&#39;);  
             },  
             success: function(data,status){//如果调用php成功 
                alert(status);
                alert(data);
                $(&#39;.con&#39;).html("用户名:"+data[0]+"密码:"+data[1]);
             }
        });
    })
})
</script>
</body>
</html>
Copy after login

ajax.php

<?php 
$name = $_POST[&#39;name&#39;];
$pwd = $_POST[&#39;pwd&#39;];
$array = array("$name","$pwd");
//这里进行一个些操作,比如数据库交互

echo json_encode($array);//json_encode方式是必须的
?>
Copy after login

The running effect is as follows:


##This article explains how to use php JQuery Ajax simply implements a code example of asynchronous page refresh. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

thinkphp redis queue related issues

Build a lamp on the server (Linux (CentOS7) Apache MySQL PHP)

Explanation of simple examples of Android PHP MYSQL development

The above is the detailed content of Code example to simply implement asynchronous page refresh through php+JQuery+Ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template