0

As I understood $.ajax() is equivalent to $.post(). I am trying to use $.post to submit input file to PHP script. The purpose is to upload the image once the user browse and choose it.

I am trying to use something simple like this:

$(document).ready(function(){
$("input:file").change(function(){
$("#fileName").data($(this).val());
$.post( "upload.php", { fileName: $(this).val() }, function( data ) {
$( ".div1" ).html( data );
}); 
}); 
}); 

When I echo $_POST["fileName"] it shows:

C:\fakepath\imag-name.jpg

When I try to get the file data using basename($_FILES["fileName"]["name"]) I get this error:

Notice: Undefined index: fileName ...... etc

Any advice or solution is appreciated. Thank you in advance.

VLS
  • 2,306
  • 4
  • 22
  • 17
DevManX
  • 476
  • 3
  • 14
  • 1
    Possible duplicate of [How can I upload files asynchronously?](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously) – David Jan 18 '16 at 02:54

1 Answers1

0

You cannot parse things from Javascript to PHP, one is server side one is client side.

Learn more here: https://softwareengineering.stackexchange.com/questions/171203/what-are-the-differences-between-server-side-and-client-side-programming

The simplest solution is make a browser cookie in JS and retrieve it in PHP. The value would be the URL of the uploaded image, probably best to give these cookies a programatically generated name (Out of scope of question.

Community
  • 1
  • 1
Bigwater
  • 223
  • 2
  • 12
  • Please click the tick next to my answer if this helps you in the least. if you cannot figure this out I can go into further details just comment and let me know. :) – Bigwater Jan 18 '16 at 02:56
  • What's he asking for is certainly possible. –  Jan 18 '16 at 03:21