zoukankan      html  css  js  c++  java
  • 表单异步提交数据

    <form name="form1">
    
        <input name="dsfurl" type="hidden" id="dsfurl">
            <table border="0" class="yy">
    
            <tbody>
                <tr>
                    <td align="center"><input name="name" id="name" type="text" class="shur"  value=""  placeholder="请输入您的姓名" ></td>
                </tr>
    
                <tr>
                    <td align="center"><input id="haoma" name="tel" type="text" class="shur" value="" placeholder="请输入联系电话"  ></td>
    
                </tr>
    
                <tr>
                    <td align="center" class="field1" style="height:30px">
                        <select type="text" name="yy"  id="dateinfo"  class="shur" style=" 280px;background: #fff;">
                            <option value=''>请选择您的预约日期</option>
                            <script language="javascript">
                                //构造当前日期对象
                                var myDate = new Date();
                                //获取年份
                                var year = myDate.getFullYear(); 
                                //获取当前月份
                                var mouth = myDate.getMonth() + 1;
                                //定义当月的天数;
                                var days;
                                //当月份为二月时,根据闰年还是非闰年判断天数
                                if (mouth == 2) {
                                   days = year % 4 == 0 ? 29 : 28;
                                }else if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 ||mouth == 12) {
                                //月份为:1,3,5,7,8,10,12 时,为大月.则天数为31;
                                days = 31;
                                }else {
                                //其他月份,天数为:30.
                                days = 30;
                                }                        
                                var syu=days-myDate.getDate();
                                    
                                if(syu < 7){
                                
                                    for(var i = 0; i <=7; i++) {
                                        time = (myDate.getMonth() + 1) + "" + myDate.getDate() + "";
                                        document.write("<option value='" + time + "'>" + time + "</option>");
                                        myDate.setDate(myDate.getDate() + 1);
                                    }
                                
                                }else{
                                    for(var i = 0; i <= syu; i++) {
                                        time = (myDate.getMonth() + 1) + "" + myDate.getDate() + "";
                                        document.write("<option value='" + time + "'>" + time + "</option>");
                                        myDate.setDate(myDate.getDate() + 1);
                                    }
                                }
                                                
                                        
                                </script>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="center"><textarea  name="bq" id="bq" cols="" rows="" class="shur2"  placeholder="请简要描述一下" ></textarea></td>
                </tr>
                <tr>
    
                    <td align="center"  ><input type="button" class="anniu" id="submit" value="点击提交预约信息" onclick="return checkgh(document.form1)";></td>
                </tr> 
                <tr>
    
                    <td align="center"> <p class="g-foot">温馨提示:工作人员将在一个工作日内与您联系!</p></td>
    
                </tr>
    
            </tbody>
        </table>
    </form>
    
    <script language="javascript">  
    document.getElementById('dsfurl').value=document.referrer;     
    </script>

    js代码:

    function checkgh(fm) {
        if (fm.name.value == '') {
            alert('请输入您的名字');
            window.setTimeout(function () {
                fm.name.focus();
            }, 0);
            return false;
        }
        if (fm.tel.value == '') {
            alert('请输入您的联系方式');
            window.setTimeout(function () {
                fm.tel.focus();
            }, 0);
            return false;
        }
        if (!isMobile(fm.tel.value)) {
            alert("请填入正确的联系号码");
            window.setTimeout(function () {
                fm.tel.focus();
            }, 0);
            return false;
        }
        submitForm(fm);
        return true;
    }
    
    
    var trueStr = '您已成功预约!';
    
    var falseStr = "很抱歉,预约失败!";
    
    function submitForm(form) {
        
        if (getCookie("submitcache") != null) {
            if (new Date().getTime() - getCookie("submitcache") < 1000 * 30) {
                alert("30秒内不能重复提交,请稍后再尝试提交!");
                return false;
                            
            }
        }
        var xmlHttp = createXmlHttp();
        if (!xmlHttp) {
            alert(falseStr);
            return 0;
        }
        var url = 'https//www.5z.com/gh22.php';
        var postData = "";
        var subtnchar = form.submit.value;
        for (var i = 0; i < form.length; i++) {
            postData += form.elements[i].name + "=" + form.elements[i].value + "&";
        }
        xmlHttp.open("POST", url, true);
          xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;&charset=utf-8");
    
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                if (xmlHttp.responseText.indexOf("ok") != -1) {
                    try {
                        document.cookie = "submitcache=" + new Date().getTime();
                    } catch (e) {}
                    alert(trueStr);
                    form.reset();
                } else if (xmlHttp.responseText.indexOf("err") != -1) {
                    alert(falseStr);
                    form.reset();
                } else {
                    var jsonString = xmlHttp.responseText;
                    alert(jsonString.substring(11, jsonString.length - 2));
                }
                form.submit.value = subtnchar;
                form.submit.disabled = false;
            }
        }
        form.submit.value = "数据提交中……";
        form.submit.disabled = true;
        xmlHttp.send(encodeURI(postData));
    
        return true;
    }
    
    function createXmlHttp() {
        var xmlHttp = null;
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    }
    
    function isMobile(s) {
        if (!(/^((1[34578]d{9})|(0d{10,11}))$/.test(s))) {
            return false;
        }
        return true;
    }
    
    function getCookie(name) {
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    }

    php文件执行的结果

    1,执行成功 返回status:ok;

    2,执行不成功,返回status:err;

    转载请注明出处: 欢迎留言或qq(1090413588)交流
  • 相关阅读:
    SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集
    SPOJ GSS3 Can you answer these queries III ——线段树
    SPOJ GSS2 Can you answer these queries II ——线段树
    SPOJ GSS1 Can you answer these queries I ——线段树
    BZOJ 2178 圆的面积并 ——Simpson积分
    SPOJ CIRU The area of the union of circles ——Simpson积分
    HDU 1724 Ellipse ——Simpson积分
    HDU 1071 The area ——微积分
    HDU 4609 3-idiots ——FFT
    BZOJ 2194 快速傅立叶之二 ——FFT
  • 原文地址:https://www.cnblogs.com/linyusong/p/7400478.html
Copyright © 2011-2022 走看看