Jscript / Ajax返回PHP而不重新加载页面

时间:2016-11-08 00:46:34

标签: javascript php jquery ajax phpexcel

完成新手但取得进步。请协助将表单数据发送到php并返回,而无需刷新页面。我知道这应该需要JQuery / AJAX。我尝试了多种方法(序列化/数据形式),但我很难过。目标是在form.php上填写一个列表框,其中包含使用PHPExcel从Excel文件获取的标题,然后通过lbox.php上传/ tmp(无fopen等)。以下所有代码均有效。 **我不打算使用按钮YET提交数据,这应该在选择上传文件时调用**

form.php的

<html>
<head> 
<title>Testing Page</title> 
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script = "text/javascript">

window.onload = startChecking;

function startChecking()
{
var  myVar = setInterval(doFunction, 1000);
}

function doFunction()
{
 var nme= document.getElementById("file");
 var x=document.getElementById("right");
    if(nme.value.length<1){
    document.getElementById("right").style.display="none";
    return false;
                 }
    document.getElementById("right").style.display="block";
//CODE NEEDED TO LOAD THE LISBOX THAT IS NOW APPEARING
    clearInterval(myVar);
}
</script>
</head> 
<body>

<form action="lbox.php" method="post" enctype="multipart/form-data" name="creationform" id="frmCreate"> 
Project Name:
<input type="text" name="YourName" value="Testing">
<br><br>
Choose your file: <br> 
<input type="file" name="file" id="file">
<input type="submit" name="Create" value="Create" /> 
</form>

<div id = "right" style="display:none">
Below should be the data from PHP loaded into the listbox
<BR>
<!-- //This code takes our array of headers and builds them into a listbox -->
<select size="15" name="decision1" multiple>
<?php foreach($myArray as $key => $value) { ?>
<option value="<?php echo $key ?>"><?php echo $value ?></option> -->
<?php }?>
</select>
</div>

</body> 
</html>

lbox.php

<?php
include 'PHPExcel/Classes/PHPExcel/IOFactory.php';

$objPHPExcel = PHPExcel_IOFactory::load($_FILES['file']['tmp_name']);

// This code creates our concatenation of column headers
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$val2 = null;
//    $highestRow         = $worksheet->getHighestDataRow(); // e.g. 10
    $highestRow         = 1; // e.g. 10
    $highestColumn      = $worksheet->getHighestDataColumn(); // e.g 'F'
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
    for ($row = 1; $row <= $highestRow; ++ $row) {
        for ($col = 0; $col < $highestColumnIndex; ++ $col) {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            $val2 .= $cell->getValue().',';
            $dataType = PHPExcel_Cell_DataType::dataTypeForValue($val2);

        }

    }

}

//The code below gets rid of the extra comma at the end
$val2 = substr($val2,0,strlen($val2)-1);
$val2 .= '';
echo '<br />';

// This code takes our concatenation and turns it into an array with the comma delimiter
$myString = $val2;
$myArray = explode(',',$myString);
print_r($myArray);
echo '<br />';

?>

谢谢,Mark。

0 个答案:

没有答案