0

I've html form. Now i'm trying to upload file using php with ajax request. But I'm getting following error message when I upload the file by php with ajax request. is it my ajax request problem or anything ?

Notice: Undefined index: file in D:\Software Installed\xampp\htdocs\wix\users\insert_galary.php on line 16

html form:

    <p><a href="javascript:void(null);" onclick="showDialog2();">Add more</a></p>                
    <div id="dialog-modal2" title="Upload your galary image" style="display: none;">
    <form method="post" action="insert_galary.php" id="myForm2" enctype="multipart/form-data" >
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td>Upload image</td>
    <td><input type="file" name="file" class="tr"/></td>
    </tr>
    </table>
    </form> 

    <span id="result2"></span>
    <script type="text/javascript">
    $("#sub2").click( function() {
    $.post( $("#myForm2").attr("action"), 
    $("#myForm2 :input").serializeArray(), 
    function(info){ $("#result2").html(info); 
    });
    clearInput();
    });

    $("#myForm2").submit( function() {
    return false;   
    });
    </script>
    </div>

        function showDialog2()
        {
            $("#dialog-modal2").dialog(
            {
                width: 610,
                height: 350,
                open: function(event, ui)
                {
                    var textarea = $('<textarea style="height: 276px;">');
                    $(textarea).redactor({
                        focus: true,
                        autoresize: false,
                        initCallback: function()
                        {
                            this.set('<p>Lorem...</p>');
                        }
                    });
                }
             });
        }

Php form:

$gid = mt_rand(100000, 999999); 
            $file =  $_FILES["file"]["name"];
            $type =  $_FILES["file"]["type"];
            $size =  ($_FILES["file"]["size"] / 1024);
            $temp =  $_FILES["file"]["tmp_name"];   
            $allowedExts = array("gif", "jpeg", "jpg", "png");
            $temp = explode(".", $_FILES["file"]["name"]);
            $extension = end($temp);    
            $galary_pic = $gid.".".$extension;  
            $galary_directory = "../galary_images/";

            $err =  array();

            if(isset($file))
            {
                    if(empty($file))                    
                        $err[] = "Upload your picture";             
                    elseif(!in_array($extension, $allowedExts))
                        $err[] = "Uploaded file must be gif, jpeg, jpg, png format";
                    elseif($size > 500)
                        $err[] = "Uploaded file must be within 500kb";                                      
            }

            if(!empty($err))
            {
                echo "<div class='error'>"; 
                foreach($err as $er)
                {
                    echo "<font color=red>$er.</font><br/>";                
                }
                echo "</div>";
                echo "<br/>";
            }
            else
            {

                echo "sql query";               

            }

Note: I already submit this question last day but I can't get any proper answer. Sorry about it

Alex
  • 97
  • 1
  • 2
  • 9
  • possible duplicate of [uploading files with jquery $.ajax and php](http://stackoverflow.com/questions/10260865/uploading-files-with-jquery-ajax-and-php) – jeroen Dec 09 '13 at 15:28
  • You're not uploading a file but just making a POST request without any file attached. If you're looking for an ajax file uploader, try jquery file upload here http://blueimp.github.io/jQuery-File-Upload/ – Ghigo Dec 09 '13 at 15:28
  • http://www.finalwebsites.com/forums/topic/php-ajax-upload-example – NemanjaLazic Dec 09 '13 at 15:30
  • @Ghigo may be your right. :) – Alex Dec 09 '13 at 15:31
  • You can use [jQuery Form](http://malsup.com/jquery/form/#file-upload) for files. – DJ22T Dec 09 '13 at 15:31

1 Answers1

0

You're not uploading a file but just making a POST request without any file attached. If you're looking for an ajax file uploader, try jquery file upload here

http://blueimp.github.io/jQuery-File-Upload

Ghigo
  • 2,312
  • 1
  • 18
  • 19