2

HTML:

<form id="imageform" method="post" enctype="multipart/form-data" >
    Upload image <input type="file" name="photoimg" id="photoimg" />
</form>
<div id='preview'></div>

JavaScript:

$('#photoimg').change(function() {
    $("#preview").html('');
    $("#preview").html('<img src="loader.gif" alt="Uploading...."/>');

    var data = new FormData();
    jQuery.each($('#photoimg')[0].files, function(i, file) {
        data.append('file-' + i, file);
    });

    $.ajax({
        url: 'ajaximage.php',
        data: data,
        contentType: false,
        processData: false,
        type: 'POST',
        success: function(data) {}
    });
});

PHP:

print_r($_FILES);

Need help to resolve this issue.

2
  • 1
    What are you trying to do? The code posted is not a valid Javascript or PHP. Looks like you have just taken parts of code and mashed them together. If this is how it actually looks of course it won't work. <?php ?> and <script></script should be used for starters. Commented Mar 19, 2013 at 7:30
  • Are you trying to upload file with $.ajax ? Commented Mar 19, 2013 at 7:30

1 Answer 1

1

You can not upload files via AJAX. You can use this ajax form plugin

Example

Sign up to request clarification or add additional context in comments.

1 Comment

$('#photoimg').change( function() {$("#preview").html('<img src="loading.gif" alt="Uploading...."/>'); var oOutput = document.getElementById("preview"); var oData = new FormData(document.forms.namedItem("imageform")); oData.append("CustomField", "This is some extra data");var oReq = new XMLHttpRequest(); oReq.open("POST", "ajaximage.php", true); oReq.onload = function(oEvent) { if (oReq.status == 200) { oOutput.innerHTML = oReq.responseText ; } else { oOutput.innerHTML = "Error " + oReq.status + " occurred uploading your file.<br \/>"; } };oReq.send(oData);});

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.