Welcome to WebmasterWorld Guest from 54.196.147.57

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP classes and Ajax

train of thought again

     

immols

10:13 am on Feb 17, 2009 (gmt 0)

5+ Year Member



Stuff basically works
My index.php is a structure in which i 'hang' pages.
A page is a php file, which is 'filled' by an instance of the class Page.
A dbo is an instance of a Databaseobject.

A page-instance knows what to show (this->dbo->get_content) and can forward the order to change te content on the page (this->dbo->set_content).

It all works like a charm. When a user clicks the save-button on a page, the current page executes this->dbo->set_content and then this->dbo->get_content and the changed data is shown.

However... I would like to use AJAX.
So now i will have my page-object generate this code:
onblur="update_field('var1','var2',var3');"

'update_field' is an ajax function
url = test.php?form=var1&page=var2&field=var3;
xmlHttp.open("GET",url,true);

How do i get this test.php to recognise the current instance of the page-object? I do not want to make a new instance of the class (or do i?). I cannot give the object to the page as a parameter on the url from the ajax function.

Any shov in the right direction is appreciated.

coopster

12:54 pm on Feb 17, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



How do i get this test.php to recognise the current instance of the page-object?

What do you mean? The current URL?

immols

2:45 pm on Feb 17, 2009 (gmt 0)

5+ Year Member



I mean i have an index.php, on which i create an instance of he class Page.
Lets call it $aPage

On this index.php i also have the ajax function.

A visitor is on aPage and wants to change some value. He submits aForm.
The website then calls aPage->aDbo->set_values(), followed by Page->aDbo->get_values(). The data now is changed in the database and shown on the form.

With ajax, i want to do the same. However, if i use ajax, i need to provide an address of a page, and not the name of a function. So i provide the url to test.php. Test.php is not aware of the existance of aPage. So it is not possible to use any function of aPage from test.php.

Or is there a way to make this test.php know about aPage?

immols

4:48 pm on Feb 17, 2009 (gmt 0)

5+ Year Member



In short: my test.php has this statement

<?php
$current_page_instance->set_contents($content_value,$content_id);
?>

But i get message '<br /> <b>Fatal error</b>: Call to a member function set_contents() on a non-object in <b>/usr/......./test.php</b> on line <b>2</b><br /> '

coopster

12:00 am on Feb 18, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Somehow when you first build that page there must be something that you are using to define which page to construct and show the visitor, I'm guessing the URL as that is how a user requests each page via HTTP, which is a stateless protocol. Therefore, pass the URL back and forth in your AJAX function and you will be able to reconstruct the instance of your object from there.

immols

7:53 am on Feb 18, 2009 (gmt 0)

5+ Year Member



Thank you for bearing with me, Coopster

User is at index.php?page=contact (with htaccess it's gonna be '/contact')

index.php contains 'aPage = new Page(vars);'
test.php is the page on which the action of the ajax function is.

You suggest that i change in my ajax function
url = test.php?form=var1&page=var2&field=var3;
to
url = index.php?form=var1&page=var2&field=var3&page=contact;

and to index.php i add something like 'if($_GET['ajax'] == 'ajax'){$current_page_instance->set_contents($_GET[field_value],$_GET[field]); return;}'

It works (yay!)
Now I think i need to get this part out of my index.php and put it in a 'require_once' file.

Thank you. I will be able to struggle further from here :-) It's hard to change a train of thought that has been running full speed for a couple of years. Working on it though.

Regards

coopster

3:06 pm on Feb 18, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Now you're on it. Good for you.
Don't forget, you can always post data with AJAX too, it doesn't have to be a GET request. This allows you to respond to 2 different types of requests with a single processing page back on the server. Food for thought.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month