sgaron Posted January 10, 2007 Share Posted January 10, 2007 Alright, I'm trying to design an interface with modules that can get refreshed. Theses modules are PHP classes. If I call the refresh function of a module, I want only that module to refresh.I'll give you a simple exemple of what I got and than tell you whats the problem. I got 2 files:Index.php[code]<?phprequires "mod.class.php";$module = new mod();$module->display();echo "<input type=button onclick='refresh();'>";?>[/code]mod.class.php[code]<?phpclass mod{ mod(){} display(){ echo"<div id=mod_id>SOME COMPUTING DONE HERE AND STUFF DISPLAYED</div>"; } refresh(){ echo "new text without the divs"; }}?>[/code]Now since I only want to refresh the text in the control and put the result inside the DIVs I have to call my [b]refresh() [/b] function in javascript that uses the [b]onreadystatechange [/b] property of my browser and does a [b]getElementById('mod_id') = responseText[/b].Here's the problem... Javascript cannot call directly the class (mod.class.php) because there is no instance of the class in that file. So, I would have to call index.php and create a refresh function in that file that would look like this:[code]function refresh(){global $module;$module->refresh();}[/code]The thing is, if I do that, I'm gonna have to create a refresh function for all the modules I'm gonna create which is really not convieniant and gives a lot of overhead in the file.Is there anyway I can tell java script to call directly the refresh() function form the [b]mod.class.php[/b] using the [b]instance of the index.php[/b] file? or maybe to call[b] index.php?$module->refresh() [/b] or any other sort of obscure function calling directly in the title bar??Steve Quote Link to comment Share on other sites More sharing options...
Jocka Posted January 10, 2007 Share Posted January 10, 2007 something like:[code]<?phpclass mod{ mod(){} display(){ echo"<div id=mod_id>SOME COMPUTING DONE HERE AND STUFF DISPLAYED</div>"; } refresh(){ echo "new text without the divs"; }}if($_GET['refresh']){ $mod = new mod(); echo $mod->refresh();}?>[/code]then send to [b]mod.class.php?refresh[/b]or something to that nature Quote Link to comment Share on other sites More sharing options...
sgaron Posted January 11, 2007 Author Share Posted January 11, 2007 Thanks for trying to help but its not really gonna work because its not the same object. Your function create a new object $mod = [b]new[/b] mod(); so it will refresh the new object and not the old one...Thats my biggest challenge... I need to get back my old object from the other page ... Is it possible to create a function like the one you did but passing it an argument that refer to a variable name and to get access to that variable from a list of variables... Lets say that PHP_VAR_LIST was the list of all variables in PHP. I could use a function like this to solve my problem:[code]<?phprequires "mod.class.php";$module = new mod();$module->display();echo "<input type=button onclick='refresh();'>";?>[/code][code]<?phpclass mod{ mod(){} display(){ echo"<div id=mod_id>SOME COMPUTING DONE HERE AND STUFF DISPLAYED</div>"; } refresh(){ echo "new text without the divs"; }}if($_GET['refresh']){ PHP_VAR_LIST[$refresh]->refresh();}?>[/code]than send [b]index.php?refresh=module[/b] and it would refresh using the [b]$module[/b] variable... Does it make any sens? Does a variable similar to my PHP_VAR_LIST I invented exist in PHP? Quote Link to comment Share on other sites More sharing options...
trq Posted January 11, 2007 Share Posted January 11, 2007 Im not really sure what your talking about here, sounds like more of an Ajax issue than a pure php one.However, I think Ive picked up that you would like to persist objects across multiple requests? This can be done by storing your object within the $_SESSION array.As for refreshing a [i]module[/i] in place. You'll need some Ajax trickery, we have an Ajax forum. Quote Link to comment Share on other sites More sharing options...
sgaron Posted January 11, 2007 Author Share Posted January 11, 2007 so what your saying is that I have to declare my class as a session variable so I can go fet it back ... Makes a lot of sense I'll try that! And I'll check the Ajax forum next time.Thx Quote Link to comment 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.