从select中获取多个值并通过ajax将其发布到php数组

时间:2016-10-26 18:47:21

标签: javascript php html ajax

我已经整合了bootstrap dualListbox插件,它允许用户从一堆选项中挑选,然后提交他们的金额。

这会使用<select> multiple="multiple"属性。

我面临的问题是这些值未发布到ajax页面。

这是我来自呼叫页面的代码。

<form name="exportForm" action="" id="exportForm" method="POST" role="form">
              <div class="row">

                  <input type="radio" name="exportType" id="exportCSV" value="csv" checked/>CSV <br/>
                  <input type="radio" name="exportType" id="exportPDF" value="csv" />PDF<br/>
                  <input type="radio" name="exportType" id="exportHTML" value="csv" />HTML
              </div>
              <br/>
              <legend>Choose Fields To be Exported</legend>
              <div class="row help">
                  <div class="col-md-12">
                      <ul>
                          <li>Click on the * field to input new options</li>
                          <li>Blah Blah</li>
                          <li>Blah blah</li>
                      </ul>
                  </div>
              </div>
              <div class="row">

                      <select multiple="multiple" name="exportColList[]" id="exportList">
                          <option value="mem_branch_code">Branch Code</option>
                          <option value="mem_employer_id"> Employer</option>
                          <option value="mem_name"> Name </option>
                          <option value="mem_surname"> Surname</option>
                          <option value="mem_married"> Relationship Status</option>
                          <option value="mem_status_type"> Member Status</option>
                          <option value="mem_skipped_idv"> IDV Status</option>
                          <option value="mem_skipped_avsr"> AVSR Status</option>
                          <option value="mem_town"> Town</option>
                          <option value="mem_suburb"> Suburb</option>
                          <option value="mem_mobile_number"> Mobile</option>
                          <option value="mem_email"> E-mail</option>
                          <option value="mem_addr1"> Address Line 1</option>
                          <option value="mem_addr2"> Address Line 2</option>
                          <option value="mem_reg_date"> Creation Date</option>
                          <option value="mem_scp_fee_date"> SCP Fee Date</option>
                      </select>
                  <div class="col-md-12" style="text-align: right; margin-top: 6px;" >
        <button class="btn btn-success" name="frmSubmit" type="submit" onclick="exportCsv(this)" ><span class="glyphicon glyphicon-download-alt" ></span> Export</button>
    </div

              </div>
          </div>
          <input type="hidden" name="export" id="export" value="0" />
          </form>

使用javascript看起来如下:

function exportCsv(button) {
        document.getElementById('export').value = 1;
        var btnText = button.innerHTML;
        button.innerHTML = 'Exporting...';
        button.disabled = true;
        var data = new FormData(document.getElementById('exportForm'));
        $.ajax({
            url: '/path/to/backend/ajax/ajax_exports.php',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            type: 'POST',
            success: function (data) {
                alert (data);
                button.innerHTML = btnText;
                button.disabled = false;
                document.getElementById('export').value = 0;
                var jsonresp = JSON.parse(data);
                if (jsonresp.rescode == 1) {
                    BootstrapDialog.show({
                        title: 'Export - Success',
                        type: BootstrapDialog.TYPE_SUCCESS,
                        message: "Export successfull"+jsonresp.msg,
                        buttons: [{
                                label: 'Close',
                                cssClass: 'btn-primary',
                                action: function (dialog) {
                                    dialog.close();
                                }
                            }, {
                                label: 'Open File',
                                cssClass: 'btn-success',
                                action: function (dialog) {
                                    document.getElementById('downloadframe').src = "/path/to/file_manager/exports/export.php?file="+jsonresp.msg;
                                    dialog.close();
                                }
                            }]
                    });
                } else {
                    BootstrapDialog.show({
                        title: 'Export - Warning',
                        type: BootstrapDialog.TYPE_WARNING,
                        message: jsonresp.msg,
                        buttons: [{
                                label: 'Ok',
                                action: function (dialog) {
                                    dialog.close();
                                }
                            }]
                    });
                }
            }
        });
    }

php部分如下所示:

if ($_POST['export'] == 1) {

    $headerValues = array('mem_branch_code'=>'Branch Code','mem_employer_id'=>'Employer','mem_name'=>'Name','mem_surname'=>'Surname','mem_married'=>'Relationship Status','mem_status_type'=>'Member Status','mem_skipped_idv'=>'IDV Status','mem_skipped_avsr'=>'AVSR Status','mem_town'=>'Town','mem_suburb'=>'Suburb','mem_mobile_number'=>'Mobile','mem_email'=>'E-mail','mem_addr1'=>'Address Line 1','mem_addr2'=>'Address Line 2','mem_reg_date'=>'Creation Date','mem_scp_fee_date'=>'SCP Fee Date');

    $filename = $_SESSION['userid'] . "_client_export_" . date('m-d-Y') . "_.csv";
    $path = "../../file_manager/exports/";
    $outputfile = fopen($path . $filename, "w") or die(json_encode(array('rescode' => -1, 'msg' => 'Unable to write to export file, if this persists pleae contact support.')));

    $headerArray = array();

    foreach ($_POST['exportColList'] as $col) {
        $headerArray[] = $headerValues[$col];
    }

    $header = implode(",", $headerArray);

    fwrite($outputfile, $header);

    while (!$resultset->eof()) {

        $line = array();

        foreach ($_POST['exportColList'] as $col) {
            if($col == 'mem_branch_code') {
                $line[] = $resultset->valueof($col);
            }elseif($col == 'mem_employer_id') {
                $sel_employer_name = new CGenRs("SELECT * FROM bfs_employer WHERE emp_id = '" . $resultset->valueof('mem_employer_id') . "' ", $cao);
                $sel_employer_name->first();
                $line[] = $sel_employer_name->valueof('emp_name');
            } elseif($col == 'mem_name') {
                $line[] = $resultset->valueof($col);
            }elseif($col =='mem_surname'){
                $line[] = $resultset->valueof($col);
            }elseif($col =='mem_married'){
                    if($resultset->valueof('mem_married') !='t'){
                        $line[] = 'Single';
                    }else{
                        if($resultset->valueof('married_in_cop') != 't' ){
                            $line[] = 'Married Out of COP';
                        }else{
                            $line[]='Married in COP';
                        }
                    }

            }elseif($col == 'mem_status_type'){
                $sel_status_type = new CGenRs("SEELCT * FROM table where type_id = '".$resultset->valueof("mem_status_type")."' ",$cao);
                $sel_status_type->first();

                $line[]= $sel_status_type->valueof('type_desc');
            }elseif($col === 'mem_skipped_idv'){
                if($resultset->valueof('mem_skipped_idv') !='t'){
                    $line[]= "Completed";
                }else{
                    $line[]="Skipped";
                }
            }else if($col == 'mem_skipped_avsr'){
                if($resultset->valueof('mem_skipped_avsr') != 't'){
                    $line[]="Completed";
                }else{
                    $line[]="Skipped";
                }
            }elseif($col=='mem_town') {
                $line[]=$resultset->valueof($col);
            }elseif($col=='mem_suburb'){
                $line[]=$resultset->valueof($col);
            }elseif($col=='mem_mobile_number'){
                $line[]=$resultset->valueof($col);
            }elseif($col=='mem_email'){
                $line[]=$resultset->valueof($col);
            }elseif($col=='mem_addr1'){
                $line[]=$resultset->valueof($col);
            }elseif($col=='mem_addr2'){
                $line[]=$resultset->valueof($col);
            }elseif($col=='mem_reg_date'){
                $line[]=date_format(date_create($resultset->valueof('mem_reg_date')), 'Y-m-d H:i');
            }elseif($col=='mem_scp_fee_date'){
                $line[]=date_format(date_create($resultset->valueof('mem_scp_fee_date')), 'Y-m-d H:i');
            }


        }

        fputcsv($outputfile, $line);
        $resultset->next();
    }
    fclose($outputfile);

    echo json_encode(array('rescode' => 1, 'msg' => $filename));

    die;
}

调试后,我发现$header数组以及$line数组为空。

有关如何改善这种情况的任何建议?

0 个答案:

没有答案