Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

obtain php response with javascript without displaying it

Status
Not open for further replies.

codymanix

Programmer
Aug 4, 2002
11
DE
hi!

i ave a counter which returns only the number as string like "003234". i want to display in my html file a text like

"you are the 003234th visitor of my website".

i know how to dynamically substitute text in a html file using javascript. but how can my script obtain the number?
 
If the number is a php variable, you don't even need to use javascript; just use php:

You are the <?= $var_name ?> person to visit the site.

Rick It's a pleasure to know that I've helped you. If I have,
please click the link below to let me know. :)
 
sorry but it didn't helped me. i don't want a serverside solution i want it done using javascript so that the counter can automatically update itself without reloading the page. the counter only returns a string like &quot;00434&quot; and i want it to be inserted in my page.
 
codymanix,

this could be done by putting the counter in its own frame, and having that frame reload itself regularly.

an iframe would be easy:

main.html:
[tt]
<html>
<body>
blah blah...
<p />
<iframe src=&quot;counter.html&quot; />
</body>
</html>
[/tt]

counter.html:
[tt]
<html>
<body onload=&quot;setTimeout('location.reload();',1000);&quot;>
You are visitor (X)
</body>
</html>
[/tt]

is your counter in javascript? how are you storing the page views?
=========================================================
if (!succeed) try();
-jeff
 
oh yeah, php:

don't know php, but that should make counter.html something like:

<html>
<body onload=&quot;setTimeout('location.reload();',1000);&quot;>
You are visitor <?= $count ?>
</body>
</html>
=========================================================
if (!succeed) try();
-jeff
 
if i do it with an iframe there is always a bit box around the counter. but i want the number display in the text on the site. isn't there a javascript-function like:

// is there a function like
var counter =
getResponseFromScript(&quot;// ???

document.text.innerHTML = counter;

...

<body>
you are the
<span name=&quot;text&quot;></span>
th visitor of my site.
</body>
 
maybe you could use a hidden frame to contain the cgi generated jscript - like jemminger suggests
<html>
<head>
<script language=&quot;javascript&quot;>
function update(count){
// here goes code to read the count from the page and then
// update the inner html like so
top.div_id.innerHTML=count;
</script>
</head>

<body onload=&quot;setTimeout('location.reload();',1000);update(<?= $count ?>);&quot;>

</body>
</html>

I'm a newbie, so i doubt this will work already but I hope the idea gets across (and helps)

Tels Mixed Linux/Win2000 Network Administrator
 
Do this for an iframe without borders:

<iframe src=&quot;counter.php&quot; style=&quot;border:0;&quot;></iframe>

Rick It's a pleasure to know that I've helped you. If I have,
please click the link below to let me know. :)
 
if i create the iframe using

<iframe src=&quot;counter.php&quot; style=&quot;border:0;&quot;></iframe>

i still get a huge white rect with a border. iam using ie5.5

isn't there another way?
 
<iframe src=&quot;counter.php&quot; style=&quot;border:0 solid white;width:50;height:10;&quot;></iframe>

Rick It's a pleasure to know that I've helped you. If I have,
please click the link below to let me know. :)
 
You could achieve the same results by using a frameset with a hidden page, and having that hidden page use DHTML to update a div/id.

The source of the hidden frameset should be something like

<html>
<head>
<script language=&quot;javascript&quot;>
function update(){
// here goes code to read the count from the page and then
// update the inner html like so
top.div_id.innerHTML='<?=$count?>';
</script>
</head>

... and div_id should be the name of the div (on the unhidden frame) where you want the string to go.

If you want to see an example of a frame src using JScript to alter text on the parent page, look at Mixed Linux/Win2000 Network Administrator
 
i don't get it! that was all my question...-HOW CAN I GET THE RESPONSE FROM THE SCRIPT?

but now i solved the problem this way:

<script language=javascript src=&quot;counter.php&quot;></script>


counter.php:

...
echo &quot;document.write(\&quot;$counter\&quot;);&quot;;
 
I had assumed that you had included the file like this:

<? include(&quot;counter.php&quot;); ?>

At the beginning of the page. And could access the variable by doing:

<?= $count ?>

sorry.

Rick if(($question==&quot;has been bugging me&quot;
AND $answer==&quot;fixed the problem&quot;) OR $answer==&quot;really good post&quot;){
print(&quot;Star&quot;);
}else{
print(&quot;Thanks.&quot;);
}
 
> <? include(&quot;counter.php&quot;); ?>
> At the beginning of the page. And could access the
> variable by doing:
> <?= $count ?>

but this is a server side solution and i don't want to reload the whole page. i wnat that my javascript fetches the number from the php script (and i don't know how to accomplish this).
to dynamically insert the count as text in the html is no problem.
 
Are you saying that you want the user to see a value that constantly updates itself whenever another user accesses the page? ie. I goto the page and the count is 674 and 5 minutes later it automatically becomes 675 because of another hit on the site. Is that what you want?

Rick if(($question==&quot;has been bugging me&quot;
AND $answer==&quot;fixed the problem&quot;) OR $answer==&quot;really good post&quot;){
print(&quot;Star&quot;);
}else{
print(&quot;Thanks.&quot;);
}
 
yes exactly this is what i want. the javascript could set a interval-timer and update the counter automatically every few seconds.
 
codymanix,

the best (if not only) way you could accomplish this would be to use an iframe with a combination of server-side and client-side script. the iframe source page would look something like this:

<html>
<body onload=&quot;window.setTimeout('location.reload();',5000);&quot;>
<?php echo $pageHits ?> page hits
</body>
</html>

this will display the number of hits stored in $pageHits at runtime, then refresh itself every five seconds.

hope this helps!
=========================================================
if (!succeed) try();
-jeff
 
Basically, you need to use JavaScript (or VBScript if you love IE) to update and reflow your page dynamically (without refreshing it), and a refresh command to fetch the updated data (hidden from the user, otherwise whats the point?).

The only other refreshable browser objects I can think of other than the browser itself are CSS Layers, frames, hidden frames or IFRAMES. Basically anything with a src property.

I think I've seen an active counter that works on (but DO NOT go there if you are politically correct - it's not porn but it's full on)

alternatively another other way to accomplish your mission could be to use a true Java applet, or research some sort of client pull/server push technologies.
Mixed Linux/Win2000 Network Administrator
 
Stop the clock, I thought of another, perfectly good way of doing this.

You can use PERL and I think, PHP as an image within a document. You'll need to figure out how the script works, but you could them simply use a JScript to refresh the image source at specified intervals.

I'd tread lightly on the issue of bandwidth and refresh rate, but I believe a great many free counters use image generating CGI's so it shouldn't be hard to find functioning examples.. If you simply want it in text, you'll need to either draw the images to look like text, or I think use one of the other methods discussed Mixed Linux/Win2000 Network Administrator
 
You can also use ASP as an image source, and we use that to keep competing sites from linking in and using commercial webcam photos one of our clients uploads to our servers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top