ajax 提交表单方法1

这个学习来自:http://www.moke8.com/article-12258-1.html  

是基于织梦cms 的表单修改为ajax

首先是html的代码:

<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>手机:<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>

其次是基于jquery 的ajax

/*将此函数绑定到form提交的onclick上*/
function add_ajaxmessage(){
/*获取对应元素的val值*/
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"); //定义变量接收上面表单每项的值和几个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", //提交到后台文件 data: dataString, //传值 success: function(data) { alert(data);//成功打印PHP返回的值 } }); return false; }

php代码:

找到:if(!empty($dede_fields))
{
    //在里面加入判断语句,不判断也可以

    if($name==""){    
        echo "请填写您的姓名!";//注意:输出用echo输出,不要用showMsg();
        exit();
    }
}

找到:$goto = !empty($cfg_cmspath) ? $cfg_cmspath : '/';
      $bkmsg = '发布成功,请等待管理员处理...';
这两句,改成:echo "提交成功!";

删掉下面这一句:showmsg($bkmsg, $goto);
原文地址:https://www.cnblogs.com/thongyan/p/7478418.html