我用post方法向服务器端发送了一个用户名和密码,想利用php实现服务器对该行为的响应,返回一个登录成功的字符串,该如何实现呢?客户端又该如何获得这个字符串呢?
回复讨论(解决方案)
去看w3school
写了个demo,输入username=fdipzone,password=123456服务器回返回成功,否则返回失败。
client.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="conent-type" content="text/html; charset=utf-8"> <title> client post</title> <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> </head> <body> <script type="text/javascript"> function fsubmit(){ $.post("server.php", { username: $("#username").val(), password: $("#password").val() },function(ret){ if(ret.success==true){ alert('login success'); }else{ alert('login fail'); } },'json'); } </script> <p>username:<input type="text" id="username"></p> <p>password:<input type="text" id="password"></p> <p><input type="button" value="submit" onclick="fsubmit()"></p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/xiazai/code/11102" title="极限网络办公Office Automation"><img
src="https://img.php.cn/upload/webcode/000/000/012/176494681157468.jpg" alt="极限网络办公Office Automation" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/xiazai/code/11102" title="极限网络办公Office Automation">极限网络办公Office Automation</a>
<p>专为中小型企业定制的网络办公软件,富有竞争力的十大特性: 1、独创 web服务器、数据库和应用程序全部自动傻瓜安装,建立企业信息中枢 只需3分钟。 2、客户机无需安装专用软件,使用浏览器即可实现全球办公。 3、集成Internet邮件管理组件,提供web方式的远程邮件服务。 4、集成语音会议组件,节省长途话费开支。 5、集成手机短信组件,重要信息可直接发送到员工手机。 6、集成网络硬</p>
</div>
<a href="/xiazai/code/11102" title="极限网络办公Office Automation" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div> </body></html>server.php
<?php$username = isset($_POST['username'])? $_POST['username'] : '';$password = isset($_POST['password'])? $_POST['password'] : '';$ret = array();if($username=='fdipzone' && $password=='123456'){ $ret['success'] = true;}else{ $ret['success'] = false;}echo json_encode($ret);?>ajax吗? 先说说你是怎样post到服务器的。
<script type="text/javascript">function submits(){// 实现账户与密码向服务器的发送// 服务器地址中传递的有参数a=1// 从服务器端接收数据num// 对num进行乘以2运算,再将结果发送给服务器端}</script><form id="loginform" action="./login.php?a=1" method="post" >username<input type="text" id="username" name="account">password<input type="password" id="pwd" name="password" ><input type="button" onclick="submits();"></form><?phpif($_POST['a'] == 1){$uName = $_POST['account'];$uPassword = $_POST['password'];// 比较数据库中信息(省略)// 向客户端传递数据num// 获得客户端发送来的运算结果}?> 就是为了实现客户端先向服务器发送数据,服务器再向客户端响应数据,最后客户端向服务器端发送对响应数据的运算结果。
总共是三轮数据交换过程,这个该怎么实现呢?
三个过程希望能通过点击一次登录按钮,就实现整个过程。
你就是按了一个按钮后,调用一个方法,然后方法里面再callback其他方法。直到流程走完。
可以这样写。
function fsubmit(){
$.post("server.php", { username: $("#username").val(), password: $("#password").val() },function(ret){
if(ret.success==true){
// 登入成功,这里可以调用第二个方法
doSecondFunc();
}else{
alert('login fail');
}
},'json');
}
function doSecondFunc(){
// do sth
如果还有,可以在这里再调用 thrid function
}
地址中可以加入参数吗,比如“server.php?a=1”,这样可以吗?
地址中可以加入参数吗,比如“server.php?a=1”,这样可以吗?
可以 但是 a 需要使用$_GET['a']来获取
<!DOCTYPE html><html><head> <title></title> <script src="http://code.jquery.com/jquery-latest.js"></script></head><body><script> $(document).ready(function () { $.post('test.php?gettest=1', {"test": "test"}, function (data) { alert(data.get); alert(data.post); }, 'json') })</script></body></html>$array = array( 'get' => $_GET['gettest'], 'post' => $_POST['test'],);echo json_encode($array);
搞了一下午,终于弄明白了,谢谢了









