在尝试将提交和上载合并为一个表单时,我在上载时遇到了问题,但对于提交表单,这是没有问题的。
jquery+ajax:
$("#oqcsubmit").click(function() {
if($("#oqc").valid()) {
var params=$("#oqc").serialize();
$.ajax({
type:"post",
url:"doinput.php",
data:params,
cache :false,
async :false,
success : function() {
$(".dt").val("");
$(".stat").val("");
return this;
},
error : function() {
alert("Data failed to input.");
}
});
return false;
}
});
<form id="oqc" enctype="multipart/form-data" >
<input type="text" id="mod" name="mod" class="dt"/>
<input type="text" id="no" name="no" class="dt"/>
<input id="filename" name="uploadedfile" type="file" />
<input type="submit" id="oqcsubmit" value="Submit" />
<input type="hidden" name="action" value="oqcdata" />
</form>
PHP:
$dbc=mysql_connect(_SRV,_ACCID,_PWD) or die(_ERROR15.": ".mysql_error());
$db=mysql_select_db("QPL",$dbc) or die(_ERROR17.": ".mysql_error());
$target_path = "data/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
//print_r($_FILES);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
switch(postVar('action')) {
case 'oqcdata' :
oqcdata(postVar('mod'),postVar('no'));
break;
}
function oqcdata($mod,$no) {
$Model = mysql_real_escape_string($mod);
$Serial = mysql_real_escape_string($no);
//build query
$sql = "INSERT INTO OQC (Model, Serial) VALUES ('".$Model."','".$Serial."')";
echo $sql;
$result=mysql_query($sql) or die(_ERROR26.": ".mysql_error());
echo $result;
mysql_close($dbc);
如何在这个页面中正确地放置上传代码?所以两个都可以工作。
目录权限:
chmod 777 data
文件在提交(而不是发送)后留在表单中。
更新
在切换前移动上传代码后,我发现错误:
PHP Notice: Undefined index: uploadedfile
这意味着表单不发送
uploadedfile
值。检查参数后,不包括uploadedfile
。为什么会这样?甚至这个值也包含在表单中并使用.serialize()
。form data :
mod:KD-R321ED
no:177X1000 // where is the uploaded file value?
action:oqcdata
最佳答案
PHP Notice: Undefined index: uploadedfile
这意味着表单不发送uploadedfile值。检查参数后,不包括上传文件。为什么会这样?
不能通过普通的跨浏览器ajax上传文件,比如jquery使用的文件。句号,句号,故事结束。
如果必须在不刷新页面的情况下进行上载,通常的技巧是创建一个
iframe
并将表单提交给它。另一个技巧是使用实验性的File API,作为html5的一部分公开。这对你自己来说可能是一种痛苦,但如果你想全部靠手工完成的话,它应该能很好地工作。我强烈建议为此使用第三方文件上传小部件。我在Plupload方面很幸运,但有些人也推荐Uploadify。他们都可以选择使用flash或html5后端来执行上传,这也给了用户一个快乐的小进度表。