ToddWelch Posted January 11, 2013 Share Posted January 11, 2013 I am using the code I have everything working on my page and the file upload is successful but I am trying to change the file name. I have implemented the code into my website found here to implement an HTML5 drag and drop file upload and it is working fine the files get uploaded to the directory but I need to make one change and it is not working. I want to change the uploaded file name to a php variable $petitionid set when the page is first loaded but the variable ends up being empty when it runs the following code in the post_file.php so I end up getting a file name of _banner.jpg I am assuming this has something to do with that the php file is being called from javascript so the variables on the original page is out of scope? Any idea how I can get the information from this variable on the original page? Here is the code in the post_file.php that is called from the javascript file that is run off of the petition.php page and $petitionid is a variable declared on the petition.php page.: // Move the uploaded file from the temporary // directory to the uploads folder: if(move_uploaded_file($pic['tmp_name'], $upload_dir.$petitionid."_banner.".get_extension($pic['name']))){ exit_status('File was uploaded successfuly!'); } Quote Link to comment https://forums.phpfreaks.com/topic/272992-variable-not-available-after-javascript-call-of-php-file/ Share on other sites More sharing options...
haku Posted January 11, 2013 Share Posted January 11, 2013 When you load a php page, and when you make a call to a php script with javascript, they are two entirely separate and independent php executions. Therefore nothing in one script will be available to the other script. So you have to save the data either in a database, in a session variable, in a cookie, or in the URL. You will need to implement one of these methods in order to pass data between your separate PHP calls (page load and javascript). Quote Link to comment https://forums.phpfreaks.com/topic/272992-variable-not-available-after-javascript-call-of-php-file/#findComment-1404861 Share on other sites More sharing options...
ToddWelch Posted January 21, 2013 Author Share Posted January 21, 2013 Thank you haku. I would have liked to use session variables but they ended up being empty as well just like the regular variables. Any idea why? I did get it to work with cookies but I would think the session variables would work much better since I would like to do more of them and have them set with files names, etc... Quote Link to comment https://forums.phpfreaks.com/topic/272992-variable-not-available-after-javascript-call-of-php-file/#findComment-1407172 Share on other sites More sharing options...
Christian F. Posted January 21, 2013 Share Posted January 21, 2013 Afraid we cannot tell you why, without seeing the code in which you tried to use them. Quote Link to comment https://forums.phpfreaks.com/topic/272992-variable-not-available-after-javascript-call-of-php-file/#findComment-1407243 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.