0

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 into journal set file='".$file."'";    
    $query=mysql_query($sql);               

?>

Only give me solution using serialize() only. If not so give me best solution.

3
  • 1
    Ajax image upload is not possible using form serialization.. you should use jquery's formData class.. check this answer here : stackoverflow.com/questions/5392344/… Commented Jun 4, 2015 at 7:54
  • @NishantSolanki..I have to post all form data with image and I have little bit knowledge of ajax. so how to post all data with image in ajax without serialize()?? Commented Jun 4, 2015 at 8:14
  • if the answer helped you out than please consider chosing as the right answe.. thanks Commented Jun 8, 2015 at 13:09

2 Answers 2

2

I have made some changes in your code.. you can use below code for 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' is your 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 that how you submit your form, so I have put a submit button
  2. You are using mysql functions, but they are officially deprecated now from php, you should use mysqli or PDO.
Sign up to request clarification or add additional context in comments.

Comments

0

Form id in HTML is addform and in ajax you are using #addformkey. You will have to change the id at one place. I doubt this will work though.

only give me solution using serialize () only

https://api.jquery.com/serialize/

The .serialize() method creates a text string in standard URL-encoded notation. It can act on a jQuery object that has selected individual form controls, such as <input>, <textarea>, and <select>: $( "input, textarea, select" ).serialize();

I doubt it can serialize a file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.