在尝试将提交和上传结合在一个表单中时,我在上传时遇到了问题,但对于提交表单来说没问题。
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