By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,198 Members | 917 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,198 IT Pros & Developers. It's quick & easy.

Passing values from JavaScript into MySQL/PHP

P: n/a
Hello

First of all i know that PHP is a server side, and JavaScript is a Client
side programming language, but i need to know if there is any way i can add
the variable "countDownInterval" from this particular FULL code into a MySQL
field.

Here's the drill:

I have a timer on a PHP webpage, and i want to prevent the user from doing a
refresh to reset the timer to it's full time (in this case 100), and i
though of since i can pass the variable from PHP into JavaScript i could
save the timer into a MySQL field and each time the user refreshs the page
it's fethed from MySQL and passed by PHP back into JavaScript.

So it user has only 5 seconds left, and uses a refresh, the script fetchs
the value 5 from MySQL table and continues to count down from there.

Is this possible? Let's me refrase that i only need a way to pass a variable
from this JavaScript into MySQL, and that the page cannot be refreshed by
it's own, since it has some pictures and it takes some time to load. the
timer is on a <ilayer>

HERE'S my example code, that shows how to pass values from PHP into
JavaScript, please complete it in a way i can pass variable
"countDownInterval" back into MySQL.

************************************************** **************************
********

<?PHP
$somePhpVariable=100;
?>

<script language="JavaScript">
//configure refresh interval (in seconds)
var countDownInterval=<?php echo $somePhpVariable ?>; // THIS IS WHERE
I PASS THE PHP VARIABLE TO JAVASCRIPT
//configure width of displayed text, in px (applicable only in NS4)
var c_reloadwidth=200
</script>

<ilayer id="c_reload" width=&{c_reloadwidth}; ><layer id="c_reload2"
width=&{c_reloadwidth}; left=0 top=0></layer></ilayer>

<script>

var countDownTime=countDownInterval+1;
function countDown(){
countDownTime--;
if (countDownTime <=0){
countDownTime=countDownInterval;
clearTimeout(counter)
window.location.reload()
return
}
if (document.all) //if IE 4+
document.all.countDownText.innerText = countDownTime+" ";
else if (document.getElementById) //else if NS6+
document.getElementById("countDownText").innerHTML =countDownTime+" "
else if (document.layers){ //CHANGE TEXT BELOW TO YOUR OWN
document.c_reload.document.c_reload2.document.writ e('Next <a
href="javascript:window.location.reload()">refresh </a> in <b
id="countDownText">'+countDownTime+' </b> seconds')
document.c_reload.document.c_reload2.document.clos e()
}
counter=setTimeout("countDown()", 1000);
}

function startit(){
if (document.all||document.getElementById) //CHANGE TEXT BELOW TO YOUR OWN
document.write('Next <a
href="javascript:window.location.reload()">refresh </a> in <b
id="countDownText">'+countDownTime+' </b> seconds')
countDown()
}

if (document.all||document.getElementById)
startit()
else
window.onload=startit

</script>
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
RootShell wrote:
Hello

First of all i know that PHP is a server side, and JavaScript is a
Client side programming language, but i need to know if there is any
way i can add the variable "countDownInterval" from this particular
FULL code into a MySQL field.


<snip>

AFAIK there are no Javascript functions that allow for MySQL interaction.
That aside, storing the value in the database seems an inefficient solution.

Have you tried putting the countdown variable into a cookie? Such a
procedure could be:
onload {
get countdown from cookie
if countdown is set
resume countdown
else
begin new countdown
}

When the user clicks the reload link, you could call a function that puts
the current countdown value into the cookie.

Hope that helps.

--

Jason, aka The Blue Raja
Jul 23 '05 #2

P: n/a
JRS: In article <41**********************@news.telepac.pt>, dated Mon,
26 Jul 2004 01:45:45, seen in news:comp.lang.javascript, RootShell
<AN***************@Netcabo.ptANTISPAM> posted :
counter=setTimeout("countDown()", 1000);


That will not count seconds accurately.

See current thread "Countdown timer inaccurate - losing time!?!" in
c.l.j, and the FAQ, via below.

Is a.c.l.j generally considered valid? I think Demon does not carry it.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.