我們可以借助jquery ajax提交dedecms自定義表單到后臺。
此例只做為參考,實際項目中根據(jù)自己的情況酌情修改。
html部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <form action="/plus/diy.php" enctype="multipart/form-data" method="post" id="diyform"> <input type="hidden" name="action" value="post" /> <input type="hidden" name="diyid" value="1" /> <input type="hidden" name="do" value="2" /> <input type="hidden" name="dede_fields" value="mail_name,text;mail_phone;mail_content,multitext" /> <input type="hidden" name="dede_fieldshash" value="86d34525cf75f8652022f6446152028d" /> <input type="hidden" name="setup" value="ajax" /> <div class="mail_rdd"> <div class="mail_content"> <table border="1" width="100%" class="mail_form"> <tr> <td width="50"> 姓名: </td> <td class="mail_input_bg1"> <input type="text" name="mail_name" id="mail_name" maxlength="20"/> </td> </tr> <tr> <td> 電話: </td> <td class="mail_input_bg1"> <input type="text" name="mail_phone" id="mail_phone" maxlength="20"/> </td> </tr> <tr style="height:240px"> <td> 留言: </td> <td class="mail_input_bg3"> <textarea name="mail_content" id="mail_content"></textarea> </td> </tr> </table> <div class="mail_button_bar"> <div class="mail_button confirm" onclick="send()"> </div> </div> </div> </div> </form> |
js部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.min.js"></script> <script> function send(){ $.ajax({ cache: true, type: "POST", url:"/plus/diy.php", data:$('#mail_form').serialize(),// 你的form id www.dedecms51.com網(wǎng) success: function(data) { if(data=="success"){ alert('發(fā)送成功!'); }else{ alert('發(fā)送失敗!'); } } }); } </script> |
/plus/diy.php 修改
找到
1 | showMsg($bkmsg,-1,0,3000); |
改成
1 2 3 4 5 6 7 8 9 | if($setup == 'ajax') { echo "success"; exit; } else { showMsg($bkmsg,-1,0,3000); } |
如果是gbk編碼的童鞋,還要修改/plus/diy.php,解決亂碼問題
找到
1 | $addvalue .= ", '".${$fieldinfo[0]}."'"; |
改成
1 | $addvalue .= ", '".iconv( "UTF-8", "gb2312//IGNORE" , ${$fieldinfo[0]})."'"; |
$.post方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var dataString = { 'name':$("#name").val(), 'tel':$("#tel").val(), 'setup':'ajax', 'action':'post', 'diyid':1, 'do':2, 'dede_fields':'name,text;tel,text', 'dede_fieldshash':'6b5fb808a4b9ea6d0603d983246a88a1', }; $.post("/plus/diy.php",dataString,function(result){ if(result=="1"){ alert('發(fā)送成功!'); $('#diyform').reset();//重置form }else{ alert('發(fā)送失敗!'); } }); |
版權聲明: 本站資源均來自互聯(lián)網(wǎng)或會員發(fā)布,如果侵犯了您的權益請與我們聯(lián)系,我們將在24小時內刪除!謝謝!