php / jquery:从表单数据创建Csv而不提交

时间:2017-10-23 18:03:48

标签: php jquery ajax csv form-submit

我有一个简单的html表单,如下所示。我正在使用PHP,该网站托管到服务器。用户访问它并提交他们的信息。我想将他们的数据保存到他们自己的PC(客户端PC)作为csv文件,无论网络连接丢失或可用。

<?php 

     error_reporting(0);
     require('commonVar.php');

     if(@$_POST)
     {
    $stdloggedid=$_POST['stdloggedid'];
    $stdloggedinBoard=$_POST['stdloggedinBoard'];
    $stdloggedinChest=$_POST['stdloggedinChest'];
    $stdloggedinName=$_POST['stdloggedinName'];  

    //file
    if(isset($_POST['csv_btn']))
    {

        // csv create 
            $answer_ids_str = '';
            $setdetail_str = '';
            $qs_id_str = '';

            foreach($setqs_id as $key=>$did)
            {

                if($_POST['answer_ids'.$did]!=''){

                    $answer_ids_str != "" && $answer_ids_str .= "|";
                    $setdetail_str != "" && $setdetail_str .= "|";
                    $qs_id_str != "" && $qs_id_str .= "|";

                    $answer_ids_str .= $_POST['answer_ids'.$did];
                    $setdetail_str .= $setdetail[$key];
                    $qs_id_str .= $did;
                }


            }

            $exam_type='it_vit';
            $set_id = $_POST['set_id'];
            $ans = $_POST['ans'];
            $filename="it_vit";
            header("Content-Type: application/vnd.ms-excel");
            $csv_filename = $stdloggedinName.'_'.$filename."_".date("Y-m-d_H-i",time()).".csv";
            if (!file_exists('D:/result/')) {
                    mkdir('D:/result/', 0777, true);
            }

            $fd = fopen ('D:/result/'.$csv_filename, "w");

            $fileContent="qtype,qs_set_id,qs_detail_id,question_id,answer_ids,boardno,chestno,applicant_id,name,exam_date_time\n";
            $fileContent.= "".$exam_type.",".$setid.",".$ans.",".$stdloggedinBoard.",".$stdloggedinChest.",".$stdloggedid.",".$stdloggedinName."\n";
            $fileContent=str_replace("\n\n","\n",$fileContent);
            header("content-disposition: attachment;filename=$csv_filename");
            echo $fileContent;

            fputs($fd, $fileContent);

            fclose($fd);
            exit();
            // csv create end

    }else{
        // insert into db
    }

 }

?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> Assesment </title>


</head>

<body>

<div>
 <section class="content-header" style="background-color:#ED1C24;color:#fff;position:fixed;width:100%;z-index:1000;padding-bottom:7px;border-bootom:2px solid #ccc">
      <h1 style=" text-transform:uppercase;font-size:18px " >
       <span><img src="../images/nhdlogo.png" width="40" style="margin-right:15px"/></span>
       VIT Assesment Test

      </h1>
</section>

<!-- Main content -->
<section class="content" >
      <div>&nbsp;</div>
      <div>&nbsp;</div>
      <div class="row">
       <!-- <i class="fa fa-refresh fa-spin"></i>-->
        <!-- left column -->
        <div class="col-md-12">
            <div class="box box-success">


                <form id="userform"  method="post" enctype="multipart/form-data">
                     <input name="set_id" type="hidden" id="set_id" value="<?=$set_id?>">
                     <input name="ans" type="text" id="ans" value="">
                        <div class="box-header with-border" id="allqs">

                            <div class="col-lg-12 box-footer" id="sbtbtn">
                                     <input type="submit" name="csv_btn" id="csv_click" class="btn btn-primary" value="csv" /> 
                                    <input type="submit" name="sarticle" id="submit_click" class="btn btn-primary" value="Submit" /> &nbsp;**Click Submit If you answer to all the questions.

                            </div>

                        </div>
                </form>
             </div>
        </div>
     </div>
 </section>

在此代码中,我想将文件保存在每个用户PC的D盘中。当wifi连接正常时,它工作正常。但不知何故,如果在将表单显示给用户的Pc后连接丢失,那么当他们按csv btn时如何创建和保存csv?

我怎么能这样做?如果wifi连接丢失,当用户按下提交按钮时,他的浏览器会显示“未找到服务器”。如果我使用ajax和form.serilize()那么我怎样才能在本地PC上创建和下载csv文件?

0 个答案:

没有答案