0

Here is my sample jQuery coding that I used for passing my image to php using AJAX.

HTML input

<input id="forkliftImagePath" name="image" type="file" />

admin_forklift_detail.php

$.ajax({
    type: "POST",
    url: "../Client/php/add_forklift_detail_process.php",
    data:  str,
    cache: false,
    contentType: false,
    processData: false,
    async: false,
    success: function(msg) {
        alert(msg);
    }
})

here is another php file that execute the code that pass from AJAX.In this file will get the image name and pass back to "admin_forklift_detail.php" for alert purpose.

add_forklift_detail_process.php

<?php
$name = $_FILES['image']['name'];
echo $name;
?>
Bjoern
  • 15,934
  • 4
  • 43
  • 48
user1348226
  • 41
  • 1
  • 10
  • 3
    What's your question? – davey Apr 07 '14 at 11:13
  • 1
    as far as I remember you cannot do file uploads with ajax like this you have to use XHR and handle the file upload separate to the form data post I could be wrong though – Dave Apr 07 '14 at 11:13
  • see this http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload – Saqueib Apr 07 '14 at 11:14
  • Do you have enctype="multipart/form-data" on your form tag? – gbestard Apr 07 '14 at 11:16
  • @Dave: the ajax call which OP has posted, is just passing the filename of the image and not the image itself. uploading is happening at the server side script(php) – dreamweiver Apr 07 '14 at 11:20
  • As others said before, uploading a file via AJAX is not as simple as that. It won't work in some browsers since it needs XHR 2. See related and linked questions for more info. – aurbano Apr 07 '14 at 12:12
  • @davey Sorry for that I did not provide a clear information. My question is I tried view on the related question that using AJAX to passing image file and content of the form to php for insert particular record to database. Unfortunately I tried to alert the image name by passing back the $name variable to AJAX. It show me Undefined Index for the image. Thanks. – user1348226 Apr 08 '14 at 02:03
  • @gbestard Yes. I did this on the form tag there. – user1348226 Apr 08 '14 at 02:04

0 Answers0