Forum Moderators: open

Message Too Old, No Replies

how to run jquery function in php script

         

MarrkoR

11:07 am on Jul 22, 2009 (gmt 0)

10+ Year Member



Hi, how to activate the function?
I have sample:


<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function()
{
//Paleidimo funkcija
var time.ready(function()
{
$('#time_show').load('time.php?nu='+ Math.random());
});

});
</script>
</head>
<body>
<?php
if($name == "Marrko") {run jquery time functions}
?>
<div id="time_show"></div>

Mike521

3:07 pm on Jul 22, 2009 (gmt 0)

10+ Year Member



it would have to be something like this:

<body>
<?php
if($name == "Marrko") { ?>
<script type="text/javascript">
//do jquery stuff
</script>
<? } ?>

MarrkoR

4:18 pm on Jul 22, 2009 (gmt 0)

10+ Year Member



This only work wiht IE.

Mike521

4:43 pm on Jul 22, 2009 (gmt 0)

10+ Year Member



then the problem is with the javascript. What is the code you're trying to run exactly? (the code that goes in the '//do jquery stuff' section from my example)

MarrkoR

10:05 pm on Jul 22, 2009 (gmt 0)

10+ Year Member



time_ref2 run fine(work), but stop function in time.php don't.


<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script>$(document).ready(function(){
<?php
$city = "Paris";
if($city == "Paris"){ ?>
var time_ref2 = setInterval(function()
{
$('#time_now').load('time.php?id='+ Math.random());
}, 1000);
<?php } ?>
});
</script>
</head> <body> <div align="center" id="time_now">--:--:--</div>
</body> </html>

time.php:


<script type="text/javascript" src="jquery.js"></script>
<?php
$sec = date("s");
echo $sec;
?>
<script>$(document).ready(function(){
<?php
if($sec == "30"){ ?>
var stop = setInterval(function()
{
clearInterval(time_ref2);
}, 2000);
<?php } ?>
});
</script>

Mike521

2:15 pm on Jul 23, 2009 (gmt 0)

10+ Year Member



ok it seems to me there are a few problems that need to be fixed, first I want to review what you're trying to do, let me know if this is correct:

1. once per second, load the contents of time.php and display them in the #time_now div
2. in time.php, get the current time in seconds and print it out. If seconds = 30, stop the interval on the parent page.

the problems I see are:

1. time.php doesn't know what time_ref2 is. time_ref2 was created on the parent page, so it doesn't exist on time.php
2. since time.php doesn't know what time_ref2 is, it can't stop the associated interval.
3. I'm not sure why you are setting an interval on the stop line anyway -- why not just clearInterval(time_ref2) without surrounding it in a setInterval?

MarrkoR

2:29 pm on Jul 23, 2009 (gmt 0)

10+ Year Member



Yes, you right.

But how this fix, sorry, i don't understand

Mike521

2:44 pm on Jul 23, 2009 (gmt 0)

10+ Year Member



I wouldn't bother trying to pass time_ref2 over to time.php

instead I would have some sort of a loop on the parent page that checks the contents of #time_now

something like:

if ( $('#time_now').html() == "30" ) {
// stop the interval
}

There are probably better ways to do it but off the top of my head that's what I'm thinking of first..