Jump to content

Variable not Available after Javascript Call of PHP file


ToddWelch

Recommended Posts

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!');

}

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

  • 2 weeks later...

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.