濮阳杆衣贸易有限公司

主頁(yè) > 網(wǎng)站建設(shè) > 建站知識(shí) > 織夢(mèng)自定義表單通過ajax提交的實(shí)現(xiàn)方法

織夢(mèng)自定義表單通過ajax提交的實(shí)現(xiàn)方法

POST TIME:2017-11-13 01:10

自定義表單通過ajax判斷,提交不用跳轉(zhuǎn)頁(yè)面,提高用戶體驗(yàn)。具體方法如下:

html表單代碼部分,就提交按鈕改成botton,,添加onclick事件

表單代碼:

<form action="javascript:;" enctype="multipart/form-data" method="post">

<input type="hidden" name="action" value="post" />

<input type="hidden" name="diyid" value="1" />

<input type="hidden" name="do" value="2" />

<label>姓名:<input type="text" id="name" name="name" value="" /></label>

<label>電話:<input type="text" id="tel" name="tel" value="" /></label>

<label>手機(jī):<input type="text" id="iphone" name="iphone" value="" /></label>

<label>郵箱:<input type="text" id="email" name="email" value="" /></label>

<table>

<tr>

<td valign="top">留言:</td>

<td><textarea id="liuyan" name="liuyan"></textarea></td>

</tr>

</table>

<input type="hidden" name="dede_fields" value="name,textchar;tel,textchar;iphone,textchar;email,textchar;liuyan,multitext" />

<input type="button" class="submit fr" value="提 交" onclick="add_ajaxmessage()"/>

</form>

js代碼(這個(gè)代碼之前一定要引入jQuery庫(kù)):

function add_ajaxmessage(){

var name=document.getElementById("name");

var tel =document.getElementById("tel");

var iphone=document.getElementById("iphone");

var email=document.getElementById("email");

var liuyan=document.getElementById("liuyan");

//定義變量接收上面表單每項(xiàng)的值和幾個(gè)dede隱藏的input的值

var dataString = 'name='+ name.value + '&tel=' + tel.value + '&iphone=' + iphone.value + '&email=' + email.value + '&liuyan='+ liuyan.value +'&action=post'+ '&diyid=1&do=2&dede_fields=name,textchar;tel,textchar;iphone,textchar;email,textchar;liuyan,multitext';

$.ajax({

type: "POST",

url: "/plus/diy.php", //提交到后臺(tái)文件

data: dataString, //傳值

success: function(data) {

alert(data);//成功打印PHP返回的值

}

});

return false;

}

php代碼:

找到:if(!empty($dede_fields))

{

//在里面加入判斷語(yǔ)句,不判斷也可以

if($name==""){

echo "請(qǐng)?zhí)顚懩男彰?quot;;//注意:輸出用echo輸出,不要用showMsg();

exit();

}

}

找到:$goto = !empty($cfg_cmspath) ? $cfg_cmspath : '/';

$bkmsg = '發(fā)布成功,請(qǐng)等待管理員處理...';

這兩句,改成:echo "提交成功!";

刪掉下面這一句:showmsg($bkmsg, $goto);

以上就是跟版網(wǎng)總結(jié)的內(nèi)容,希望對(duì)大家有所幫助。



收縮
  • 微信客服
  • 微信二維碼
  • 電話咨詢

  • 400-1100-266
淳安县| 广宁县| 奉贤区| 枞阳县| 湾仔区| 盐池县| 庐江县| 巴楚县| 德阳市| 七台河市| 威宁| 雷波县| 博湖县| 沿河| 普洱| 屏南县| 林口县| 广州市| 鹿邑县| 娄底市| 古田县| 珠海市| 华坪县| 吕梁市| 海城市| 忻城县| 新龙县| 哈巴河县| 桓仁| 满城县| 岳普湖县| 南郑县| 福泉市| 鄢陵县| 鸡东县| 大厂| 日照市| 定襄县| 中方县| 望谟县| 历史|