jquery - php ajax upload not working -


i want upload image using php , ajax jquery, got problem, first if on submit #uploadimage form, form process action not ajax whatever remove action.second if button change type button , javascript function .submit on click function ajax called $_files not set because image not save , there nothing output message

the view

<form id="uploadimage" action="http://localhost/pkh/mod/page/tpl/ajax_php_file.php" method="post" enctype="multipart/form-data">        <input type="file" name="file" id="file" required />        <input type="submit" value="upload" class="submit" /> </form> 

the js

<script type="text/javascript">     $(document).ready(function (e) {         $("#uploadimage").submit(function(e) {             e.preventdefault();             $("#message").empty();             $('#loading').show();             $.ajax({                 url: "http://localhost/pkh/mod/page/tpl/ajax_php_file.php", // url request send                 type: "post",             // type of request send, called method                 data: new formdata(this), // data sent server, set of key/value pairs (i.e. form fields , values)                 contenttype: false,       // content type used when sending data server.                 cache: false,             // unable request pages cached                 processdata:false,        // send domdocument or non processed data file set false                 success: function(data)   // function called if request succeeds                 {                     $('#loading').hide();                     $("#message").html(data);                 }             });         });          // function preview image after validation         $(function() {             $("#file").change(function() {                 $("#message").empty(); // remove previous error message                 var file = this.files[0];                 var imagefile = file.type;                 var match= ["image/jpeg","image/png","image/jpg"];                 if(!((imagefile==match[0]) || (imagefile==match[1]) || (imagefile==match[2])))                 {                     $('#previewing').attr('src','noimage.png');                     $("#message").html("<p id='error'>please select valid image file</p>"+"<h4>note</h4>"+"<span id='error_message'>only jpeg, jpg , png images type allowed</span>");                     return false;                 }                 else                 {                     var reader = new filereader();                     reader.onload = imageisloaded;                     reader.readasdataurl(this.files[0]);                     }             });         });         function imageisloaded(e) {             $("#file").css("color","green");             $('#image_preview').css("display", "block");             $('#previewing').attr('src', e.target.result);             $('#previewing').attr('width', '400px');             $('#previewing').attr('height', '252px');         };     });  </script> 

the php

<?php       if(isset($_files["file"]["type"]))     {         $validextensions = array("jpeg", "jpg", "png");         $temporary = explode(".", $_files["file"]["name"]);         echo $_files["file"]["type"];         echo $_files["file"]["name"];          $file_extension = end($temporary);         if ((($_files["file"]["type"] == "image/png") || ($_files["file"]["type"] == "image/jpg") || ($_files["file"]["type"] == "image/jpeg")         ) && ($_files["file"]["size"] < 100000)//approx. 100kb files can uploaded.         && in_array($file_extension, $validextensions))          {             if ($_files["file"]["error"] > 0)             {                 echo "return code: " . $_files["file"]["error"] . "<br/><br/>";             }             else             {                 if (file_exists("http://localhost/pkh/data/files/ktp/" . $_files["file"]["name"]))                  {                     echo $_files["file"]["name"] . " <span id='invalid'><b>already exists.</b></span> ";                 }                 else                 {                     $sourcepath = $_files['file']['tmp_name']; // storing source path of file in variable                     $targetpath = "http://localhost/pkh/data/files/ktp/".$_files['file']['name']; // target path file stored                     move_uploaded_file($sourcepath,$targetpath) ; // moving uploaded file                     echo "<span id='success'>image uploaded successfully...!!</span><br/>";                     $namafile = basename( $_files["file"]["name"]);                      $q = " update set tbl_applicant filektp = '{$namafile}' applicantid = '{$userinfo['applicantid']}'";                     $appobject->query($q);                      echo "<br/><b>file name:</b> " . $_files["file"]["name"] . "<br>";                     echo "<b>type:</b> " . $_files["file"]["type"] . "<br>";                     echo "<b>size:</b> " . ($_files["file"]["size"] / 1024) . " kb<br>";                     echo "<b>temp file:</b> " . $_files["file"]["tmp_name"] . "<br>";                 }             }         }         else         {             echo "<span id='invalid'>***invalid file size or type***<span>";         }     } ?> 


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -