jquery - PHP image upload through Ajax serialize() -


file upload through ajax serialize():

<form id="addform" class="form-horizontal" enctype="multipart/form-data" > <div class="form-group">     <label for="link" class="control-label col-xs-3">image</label>     <div class="col-xs-6">         <input id="file" name="file" type="file"  class="form-control">     </div> </div> </form> 

ajax code using serialize():

$('#save11').click(function(){            $.ajax({                             type : "post",             url : "page/add-journal.php",             data :$('#addform').serialize(),             success : function(data)             {                 alert(data);                 window.location.href="home-page.php";                    }     }); }); 

here php code:

<?php     include '../dbconnection.php';     $tmp=$_files['file']['tmp_name'];     $serverpath="upload/".$_files['file']['name'];     $file=$_files['file']['name'];     move_uploaded_file($tmp,$serverpath);      $sql="insert journal set file='".$file."'";         $query=mysql_query($sql);                 ?> 

only give me solution using serialize() only. if not give me best solution.

i have made changes in code.. can use below code uploading images using ajax

<form id="addform" class="form-horizontal" enctype="multipart/form-data" >             <div class="form-group">                 <label for="link" class="control-label col-xs-3">image</label>                 <div class="col-xs-6">                     <input id="file" name="file" type="file"  class="form-control">                 </div>                 <input type="submit" name="save" value="save" />             </div>         </form>         <script>             $('#addform').submit(function(e) {                 e.preventdefault();                 var data = new formdata(this); // <-- 'this' form element                  $.ajax({                     url: 'page/add-journal.php',                     data: data,                     cache: false,                     contenttype: false,                     processdata: false,                     type: 'post',                     success: function(data) {                         alert(data);                         window.location.href = "home-page.php";                     }                  });             });         </script> 

note:

  1. you haven't provided how submit form, have put submit button
  2. you using mysql functions, officially deprecated php, should use mysqli or pdo.

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -