• Advertisement
Sign in to follow this  

[web] PHP / JavaScript Tricky Interaction Question

This topic is 3583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I'm pretty new to web development, having more experience in Java programming. Right now, I'm creating a new website and I've designed a cool feature where using PHP I create a menu page which reads the directory structure on the server and creates the links appropriately (it basically looks for folders that contain a main.html file and adds those links to the list). Now, the problem is that I need to know what directory I'm currently in, aka I need access to the path of a static HTML file running in another frame in order to have the right path to generate those links. The problem is that the path is on the server, so it screams PHP but the frame can only be accessed on the client, so it screams javascript. Am I just stuck? Googling for passing data from javascript to PHP wasn't pretty and I'm wondering if there's some methodology I can use to help me out here.

Share this post


Link to post
Share on other sites
Advertisement
Is it something that you could write in to a cookie with PHP?

You can read the cookie back with javascript... but this sounds kind of a bad idea to me. PHP, javascript, and Frames? Oh my!

Otherwise... you could probably render the directories as hidden elements in one frame, read them into the other frame with javascript, and do whatever that way.

I'm not sure what your goal is?

Share this post


Link to post
Share on other sites
Well there is a few ways you can move data from your server side scripts (php) to your browser (javascript/html).

You could use php to set cookies on the browser to move data, but its not really a good way to move data. Cookies have a size limit and are not ideal for moving data.

You could also have the php script add in a javascript variable containing the path. You are not limited to just outputting html with php. Example:

var current_path = <?= $current_path ?>;

Something like that would work.

You could also move data by using JSON or xml. This is done by javascript sending a request to the server and the server sending back the data in one of those formats. This is probably the most complex solution, but its very useful for doing ajax stuff.

I'd suggest using the second method, outputting the javascript variable holding the path. Effective and simple.

Share this post


Link to post
Share on other sites
The ultimate goal was to set up the linking structure to reflect a file system on the server.

Thanks for the help, I ended passing data to PHP by putting it in the URL and using the $_GET method. Right now, I have the dynamic menu actually changing based on where you are.

Now, the only problem I'm currently facing is that when I click on a link in the menu, I run javascript that changes the href of the main frame. Then, the menu refreshes itself to reflect the updated location but the problem is that the main page might not load in time, and the menu may or may not "advance" as required. I don't even require that the page contents are updated, just the URL which is parsed to figure out the current path essentially. Is there any way to ensure that this data is properly synchronized, or do some fancy waiting in javascript to ensure that the new url is read?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement