JavaScript Check if a file upload has been successful with AJAX / PHP check file upload size in javascript,check file upload in

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


?>
Answer:1

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

Answer:2

in a $.each() I do a AJAX-request: $.each(all, function(i,v) { $.ajax({ url: "/mycontroller/"+encodeURIComponent(v), success: function(data){ ...

in a $.each() I do a AJAX-request: $.each(all, function(i,v) { $.ajax({ url: "/mycontroller/"+encodeURIComponent(v), success: function(data){ ...

I have a modal form using bootstrap. The form contains some text inputs and a image input. I submit the form with ajax, and all data is received at the PHP file correctly. Alas, the image isn't ...

I have a modal form using bootstrap. The form contains some text inputs and a image input. I submit the form with ajax, and all data is received at the PHP file correctly. Alas, the image isn't ...

  1. submit form with javascript
  2. submit form with jquery
  3. submit form with ajax
  4. submit form with button
  5. submit form with link
  6. submit form with anchor
  7. submit form with a tag
  8. submit form with button outside form
  9. submit form with enter key
  10. submit form with get method
  11. submit form with php
  12. submit form with react
  13. submit form with input type button
  14. submit form with fetch
  15. submit form with curl
  16. submit form with selenium python
  17. submit form with enter key react
  18. submit form with value javascript
  19. submit form with multiple inputs
  20. submit form with python requests

Basically I'm trying to copy data from <input>s on one tab and then paste it into a form <input>s on another tab using a chrome extension. The part that I'm having trouble with figuring ...

Basically I'm trying to copy data from <input>s on one tab and then paste it into a form <input>s on another tab using a chrome extension. The part that I'm having trouble with figuring ...

I'v been following a few walkthroughs on how to implement d3 charts in an angular application. Basically, Im trying to implement the following d3 chart into my custom angular directive ('workHistory')....

I'v been following a few walkthroughs on how to implement d3 charts in an angular application. Basically, Im trying to implement the following d3 chart into my custom angular directive ('workHistory')....