How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-PHP?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Aug 2009
Posts: 35
Reputation: Altairzq is an unknown quantity at this point 
Solved Threads: 0
Altairzq Altairzq is offline Offline
Light Poster

How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-PHP?

 
-1
  #1
Sep 17th, 2009
Hello.

I'm using AJAX for all the updates to the web site I'm building and it works very well. But now I face the need to:

1) Upload a file (an image or video) to a folder in the server.
2) Insert a row in MySQL with the name of the file plus some other information (year and a comment).

The user would upload the file and fill in the information in the same window, and should have a single message (if possible) saying that the file was added or not (meaning both the upload and the insert).

I have been doing some googling and found information as how to simulate and AJAX upload, since it seems AJAX does not support file uploads (hope I'm wrong). This is what I found:

http://www.anyexample.com/programmin...ile_upload.xml

So following that example I could upload the file, and I'd have all the information from the form, in the PHP, and I could synchronize the upload and the insert, but then I can't figure out how to send the OK from the PHP to a Javascript (i guess) to update the html and display the message in the web site. And anyway, is this the right approach to do what I need?

I'm a rookie in Javascript and AJAX, so I'd really appreciate any hint.
Last edited by Altairzq; Sep 17th, 2009 at 9:12 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,081
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-

 
0
  #2
Sep 18th, 2009
Yes, unfortunatley you can't use XMLHttpRequest to upload a file fro the users computer.

The reason is that JavaScript does not have access to the file contents. It only has access to the file name, once the user has selected a file to upload. This is a security feature.

The actual upload of files, is completely done by the browser, and is not in the control of client side scripting.

Unfortunately PHP (without special extensions) cannot control the upload of the file into the sever either. This is why creating and upload progress monitor in PHP is difficult under default settings.

Taking that into account. Your choices in uploading files from browser to PHP without refreshing the page are:

1) Use an Iframe to upload the file
2) Use a client side plugin, such as Flash or Java

The first is probably the simplest, and more cross browser compatible.

So following that example I could upload the file, and I'd have all the information from the form, in the PHP, and I could synchronize the upload and the insert, but then I can't figure out how to send the OK from the PHP to a Javascript (i guess) to update the html and display the message in the web site. And anyway, is this the right approach to do what I need?
You'll need a way for the IFRAME to send a message to the parent page. From the Iframe you can reference the parent window's window object using: window.parent

A simple example is:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // inside iframe
  2. window.parent.document.write('hi');

This will write "hi" in the parent window.

So you can use the same to invoke any other function. eg:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // inside iframe
  2. window.parent.uploadDone(true);

So when your PHP page finishes writing the upload file, you could send back a page with the JS:

  1. echo '<script>window.parent.uploadDone(true);</script>';

This will let the parent know the status of the upload.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,081
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-

 
0
  #3
Sep 18th, 2009
BTW: in the parent, you'll have defined the function: uploadDone()

eg:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function uploadDone(status) {
  2.  
  3. if (status) alert('upload complete');
  4. else alert('upload error');
  5.  
  6. }
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 35
Reputation: Altairzq is an unknown quantity at this point 
Solved Threads: 0
Altairzq Altairzq is offline Offline
Light Poster

Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-

 
0
  #4
Sep 19th, 2009
Wow your reply is pure gold to me, thanks a lot! I'm working on it, I have the upload working with the iframe, now I need to set up the communication as you show, it's exactly what I need, much appreciated!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 35
Reputation: Altairzq is an unknown quantity at this point 
Solved Threads: 0
Altairzq Altairzq is offline Offline
Light Poster

Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-

 
0
  #5
Sep 24th, 2009
Made a small test and it's working beautifully, thanks again

One rookie question: why is it necessary to use an iframe to do this upload? Couldn't it be done in a simple div? Well I know it can't, I tried, but why?

Or where could I find information that would clarify this point to me?
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,081
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-

 
0
  #6
Sep 25th, 2009
Originally Posted by Altairzq View Post
Made a small test and it's working beautifully, thanks again

One rookie question: why is it necessary to use an iframe to do this upload? Couldn't it be done in a simple div? Well I know it can't, I tried, but why?

Or where could I find information that would clarify this point to me?
The Form has to be submitted by the browser, instead of programmatically via JavaScript, in order to send the file contents.

The Iframe does not belong to the same document. So the browser can submit the form within the Iframe without reloading the parent document. It only reloads the Iframe.

If it were a DIV, the browser would reload the parent.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,081
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-

 
0
  #7
Sep 25th, 2009
Something that might interest you is that before you could do XMLHttpRequest, the basis of AJAX, you could create Iframes programmatically with client side scripting.

Back then it was called iframe remoting, and it worked well and achieved all that you can do now with current AJAX and more, such as sending files. It was also cross domain and had detailed load progress indication. So in a way, current AJAX was a step backwards functionality wise. (It is just easier to make it work cross browser)

The new specifications from W3C, WebSockets, will really go beyond any of this however, especially since it allows low level TCP instead of just HTTP.
http://dev.w3.org/html5/websockets/
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 35
Reputation: Altairzq is an unknown quantity at this point 
Solved Threads: 0
Altairzq Altairzq is offline Offline
Light Poster

Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-

 
0
  #8
Sep 28th, 2009
Thank you this is really interesting to know. I should read about all this stuff to get a background of what I'm doing. I need more time ack!

Ah well.. I'm facing another problem now, related to this but will start a new thread.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 35
Reputation: Altairzq is an unknown quantity at this point 
Solved Threads: 0
Altairzq Altairzq is offline Offline
Light Poster

Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-

 
0
  #9
Sep 30th, 2009
Originally Posted by digital-ether View Post
So when your PHP page finishes writing the upload file, you could send back a page with the JS:
  1. echo '<script>window.parent.uploadDone(true);</script>';
This will let the parent know the status of the upload.
This bit is not working for me. It writes the output in a new window as plain text and that's it.

So far I was using a small PHP script that called the JS function outside the <??> tags (at the end of the php code) and it worked fine. But now I'm inserting the upload inside a PHP program that controls all the web flow and I would need to pass control to a JS function from inside a function and then exit().

So it seems nothing is trying to interpret that code and it's just plain text written in a new document. What I'm doing wrong?
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,081
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-

 
0
  #10
Sep 30th, 2009
The new window must be interpreted as HTML. If you name the page with a php extension (.php) then the server should interpret it as HTML, and execute the JavaScript.

However, you can also send the content-type header.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. header('Content-Type: text/html');

Is this the issue you're having?
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC