Check if a file upload has been successful with AJAX / PHP

时间:2015-09-11 15:15:38

标签: javascript php jquery ajax

So I'm developing a website for a friend of mine and have encountered an issue with the file upload field within a form. The form consists of several text inputs and the file field itself. Variables are created within a file called show.js, which checks my other file, show_parse.php to see if the data has been input correctly, and then retrieve the appropriate error message.

The issue:

The error messages are being spat out accordingly when either the .js file is not included, which leads to a redirect to show_parse.php (which I am trying to avoid by spitting out the errors into the #message div without page refresh).

I know that this script works for text input fields. I have trialed and error'd it on numerous occasions. I know that the issue is with the AJAX and checking to see if the file input is empty or not.

The error:

WITH the .js file included, it returns all of the error messages for the text input files, however when it reaches the error message for the file input I recieve (Notice: Undefined variable: UploadTmp ...).

Any light that could be shed on the matter would be much appreciated.

Thanks in advance, Rich

show.js

$(document).ready(function(){
    $("#submit").click(function(){

        var name = $("#name").val();
        var description = $("#description").val();
        var url = $("#url").val();
        var UploadFileField = $("#UploadFileField").val();

        $.ajax({
        type: "POST",
        url: "show_parse.php",
        data: {name:name, description:description, url:url, UploadFileField:UploadFileField},
        success: function(html){    


            if(html=='1') {
            window.location="index.php";
            } else {
            $("#message").html(html).css('color','#be4343');}},
            beforeSend:function(){
            $("#message").html("<span style='color:green ! important'>Creating episode</br></br>");
}
});
return false;
});
});

show_parse.php

<?php 

        include('connect.php');        

$name = $_POST['name'];
$name = mysql_real_escape_string($name);
$description = $_POST['description'];
$description = mysql_real_escape_string($description);
$url = $_POST['url'];
$url = mysql_real_escape_string($url);

if(isset($_FILES['UploadFileField'])){
$UploadName = $_FILES['UploadFileField']['name'];
$UploadName = mt_rand(100000, 999999).$UploadName;
$UploadTmp = $_FILES['UploadFileField']['tmp_name'];
$UploadType = $_FILES['UploadFileField']['type'];   

$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);

}
if(strlen($name) < 1){
echo "You must enter a name for the show.";
exit();
}
if(strlen($name) > 55){
echo "The name for the show is too long.";  
exit();
}
if(strlen($description) < 1){
echo "You must enter a description for this episode.";  
exit();
}
if(strlen($description) > 500){
echo "The maximum word-count for a description is 500 characters."; 
exit();
}
if(strlen($url) < 1){
echo "Please enter the URL link for the episode (to YouTube etc)";  
exit();
}
if(!$UploadTmp){ // This is checked correctly when the show.js script is not included.
echo "You need to select a thumbnail image.";
exit();
}
move_uploaded_file($UploadTmp, "images/$UploadName");
$sql = "INSERT INTO shows (name, description, image, link, ep_num) SELECT '".$name."', '".$description."', '".$UploadName."', '".$url."', count(*)+1 from shows WHERE name='".$name."'";
$res = mysql_query($sql);

if($res = TRUE){
echo "<span style='color:green ! important'>Success</br></br>";
}


?>

2 个答案:

答案 0 :(得分:0)

您无法使用旧版浏览器上传AJAX文件。如果您愿意,您仍然可以使用iframe。 相关问题:jQuery Ajax File Upload

答案 1 :(得分:0)

您收到的错误是因为未设置$UploadTmp。因此,if(!$UploadTmp)永远不会在您的代码上设置,当您到达此行时:$UploadTmp - 它失败(此时if (!isset($UploadTmp))确实未定义)。您可能希望将其更改为set echo off set verify off set feedback off set linesize 256 set pagesize 0 set term off SPOOL 'myscript.sql' SELECT * FROM USER_TAB_COLS WHERE ROWNUM < 10; SPOOL OFF set colsep , SPOOL myfile.csv @myscript.sql SPOOL OFF