PHP or JS file question

reverend boltron

Senior member
Nov 18, 2004
945
0
76
I was wondering if it is possible to update the contents of a php file or a JS file without having to reload the entire file again.

What I am working on is a group of select boxes that are "chained" or dynamically update based on what the user selects in a given box.

The 'parent box' is the "User" box, which has a child, "Book", who has a child, "Chapter" who has a child, "Verse".

when you click on User, the other boxes update according to that, so if user is changed to 'joe', then the books load for 'joe', and the chapters of the first book in the option list load in chapter, and the verses in the first chapter load in verse. When the user selects a verse, the verse loads in a div box via a MySQL request.

The problem that I am having though is my verses alone total 1,344. It isn't really a problem when the page loads, because of connection speeds, but I don't want it to turn into an issue, and I don't want to neglect anyone who might have dial up.

How this page was originally set up was with a JavaScript library that would do all of the dynamic updating for the boxes, and it did it nicely too. I wouldn't run into problems with the child boxes not repopulating properly automatically and properly.

When I made it possible for there to be multiple users though, I switched over to using php and MySQL only. This was great because I didn't have the overhead of the JS library and it cut the file size down to 3k instead of 66k.

So what I want to do though, is use both the JS and the PHP/MySQL at the same time.
I don't really know how everything works, and I don't have a lot of time because of my living conditions (I'm in the Philippines doing an internship), but here is what I was thinking about doing.

I want to use a select statement as the condition for loading the specific for the specific user. The default will be the originally selected user in the user box, that will be completely loaded when the page loads.

I would like to have a PHP function that is trigged for both onclick and onchange in the 'User' select box.

what the function will do is check to see if the user is in the list of users that have already been loaded. If it is the first time the user box has been changed, then the list will be empty except for the default.

the pseudo code will look something like
$gotoSwitch=0;

while($counter>$arrayLen) if($user==$usersLoaded[$counter]) $gotoSwitch=1;

if(!$gotoSwitch) appendData($user);

switch($user)
{
include(userdata.php)
default:
//default data
}


appendData($user)
{
//searches mysql for select box info
//appends userdata.php
}

Now, I'm sure that there are a few bugs in the code, and that is all good and well, it will get ironed out in the process. But I was wondering if there was a way I could do this with JS too. I think that the PHP might cause the file to be downloaded each time, but I think if I can do this with JS, then I would be on the client side. But I might not even know what I'm talking about...

I am looking into how cookies work. Because I honestly don't know anything about them, and if I could do this with a cookie, then maybe it would be a decent route to take.

The whole point to this is to cut down the total loading/reloading time and to give the illusion that things are there when they aren't.

If I can get this to work, then I would like to have a few functions that go around in the background that grab the data so that the user won't notice the loading times at all.
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Hmm... what you are asking to do is to re-request information from the server once a page is loaded without having to reload the whole page. This is definitely possible with PHP and JavaScript. This technique of requesting information from the server after the page load is essentialyl Ajax - a term that you maybe familiar with. Regardless you can read about it all over the web... a quick google search gave me this result:
http://www.webpasties.com/xmlHttpRequest/

That tutorial seems to cover the basics well and should give you a clear idea of how to get what you want done.

Basically, you will want to have the user box filled with all the users on the page load -- then whenever the value in that box changes you will need to request information from say books.php to which you pass the user id -- you then update the book box. Similarly if the book value is changed by the user you request information from chapters.php with the book id (and user id if you need to) and so on.

The important thing to understand is that in my breakdown above, books.php and chapters.php do not update the the "whole" page -- they respond with just enough information so that your JavaScript can change the page without reloading it. Also, books.php and chapters.php do not have to be seperate files -- if you read the tutorial you should be able to see pretty easily how to have a single php script to handle these Ajaxy requests.
 

reverend boltron

Senior member
Nov 18, 2004
945
0
76
Hey, thanks for your reply statik.

I have actually already looked at that site, it is very helpful.

What you described above is similar to how I have my page set up now. But I'm running into problems with the boxes and their repopulation, also with delay. I want to cut down and eliminate on the amount of data that is repeatedly downloaded.

I am currently thinking about possibly loading the values into arrays and trying to go that route. If that works, I'll update here.

I have no idea how cookies work, but I will also see if I can use that.

Is there a way to append data through an xmlHttpRequest? I know I can put new data in there, but what if I want to add on to what I already have?