| | |
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:
Solved Threads: 0
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.
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.
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.
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:
This will write "hi" in the parent window.
So you can use the same to invoke any other function. eg:
So when your PHP page finishes writing the upload file, you could send back a page with the JS:
This will let the parent know the status of the upload.
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?
A simple example is:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
// inside iframe 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)
// inside iframe window.parent.uploadDone(true);
So when your PHP page finishes writing the upload file, you could send back a page with the JS:
php Syntax (Toggle Plain Text)
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
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:
eg:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function uploadDone(status) { if (status) alert('upload complete'); else alert('upload error'); }
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Aug 2009
Posts: 35
Reputation:
Solved Threads: 0
Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-
0
#4 Sep 19th, 2009
•
•
Join Date: Aug 2009
Posts: 35
Reputation:
Solved Threads: 0
Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-
0
#5 Sep 24th, 2009
Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-
0
#6 Sep 25th, 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?
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
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/
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Aug 2009
Posts: 35
Reputation:
Solved Threads: 0
Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-
0
#8 Sep 28th, 2009
•
•
Join Date: Aug 2009
Posts: 35
Reputation:
Solved Threads: 0
Re: How to synchronize a file upload with an insert in MySQL through Javascript-AJAX-
0
#9 Sep 30th, 2009
•
•
•
•
So when your PHP page finishes writing the upload file, you could send back a page with the JS:
This will let the parent know the status of the upload.php Syntax (Toggle Plain Text)
echo '<script>window.parent.uploadDone(true);</script>';
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?
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.
Is this the issue you're having?
However, you can also send the content-type header.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- A little help with Help with ajax/php (JavaScript / DHTML / AJAX)
- how to upload images to mysql with a foreign key using php (PHP)
- Looking for PHP / MySQL Developer (Web Development Job Offers)
- File Upload Variables (PHP)
- JavaScript / AJAX Guru (Software Development Job Offers)
- [For Hire] Php, Mysql, ASP, AJAX, DHTML programmer (Post your Resume)
- AJAX & PHP Developer ( Full Time ) (Web Development Job Offers)
- [For Hire] PHP, Mysql, ASP, AJAX, DHTML programer (Post your Resume)
- File upload problem (PHP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Hiding a div if a variable is empty
- Next Thread: Change the name of a file getting downloaded
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxjspservlets array browser bug calendar captchaformproblem checkbox child class close codes cookies createrange() cursor date debugger dependent disablefirebug dom dropdown editor element embed engine events explorer ext file firefox form forms getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe image() images internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl maps masterpage math matrixcaptcha media object onerror onmouseoutdivproblem onreadystatechange parent paypal pdf php position post programming progressbar rated redirect regex runtime safari scriptlets scroll search security session shopping size software sql star stars synchronous text textarea toggle unicode variables web webservice windowsxp wysiwyg \n







