2

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>";
}


?>
Filthy_Rich
  • 666
  • 5
  • 20

2 Answers2

0

You cannot use AJAX file upload with older browsers. You can still use an iframe if you want to. Related question: jQuery Ajax File Upload

Community
  • 1
  • 1
SalDev
  • 398
  • 3
  • 9
0

The error you're getting is because $_FILES['UploadFileField'] is not set. Therefore $UploadTmp is never set on your code, and when you get to this line: if(!$UploadTmp) - it fails ($UploadTmp is indeed undefined at this point). You may want to change it to if (!isset($UploadTmp)).

uri2x
  • 3,162
  • 1
  • 11
  • 25