手记

php中实现数据存储在excel

index.php为入口,为一个表单,结构比较简单。只粘贴js代码部分:


function onext(){    var isMobile=/^(?:13\d|15\d|18\d)\d{5}(\d{3}|\*{3})$/;      var isPhone=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;    var iname=$('input[name=iname]').val();    var phone=$('input[name=phone]').val();    var wp=$('input[name=workplace]').val();    if(iname=='' || phone=='' || wp==''){        showDialog('alert', '请检查姓名、电话号码或者工作单位有没有填写', "在线登记", "430");        return false;    }    if(phone!='' && !isMobile.test(phone) && !isPhone.test(phone)){        showDialog('alert', '你填写的电话号码格式不正确。', "在线登记", "430");        return false;    }    document.ad.submit();}

引用了一个jquery.showdialog的函数


function detectMacXFF() {    var userAgent = navigator.userAgent.toLowerCase();    if(userAgent.indexOf("mac") != -1 && userAgent.indexOf("firefox") != -1) {        return true;    }}function in_array(needle, haystack) {    if(typeof needle == "string" || typeof needle == "number") {        for(var i in haystack) {            if(haystack[i] == needle) {                return true;            }        }    }    return false;}function sd_load(sd_width) {    if(sd_width) {        $("#SD_window").css("width", sd_width + "px");    }    var sd_top = ($(window).height() - $("#SD_window").height()) / 2 + $(document).scrollTop();    if(sd_top < 0) {        sd_top = 0;    }    var sd_left = ($(window).width() - $("#SD_window").width()) / 2;    if(sd_left < 0) {        sd_left = 0;    }    $("#SD_window").css("top", sd_top);    $("#SD_window").css("left", sd_left);}function sd_remove() {    $("#SD_close,#SD_cancel,#SD_confirm").unbind("click");    $("#SD_window,#SD_overlay,#SD_HideSelect").remove();    if(typeof document.body.style.maxHeight == "undefined") {        $("body","html").css({height: "auto", width: "auto"});    }}function showDialog(mode, msg, t, sd_width) {    var sd_width = sd_width ? sd_width : 400;    var mode = in_array(mode, ['confirm', 'window', 'info', 'loading']) ? mode : 'alert';    var t = t ? t : "提示信息";    var msg = msg ? msg : "";    var confirmtxt = confirmtxt ? confirmtxt : "确定";    var canceltxt = canceltxt ? canceltxt : "取消";    sd_remove();    try {        if(typeof document.body.style.maxHeight === "undefined") {            $("body","html").css({height: "100%", width: "100%"});            if(document.getElementById("SD_HideSelect") === null) {                $("body").append("<iframe id='SD_HideSelect'></iframe><div id='SD_overlay'></div>");            }        } else {            if(document.getElementById("SD_overlay") === null) {                $("body").append("<div id='SD_overlay'></div>");            }        }        if(mode == "alert") {            if(detectMacXFF()) {                $("#SD_overlay").addClass("SD_overlayMacFFBGHack");            } else {                $("#SD_overlay").addClass("SD_overlayBG");            }        } else {            if(detectMacXFF()) {                $("#SD_overlay").addClass("SD_overlayMacFFBGHack2");            } else {                $("#SD_overlay").addClass("SD_overlayBG2");            }        }        $("body").append("<div id='SD_window'></div>");        var SD_html;        SD_html = "";        SD_html += "<table cellspacing='0' cellpadding='0'><tbody><tr><td class='SD_bg'></td><td class='SD_bg'></td><td class='SD_bg'></td></tr>";        SD_html += "<tr><td class='SD_bg'></td>";        SD_html += "<td id='SD_container'>";        SD_html += "<h3 id='SD_title'>" + t + "</h3>";        SD_html += "<div id='SD_body'><div id='SD_content'>" + msg + "</div></div>";        SD_html += "<div id='SD_button'><div class='SD_button'>";        SD_html += "<a id='SD_confirm'>" + confirmtxt + "</a>";        SD_html += "<a id='SD_cancel'>" + canceltxt + "</a>";        SD_html += "</div></div>";        SD_html += "<a href='javascript:;' id='SD_close' title='关闭'></a>";        SD_html += "</td>";        SD_html += "<td class='SD_bg'></td></tr>";        SD_html += "<tr><td class='SD_bg'></td><td class='SD_bg'></td><td class='SD_bg'></td></tr></tbody></table>";        $("#SD_window").append(SD_html);        $("#SD_confirm,#SD_cancel,#SD_close").bind("click", function(){            sd_remove();        });        if(mode == "info" || mode == "alert") {            $("#SD_cancel").hide();            $("#SD_button").show();        }        if(mode == "window") {            $("#SD_close").show();        }        if(mode == "confirm") {            $("#SD_button").show();        }        var sd_move = false;        var sd_x, sd_y;        $("#SD_container > h3").click(function(){}).mousedown(function(e){            sd_move = true;            sd_x = e.pageX - parseInt($("#SD_window").css("left"));            sd_y = e.pageY - parseInt($("#SD_window").css("top"));        });        $(document).mousemove(function(e){            if(sd_move){                var x = e.pageX - sd_x;                var y = e.pageY - sd_y;                $("#SD_window").css({left:x, top:y});            }        }).mouseup(function(){            sd_move = false;        });        $("#SD_body").width(sd_width - 50);        sd_load(sd_width);        $("#SD_window").show();        $("#SD_window").focus();    } catch(e) {        alert("System Error !");    }}function showInfo(msg, fn, timeout) {    showDialog("info", msg);    $("#SD_confirm").unbind("click");    if(fn && timeout) {        st = setTimeout(function(){            sd_remove();            fn();        }, timeout * 1000);    }    $("#SD_confirm").bind("click", function(){        if(timeout) {            clearTimeout(st);        }        sd_remove();        if(fn) {            fn();        }    });}function showWindow(title, the_url, sd_width) {    var sd_width = sd_width ? sd_width : 400;    $.ajax({        type        : "GET",        dataType    : "html",        cache       : false,        timeout     : 10000,        url         : the_url,        data        : "isajax=1",        success     : function(data){            showDialog("window", data, title, sd_width);        },        error       : function(data){            showDialog("alert", "读取数据失败");        },        beforeSend  : function(data){            showDialog("loading", "正在读取数据...");        }    });}function showConfirm(msg, fn) {    showDialog("confirm", msg);    $("#SD_confirm").unbind("click");    $("#SD_confirm").bind("click", function(){        if(fn) {            fn();        }    });}

提交后的处理程序i.php


<?php//dataheader('Content-Type:text/html;charset=utf-8');$iname=iconv("UTF-8","gbk",trim($_POST['iname']));$phone=iconv("UTF-8","gbk",trim($_POST['phone']));$workplace=iconv("UTF-8","gbk",trim($_POST['workplace']));$ksobject=iconv("UTF-8","gbk",$_POST['ksobject']);//date_default_timezone_set('Etc/GMT-8');     //这里设置了时区$time= date("Y-m-d H:i:s");$fp = fopen("ksdata.csv","a"); //打开csv文件,如果不存在则创建//$data_1 = array($name,$cartype,$buycar,$phone,$email,$time,$carnumber);$data_1=array($iname,$phone,$workplace,$ksobject);$data_1 = implode(",",$data_1); // 用 ' 分割成字符串$data_str =$data_1."\r\n"; //加入换行符$p=fwrite($fp,$data_str); // 写入数据//echo $p;if($p!=null&&$p!=""){    //echo "ok...";}else{    echo "<script>alert('提交失败!');location.href='index.php';</script>";}fclose($fp); //关闭文件句柄// echo "提交成功";echo "<script>alert('提交成功!');location.href='index.php';</script>";  ?>


0人推荐
随时随地看视频
慕课网APP