我正在尝试发布文件和按钮值并在php中处理它。
问题在我的php脚本中ha2e.php
条件if (isset($_FILES['file'])) {}
总是false
HTML:
<div id="htmlar2en" style="text-align:center">
<div class="container">
<div style="text-align:center">
<h1> Arabic to English HTML </h1>
<form method="post" enctype="multipart/form-data" target="iframe4" id = "htmla2e">
*.XLSX <input type="file" name="file" /> <input type="submit" id="submit4" name="submit4" value="HTML Arabic to English" />
</form>
<iframe name="iframe4" id="iframe4" src="" style="display:none" ></iframe>
</div>
</div>
</div>
jquery的:
$(document).ready(function() {
var extra_data = "This is more stuff to send";
var Bvalue = $(this).attr("value");
$('#submit4').click(function() {
$.ajax({
type: "POST",
url: "he2a.php",
data: {'form': $("#htmla2e").serialize(), 'other': extra_data, 'submit4': Bvalue},
success: function(msg) {
alert("Form Submitted: " + msg);
}
});
});
});
PHP:
<?php
if (isset($_FILES['file'])) {
header('Content-Type: text/html; charset=UTF-8');
echo '<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />';
require_once "simplexlsx.class.php";
require '../../Arabic.php';
$Arabic = new I18N_Arabic('Transliteration');
$xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
echo "df";
echo '<table border="1" cellpadding="3" style="border-collapse: collapse">';
$eng = array();
list($cols,) = $xlsx->dimension();
foreach( $xlsx->rows() as $k => $r) {
// if ($k == 0) continue; // skip first row
echo '<tr>';
for( $i = 0; $i < $cols; $i++)
{
if ($_POST['submit3'] == 'HTML English to Arabic')
{
$temp = $Arabic->en2ar($r[$i]);
}
else if ($_POST['submit4'] == 'HTML Arabic to Englis')
{
$temp = $Arabic->ar2en($r[$i]);
}
else
continue;
echo '<td>'.( (isset($r[$i])) ? $temp : ' ' ).'</td>';
}
echo '</tr>';
}
echo '</table>';
}
?>
答案 0 :(得分:1)
当你使用ajax请求时,$ _FILES当然是空的。
$('#submit4').click(function() {
$.ajax({
type: "POST",
url: "he2a.php",
data: {'form': $("#htmla2e").serialize(), 'other': extra_data, 'submit4': Bvalue},
success: function(msg) {
alert("Form Submitted: " + msg);
}
});
你应该将表单操作分配给你想要发布数据的网址,例如ha2e.php,fllow就是代码。
<form method="post" enctype="multipart/form-data" target="iframe4" id = "htmla2e" action="ha2e.php">
*.XLSX <input type="file" name="file" /> <input type="submit" id="submit4" name="submit4" value="HTML Arabic to English" />
</form>
最后我们使用chrome开发人员工具,我们将看到我们将文件发布到操作ha2e.php,如果action为空,则默认为当前url页面。
答案 1 :(得分:1)