Forum Moderators: coopster

Message Too Old, No Replies

Upload file using javascript and PHP

         

schuey97

12:48 pm on May 25, 2009 (gmt 0)

10+ Year Member



Hi All
Have a problem here that has got me stumped. Not sure if it should be in the javascript section or here.

At the moment I have a button that when pressed, javascript unhides a div section and places HTML in that div. This div is an email form which includes the ability to add a file to upload (a resume). When a user submits this form, javascript goes through and validates everything. Then it passes the variables to a PHP script via:
window.location.href="php/resumeEmail.php?name=" + name + "&job=" + job + "&email=" + email + "&resume=" + resume + "&message=" + message;

The PHP script uses $_GET to recieve these variables. The email section of the script is working fine, but I cant get the PHP script to upload the resume which is the file the user needs to upload. Normally I would use $_FILES to upload a file but this seems to only work if you go from the html form to the php script, not through javascript.

Is there anyway of getting something like this to work, or am I going to have to create the div in php instead of javascript.

Thanks for your help

rainborick

6:25 pm on May 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need set the <form> method="post" in order to send the user's file to your PHP script, and then have your JavaScript do form.submit() if the user's input validates. You can't do it with window.location pointing to the script's URL with a query string. The script will then need to receive the information in the $_POST vars and use the PHP function move_uploaded_file() to retrieve the file.

penders

12:42 am on May 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...and set enctype="multipart/form-data" in your FORM element. And use $_FILES as you have done previously. It does not matter if you are submitting the form (or creating the div/form) using JavaScript or not. But... when you are setting window.location.href="..." you are NOT actually submitting the form.

schuey97

12:26 pm on May 26, 2009 (gmt 0)

10+ Year Member



Thanks for the help....I was able to change my code a little bit and it works fine