首页 > 代码库 > php+ajax+jq
php+ajax+jq
- <html>
- <head>
- <meta charset="UTF-8">
- <title>JQueryAjax+PHP</title>
- <script type="text/javascript" src=http://www.mamicode.com/"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>
<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(‘ajax.php‘,{name:username,pwd:password},function(data) { alert(data); $(".con").html(data); }) }) }) </script> </body> </html>
ajax.php:
- ajax.php
- <?php
- echo ‘用户名:‘,$_POST[‘name‘],‘,密码:‘,$_POST[‘pwd‘]."<br>";
- //这里可以进行一些操作,比如数据库交互
- echo "操作完毕";
- ?>
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=http://www.mamicode.com/"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>
<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>
php+ajax+jq
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。