use PHP with Javascript question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
skyandfish
Forum Newbie
Posts: 3
Joined: Sun Dec 29, 2002 11:57 pm

use PHP with Javascript question

Post by skyandfish »

A web page written in PHP (foo.php) and a link in the page is:

<a href="#" onClick="record_12history_hitrate()">........

the function record_12history_hitrate() is defined on the top of the page as the following:

<script language="JavaScript">
<!--
function record_12history_hitrate()
{
//open a new window to display another page
window.open('./activity.php?activity_id=30', '12history');

//record this hit into db
// connectDB() and record_12history_hitrate() are both php function
//defined my self, the former is used to connect mysql db, the latter is
// used to write the data into db
document.writeln("<?php connectDB(); record_12history_hitrate(); ?>");
}


My question is: the new page could be opened, and the db could be written. But after each hit the link, the original page foo.php became
blank, it seemed that something is wrong, so, what's the reason or somebody has good other idea to implement it??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe I missed the point but you might have missunderstood the concept of serverside/clientside scripting. From the comment "//record this hit into db" I take it you want the php-code to be executed when the function record_12history_hitrate() is invoked, but that's not the case.
When php delivers the document (to the webserver that delivers it to the client/browser) it already has processed the <?php ...?>-blocks. So your browser will get something like

Code: Select all

<script language="JavaScript">
<!--
function record_12history_hitrate()
&#123;
//open a new window to display another page
window.open('./activity.php?activity_id=30', '12history');

//record this hit into db
// connectDB() and record_12history_hitrate() are both php function
//defined my self, the former is used to connect mysql db, the latter is
// used to write the data into db
document.writeln("<tr><td>abcd</td><td>1234</td> .... ");
&#125;
php doesn't care about the clientside javascript function record_12history_hitrate() and the browser does not care about the server-side php-blocks (because the majority of the browser does not know anything about php and they won't see any php-commands)
you probably have to perform that action in activity.php when a parameter activity_id has been sent.
skyandfish
Forum Newbie
Posts: 3
Joined: Sun Dec 29, 2002 11:57 pm

Post by skyandfish »

hi, volka, yes, I made a mistake for the server script and client script ;)

I think the idea of u that do someting in the activity.php doesn't fit the case, because there are couple of entry to enter activity.php but I only want to record the "hit" way into db.

Now I get an idea is that opening 2 windows at the same time:
window.open('./activity.php?activity_id=30', '12history');
window.open('./test.php, "test");

In test php I can write dat into db, and I use onLoad="setTimeout(window.close, 0) in to <body> tag so that the window can be closed immdeiately. But this idea will increase the load of the user.

Do u have any other idea??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what about

Code: Select all

window.open('./activity.php?activity_id=30&hit=true', '12history');
?
Post Reply