Home > Archive > PHP Language > December 2005 > PHP and Ajax
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| jrd100 2005-12-03, 9:55 pm |
| I am trying to write an web application using AJAX to track what link
the user clicked to leave the website.
The purpose of this is to have the embedded client-side XMLHttpRequest
object call a php script when the user clicks a hyperlink. Then the php
script gets the value from the query string, assigns this value to
variable and updates a MySQL table.
This is not working though. The php script ignores the GET request sent
by the client-side XMLHttpRequest object.
If I call the php script directly by entering a url with parameter from
the browser address bar, the database updates.
I am assuming the reason for failure is once a user leaves the page
where the getHTTPObject was first declared, it is destroyed and
communication with the server is gone.
The below is tested on RedHat 7.3/Apache. The test browser is Firefox
1.5. IE does not currently support a native XMLHttpRequest object as of
yet.
Thanks in advance for any insight.
CODE
*__ajax.html__*
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
function getHTTPObject() {
var xmlhttp;
xmlhttp = XMLHttpRequest();
}
http = getHTTPObject();
function phoneHome(obj) {
var x = obj.href;
var url = "yo.php?param="+x;
http.open("GET",url, true);
http.send(null);
}
</script></head>
<body>
<a href="http://yo.com" onClick="phoneHome(this)">yo</a>
<br>
<a href="http://oy.com" onClick="phoneHome(this)">oy</a>
</body></html>
*__yo.php__*
<?php
$link = $_GET['param'];
// assume database connection
$dbh=mysql_connect ("localhost", "<un>", "<pwd>");
mysql_select_db ("<database>");
$query = "insert into sample (idx,link) values('','$link')";
$result = mysql_query($query);
?>
| |
| Janwillem Borleffs 2005-12-04, 7:55 am |
| jrd100 wrote:
> I am trying to write an web application using AJAX to track what link
> the user clicked to leave the website.
>
> The purpose of this is to have the embedded client-side XMLHttpRequest
> object call a php script when the user clicks a hyperlink. Then the
> php script gets the value from the query string, assigns this value to
> variable and updates a MySQL table.
>
> This is not working though. The php script ignores the GET request
> sent by the client-side XMLHttpRequest object.
>
You don't need to use "ajax" for this.
In ajax.html, just use a link like the following:
<a href="you.php?param=http://yo.com">Go</a>
And in yo.php, do something like:
$link = $_GET['param'];
// assume database connection
$dbh=mysql_connect ("localhost", "<un>", "<pwd>");
mysql_select_db ("<database>");
$query = "insert into sample (idx,link) values('','$link')";
$result = mysql_query($query);
header("Location: $link");
Advantage: Works in all browsers *without* JavaScript.
JW
| |
| Hilarion 2005-12-06, 7:55 am |
| > I am trying to write an web application using AJAX to track what link
> the user clicked to leave the website.
>
> The purpose of this is to have the embedded client-side XMLHttpRequest
> object call a php script when the user clicks a hyperlink. Then the php
> script gets the value from the query string, assigns this value to
> variable and updates a MySQL table.
>
> This is not working though. The php script ignores the GET request sent
> by the client-side XMLHttpRequest object.
>
> If I call the php script directly by entering a url with parameter from
> the browser address bar, the database updates.
>
> I am assuming the reason for failure is once a user leaves the page
> where the getHTTPObject was first declared, it is destroyed and
> communication with the server is gone.
>
> The below is tested on RedHat 7.3/Apache. The test browser is Firefox
> 1.5. IE does not currently support a native XMLHttpRequest object as of
> yet.
>
> Thanks in advance for any insight.
>
> CODE
>
> *__ajax.html__*
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head>
> <script>
> function getHTTPObject() {
> var xmlhttp;
> xmlhttp = XMLHttpRequest();
> }
>
> http = getHTTPObject();
"getHTTPObject" function does not return any value, so
probably this is the cause of the problem. It's not PHP
issue.
You should probably change the code above to:
var http = XMLHttpRequest();
> function phoneHome(obj) {
> var x = obj.href;
> var url = "yo.php?param="+x;
> http.open("GET",url, true);
> http.send(null);
> }
> </script></head>
> <body>
> <a href="http://yo.com" onClick="phoneHome(this)">yo</a>
> <br>
> <a href="http://oy.com" onClick="phoneHome(this)">oy</a>
When user clicks the link the "phoneHome" function is
called and asynchronous (third parameter in "open" method)
request to your server is made. It's asynchronous, so sometimes
BEFORE the request is fulfilled, the browser goes on, which
means navigating to the addres given in "href". In that case
"XMLHttpRequest" defined on this page is freed (because the
bage is unloaded), which may mean aborting the request.
> </body></html>
>
> *__yo.php__*
>
> <?php
>
> $link = $_GET['param'];
>
> // assume database connection
> $dbh=mysql_connect ("localhost", "<un>", "<pwd>");
>
> mysql_select_db ("<database>");
> $query = "insert into sample (idx,link) values('','$link')";
> $result = mysql_query($query);
>
> ?>
Better working solution would be navigating to the PHP script
and after logging the destination, redirecting to the
real target URL. Janwillem Borleffs allready described
that solution.
If you do not like that the "extended" URLs which are used
in that solution ("yo.php?param=http://yo.com") are visible do
the user, then you can mask it (I do not recommend it because
it's a way to deny the user the information of the true
information to where the user will go) by adding some
javascript code like:
<a
href="yo.php?param=http%3A%2F%2Fyo%2Ecom"
onmouseover="window.status='http://yo.com'"
onmouseout="window.status=''"
>yo</a>
which is constructed in PHP like this:
<?php
$url = 'http://yo.com';
$link = 'yo';
printf(
'<a href="yo.php=%s" onmouseover="window.status='%s'" onmouseout="window.status=''">%s</a>',
urlencode( $url ),
htmlspecialchars( addcslashes( $url, ''\\"'."\n\r" ) ),
$link
);
?>
This will work even if the target link contains parameters
and other strange stuff.
On server side yo do not have to do anything (beside what
Janwillem Borleffs gave you).
Hilarion
|
|
|
|
|