一、Form表單形式
標簽:form(聲明調用的方法action和請求方式method)
標簽:input(字段name敏沉;值value)
1.字段分開方式
.html文件提交數(shù)據(jù)
<form action="loginAction" method="post">
<input type="text" placeholder="用戶名" name="username">
<input type="password" placeholder="密碼" name="password">
<button type="submit">登 錄</button>
</form>
.php文件獲取數(shù)據(jù)
$username = I('username');
$password = I('password');
2.字段集合方式
.html文件提交數(shù)據(jù)
<form action="loginAction" method="post">
<input type="text" placeholder="用戶名" name="User['username']">
<input type="password" placeholder="密碼" name="User['password']">
<button type="submit">登 錄</button>
</form>
.php文件獲取數(shù)據(jù)
$user = I('User');
$username = user['username'];
$password = user['password'];
二琴许、AJAX方式
標簽:input(唯一標識id檐春;值value)
標簽:script(聲明方法function)
jQuery:$("#id").val()(獲取id的值)
jQuery:$.ajax(ajax請求方式寫法)
html樣式數(shù)據(jù)body
<input id="username" type="text" placeholder="用戶名">
<input id="password" type="password" placeholder="密碼">
<span id="submit" onclick="loginAction()" >登 錄</span>
script請求數(shù)據(jù)head
<script type="text/javascript">
function loginAction() {
/*修改標簽的元素內容*/
document.getElementById("submit").innerHTML="登錄中";
/*獲取標簽的值*/
var account = $("#username").val(), password = $("#password").val();
var data = {
'username' : account,
'password' : password
};
/* ajax請求*/
$.ajax({
type : "POST", // 請求方式
url : "__URL__/do_login", // 發(fā)送請求的地址
data : data, // 請求參數(shù)
async : true, // 異步請求
cache : false, // 緩存
dataType : "json", // 返回的數(shù)據(jù)類型
/*請求完成的回調函數(shù)(請求成功或失敗時均調用*/
complete : function(XMLHttpRequest, textStatus){
document.getElementById("submit").innerHTML="登 錄";
},
/*請求成功的回調函數(shù)*/
success : function(data, textStatus) {
if (data.code == 0) {
window.location.href = "{:U('Index/index')}";
}else{
console.log(data.message);
}
},
/*請求失敗的回調函數(shù)*/
error : function(XMLHttpRequest, textStatus, errorThrown){
},
});
}
</script>