Welcome to WebmasterWorld Guest from 54.204.100.232

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

True interactivity with php

Running code after the page has finished loading?

   
2:18 pm on Jun 26, 2008 (gmt 0)

5+ Year Member



Is it possible to run PHP after a page has finished loading completely?

The only way that I've found to do it is to make a form with a hidden variable, and then use javascript to set the variable and submit the form which reloads the page, and then have PHP check if the post variable is set when the page reloads.

test.php
<?
if(isset($_POST['test'])){

echo "you pushed the button!";

}else{

?>
<form name="t" method="post" action="test.php">
<input type="hidden" name="test" />
<input type="button" onclick="javascript:document.t.test.value='ok';document.t.submit();" />
<!-- Yes, I realize that I don't have to use javascript in this instance, but its just an example of a deeper problem -->
</form>
<?
}

?>

It would be really nice if you could just use runat=server or some kind of script to just call a php function without reloading the page.

Am I just missing something?

2:27 pm on Jun 26, 2008 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Ajax is the way to go.

dc

2:50 pm on Jun 26, 2008 (gmt 0)

5+ Year Member



Ajax is pretty, but some of the things I want to do involve getting and setting information to a database, which PHP is pretty good at, and it would be nice to be able to do stuff without having to reload the page every time.

Maybe it's time to learn ASP.net

2:53 pm on Jun 26, 2008 (gmt 0)

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



That won't change anything. HTTP is a stateless prototcol. Getting information requires a round trip to the server and the server side processing can be whatever you choose to use, whether that be PHP or ASP. Ajax is JavaScript. Basically you are loading only a part of the page, not the entire page contents all over again when you use AJAX or other related client-side coding techniques.
3:32 pm on Jun 26, 2008 (gmt 0)

5+ Year Member



hmm, ok

It would be nice if there was a way to make it not stateless in the future, but in the mean time it's good to know that there really isn't a better way to be doing it that I've been missing.

I use some ajax, but as you say it's just javascript, and client side, and therefore it's limited on what you can do with it.

I wonder how long it will be before someone develops a client side/server side language that can be both interactive with the user and accomplish real stuff on the server.

It would need to run client side, but be able to make calls to server side functions and get data back without reloading the page all over again...

4:07 pm on Jun 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would need to run client side, but be able to make calls to server side functions and get data back without reloading the page all over again...

That is exactly what AJAX does. To be precise: with AJAX you can have JavaScript call a script on the server and parse the server's reply.

[edited by: RonPK at 4:09 pm (utc) on June 26, 2008]

4:29 pm on Jun 26, 2008 (gmt 0)

5+ Year Member



Look up the YUI library, and take a look at the Connection Manager. That should give you a starting point.

There are other libraries out there, but since I use YUI all over the place anyway... :)

4:57 pm on Jun 26, 2008 (gmt 0)

5+ Year Member



I'll definitely look into that.
I always enjoy learning something new, especially because I never really thought of ajax to do anything other than make stuff look pretty and move around the page.
6:18 am on Jun 27, 2008 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



>> I never really thought of ajax to do anything other than make stuff look pretty and move around the page

While AJAX uses JavaScript they aren't the same thing. From what I understand, you have used JavaScript to make stuff look pretty ;) AJAX isn't one scripting language, but multiple technologies working together. If you are looking for a nice PHP/AJAX tutorial I really like Rasmus' one here: [news.php.net...]

8:27 am on Jun 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is not a bad overview, apart from these lines:

var browser = navigator.appName; 
if(browser == "Microsoft Internet Explorer"){

Browser sniffing s***s, as UA strings can be tampered with. It's better to use object detection to decide which object to use (duh).

6:47 pm on Jun 27, 2008 (gmt 0)

5+ Year Member



I use some ajax, but as you say it's just javascript, and client side, and therefore it's limited on what you can do with it.

No, it's not as limited as you seem to believe. Google uses AJAX in Gmail, Google Maps, Google Suggest, iGoogle, Google Calendar, and a number of other complex applications.

8:54 pm on Jun 27, 2008 (gmt 0)

5+ Year Member



Well webfoo,
I am starting to see the errors of my thinking.
I do know that Google uses ajax in those things, but I guess I didn't realize it used them to interact with the server.
Now, Google suggest was an oversight for me, but I don't really use it.
iGoogle, which I do use, uses ajax to let you move the widgets around on the page, which is more of how I saw ajax. pretty fluffy stuff, but not really all that useful.
Now that I've had time to think about it, I realize that when you move stuff around it does have to interact with the server to save where you moved the widget to.
So I'm going to have to take another look at the whole subject, start thinking how I can make use of it.