網(wǎng)站建設(shè)中的表單的提交
1:提交時提交:
[/h
提交用于防止表單在提交前提交。如果錯誤,提交;如果是真的,不要提交!
[/h
檢查()是提交前的驗證功能。如果不符合要求,返回false將阻止表單提交。以下是示例文本:
復(fù)制代碼
公式=' 4。PHP ' method=' post ' on submit=' returncheck()' enctype=' multipart/form-data '//enctype設(shè)置文件上傳格式(該格式是上傳文件所必需的)
輸入id=' name ' type=' text ' placeholder='請輸入名稱'
input id=' paw ' type=' text ' placeholder='請輸入密碼'
input type=' file '
input type=' submit ' value=' submit '
/表格
復(fù)制代碼
復(fù)制代碼
腳本
check(){
var name=document . GetElementbyID(' name ');
var paw=document . GetElementBYID(' paw ');
if(name==''paw==''){
返回false
}否則{
返回真;
}
}
/腳本
復(fù)制代碼
2:使用ajax的異步提交:
1:創(chuàng)建一個XMLHttpRequest核心對象(老師要求它要用心寫)
復(fù)制代碼
函數(shù)getXhr(){
var xhr=null
if(窗口。XMlhttprequest){
xhr=新的XMlhttprequest;IE8以外的其他瀏覽器{
}否則{
}
formaction=' 3。PHP ' method=' post ' id=' my form ' name=' my form '
}
復(fù)制代碼
ii:建立連接并提交(帖子)
復(fù)制代碼
車身
username:input type=' text ' id=' user ' name=' user ' br
password:input type=' text ' id=' paw ' br
input type=' button ' value=' submit ' id=' BTN '
/form
varbtn=document . getelement byid(' BTN ');
/車身
腳本
BTN . onclick=function(){
var Xhr=GetXhr();
xhr.open('post ',' 3 . PHP ');//與server
xhr . setrequest header(' content-type ',' application/x-www-form-URL encoded ')建立連接;//設(shè)置post的請求頭
varuser=document . getelement by id(' user ')。價值;//獲取名稱值
varpaw=document . getelementbyid(' paw ')。價值;//獲取paw值
xhr . send(' user='+user+' PAW='+PAW);//客戶端向服務(wù)器
xhr . onreadystatechange=function(){//客戶端接受服務(wù)器的響應(yīng)
//以確保服務(wù)器響應(yīng)的數(shù)據(jù)完全發(fā)送
if(xhr.readyState==4){
//以確保請求成功。狀態(tài)代碼200為成功
if(xhr.status==200){
//接受服務(wù)器端數(shù)據(jù)
var data=xhr.responseText
//test
console.log(數(shù)據(jù));
}
}
}
}
/腳本
復(fù)制代碼
ii:建立連接并提交(get)
復(fù)制代碼
腳本
BTN . onclick=function(){
var Xhr=GetXhr();
BTN . onclick=function(){
var user=document . GetElementbyID(“用戶”)。價值;//獲取名稱值
xhr . send(' user='+user+' PAW='+PAW);//客戶端向服務(wù)器
varpaw=document . getelementbyid(' paw ')。價值;//獲取paw值
xhr.send(空);//客戶端向服務(wù)器發(fā)送請求
xhr . onreadystatechange=function(){//客戶端接受服務(wù)器端響應(yīng)
//以確保服務(wù)器響應(yīng)的數(shù)據(jù)完全發(fā)送
if(xhr.readyState==4){
//以確保請求成功。狀態(tài)代碼200為成功
if(xhr.status==200){
//接受服務(wù)器端數(shù)據(jù)
var data=xhr.responseText
//test
console.log(數(shù)據(jù));
}
}
}
}
}
復(fù)制代碼
注意:發(fā)送不能從發(fā)布或獲取中省略。get類型的發(fā)送是send(null ),無法向服務(wù)器發(fā)送請求數(shù)據(jù)
3。jQuery中的ajax提交:
比較常用的
$get(url、數(shù)據(jù)、回調(diào)、類型)
//網(wǎng)址:提交地址數(shù)據(jù):提交數(shù)據(jù)回調(diào):回調(diào)函數(shù)類型:指定服務(wù)器端返回的數(shù)據(jù)格式。默認為超文本標記語言格式。其他格式包括xml和json。
//帖子和它一樣,只是換成了帖子。
不常用
復(fù)制代碼
$ajax({
Url:'4.php ',//設(shè)置請求地址
type:'post ',//設(shè)置請求方法
async:'true ',//設(shè)置異步請求
data: { name: ' David ',age:' 20'},//發(fā)送請求的數(shù)據(jù) 唯一鍵:value
success :函數(shù)(數(shù)據(jù),文本狀態(tài)){//回調(diào)函數(shù)
console.log(數(shù)據(jù)),成功后;
},
error:函數(shù)(xmlhttprequest,textstatus,errorstown){//失敗后回調(diào)函數(shù)
console . log(text status);
},
})
復(fù)制代碼
福州優(yōu)橙互聯(lián)網(wǎng)(Fuzhou Youcheng Internet),一家專注于福州網(wǎng)站建設(shè)的網(wǎng)站制作公司,為客戶提供小程序制作,促進福州網(wǎng)絡(luò)的全網(wǎng)推廣和精準拓客。歡迎訪問http://www.cdsrny.cn