Jump to content

This one is a hard one!


sgaron

Recommended Posts

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]<?php

requires "mod.class.php";

$module = new mod();
$module->display();

echo "<input type=button onclick='refresh();'>";

?>[/code]

mod.class.php

[code]
<?php

class 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
Link to comment
https://forums.phpfreaks.com/topic/33644-this-one-is-a-hard-one/
Share on other sites

something like:

[code]
<?php

class 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
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]<?php

requires "mod.class.php";

$module = new mod();
$module->display();

echo "<input type=button onclick='refresh();'>";

?>[/code]


[code]
<?php

class 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?

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.

Archived

This topic is now archived and is closed to further replies.

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