我在尝试上传任何内容之前都有代码来验证我的表单。
我认为我已经错误地嵌套了if语句,这就是它无法正常工作的原因。
我已将我的PHP和HTML表单代码放在一个小提琴中,供有意愿看的人使用:https://jsfiddle.net/1htqcyun/
以下是PHP的片段:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//if the year is left empty then show error, if not then test_input
if (empty($_POST["year"])) {
$yearErr = "*enter a year";
} else {
$year = test_input($_POST["year"]);
}
//if the description is left empty then show error
if (empty($_POST["description"])) {
$descriptionErr = "*enter a description";
} else {
$description = test_input($_POST["description"]);
}
// If the date has not been selected in the drop down, and the select is left at "Please Select" value = 0
if(isset($_POST['dateID']) && $_POST['dateID'] == '0'){
$decadeErr = "*select a decade";
}
else {
$dateID = test_input($_POST["dateID"]);
}
答案 0 :(得分:0)
仅供参考 - 使用jsFiddle作为PHP示例没有帮助。试试http://phpfiddle.org/
我认为问题是在代码的第10行过早调用$_FILE
变量:
$target_file = $target_dir . basename($_FILES["file_upload"]["name"]);
如果您第一次运行该脚本,则会失败。我会改为:
if(!empty($_POST)){
if(!empty($_FILES["file_upload"])){
$target_file = $target_dir . basename($_FILES["file_upload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["uploaded"]["tmp_name"]);
}
$year = $description = $dateID = "";
$yearErr = $descriptionErr = $decadeErr = "";
// continue the rest of your $_POST handling...
}
这种方式如果没有发布任何内容,就像第一次运行时一样,脚本不会尝试处理尚未发布的项目。