• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php+jQuery+Ajax实现页面异步刷新功能

php 搞代码 4年前 (2022-01-22) 13次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了php+jQuery+Ajax实现页面异步刷新,,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

具体如下:

JQueryAjax.html中的代码如下(用的较为简单的$.post)

<html><head><meta charset="UTF-8&q<mark style="color:transparent">本%文来源gaodaimacom搞#^代%!码网@</mark>搞代gaodaima码uot;><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('ajax.php',{name:username,pwd:password},function(data) {   alert(data);   $(".con").html(data);  }) })})</script></body></html>

ajax.php

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

在非json格式下,后台只能返回字符串,如果想后台返回数组,可以采用json格式

例如将JQueryAjax中的代码修改为如下形式:

<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('Error loading XML document');     },     success: function(data,status){//如果调用php成功     alert(status);    alert(data);    $('.con').html("用户名:"+data[0]+"密码:"+data[1]);    }  }); })})</script></body></html>

ajax.php

<?php $name = $_POST['name'];$pwd = $_POST['pwd'];$array = array("$name","$pwd");//这里进行一个些操作,比如数据库交互echo json_encode($array);//json_encode方式是必须的?>

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP面向对象继承用法详解

php图像处理函数imagecopyresampled的用法

php die()与exit()的区别实例详解

以上就是php+jQuery+Ajax实现页面异步刷新功能的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php+jQuery+Ajax实现页面异步刷新功能

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址