DEVILofDARKNESS Posted October 16, 2009 Share Posted October 16, 2009 Okay I have this piece of code: <script type="text/javascript"> num = <?= $ned; ?> var timer; function countdown() { num--; document.getElementById('countdown_ele').innerHTML = num; if(num < 1){ clearTimeout(timer); document.getElementById('countdown_ele').innerHTML = 'FINISHED'; <?= update(); ?> } } function init() { document.getElementById('countdown_ele').innerHTML = num; timer = setInterval("countdown()",1000); } </script> The js accepts the $ned that is given by php, but does not start the function update(); Is this because js can only accept vars/strings/... from php but not execute a function.? Or is there an other way that I can solve this? P.S.: this is a piece of a php script. Quote Link to comment Share on other sites More sharing options...
cags Posted October 16, 2009 Share Posted October 16, 2009 providing update() returns a string value, when your scipt is first called it will run that function and output the returned value. But it does this when the script is originally requested, not as part of a call to the function countdown(). If you wish to actually run a PHP function each time the countdown() function is called you will need to use AJAX. Quote Link to comment Share on other sites More sharing options...
haku Posted October 17, 2009 Share Posted October 17, 2009 Is this because js can only accept vars/strings/... from php but not execute a function.? That's exactly what it is. PHP is run on the server, Javascript is run on the local computer. So if it's a string, the php outputs the string on the server, then sends that string to the computer as text - the php doesn't exist as far as the browser is concerned. But if it's a function, the function is run on the server, returns a blank value, and sends it to the browser. The javascript tries to run the code, but there is no php function there (php is only run on the server), just blank output, so it doesn't work. Look at your source code to see what I mean. The only way to do what you want is through ajax, like the poster above me said. Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted October 17, 2009 Author Share Posted October 17, 2009 Okay thanks I will have a look at it. Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted October 17, 2009 Author Share Posted October 17, 2009 Okay I solved it without any ajax I did with javascript: document.getElementById('countdown_ele').innerHTML = 'FINISHED'; var head = document.getElementsByTagName('head').item(0); var script = document.createElement('script'); script.setAttribute( 'type', 'text/javascript' ); script.setAttribute( 'src', './functions.php?func=updateart' ); head.insertBefore( script, head.firstChild ); I know it calls the functions.php,(saw it in firebug) but somehow it just does the delete query and not the insert or update queries :f what have I done wrong? if($_GET['func']=='updateart'){ $qry = "SELECT * FROM countdown WHERE (nation_id='$nationid') && (weapbuild='weap')"; $res = mysql_query($qry); $ass = mysql_fetch_assoc($res); $qury = "SELECT ammount FROM region_weapons WHERE (weapon_id='" . $ass['wb_id'] . "') && (region_id='" . $ass['region_id'] . "')"; $resu = mysql_query($qury); if(mysql_num_rows($resu) > 0){ $query2 = "UPDATE region_weapons SET ammount = 'ammount + " . $ass['extra'] . "' WHERE (weapon_id='" . $ass['wb_id'] . "') && (region_id='" . $ass['region_id'] . "')"; $res = mysql_query($query2); }else{ $query3 = "INSERT INTO region_weapons('region_id','weapon_id','ammount') VALUES ('" . $ass['region_id'] . "','" . $ass['wb_id'] . "','" . $ass['extra'] . "')"; $res = mysql_query($query3); } $query4 = "DELETE FROM countdown WHERE (wb_id='" . $ass['wb_id'] . "') && (region_id='" . $ass['region_id'] . "')"; $result = mysql_query($query4); } Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted October 18, 2009 Author Share Posted October 18, 2009 I found out, that firebug actually gave me an error: updateart is not defined functions.php?func=updateart() the two brackets: () are not in the javascript but were shown in red by firebug, I can though open functions.php?func=updateart manually... document.getElementById('countdown_ele').innerHTML = 'FINISHED'; var head = document.getElementsByTagName('head').item(0); var script = document.createElement('script'); script.setAttribute( 'type', 'text/javascript' ); script.setAttribute( 'src', './functions.php?func=updateart' ); head.insertBefore( script, head.firstChild ); So what is the problem? Quote Link to comment Share on other sites More sharing options...
haku Posted October 18, 2009 Share Posted October 18, 2009 The problem is you can't add a script dynamically that way. Again, you need to do it using Ajax. Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted October 18, 2009 Author Share Posted October 18, 2009 I don't want to go the ajax way because I don't know anything of it, so here a last try: function countdown() { num--; document.getElementById('countdown_ele').innerHTML = num; if(num < 1){ clearTimeout(timer); self.location="functions.php?func=updateart"; } } if($_GET['func']=='updateart'){ session_start(); require './checkuserid.php'; $qry = "SELECT * FROM countdown WHERE (nation_id='$nationid') && (weapbuild='weap')"; $res = mysql_query($qry); $ass = mysql_fetch_assoc($res); $qury = "SELECT ammount FROM region_weapons WHERE (weapon_id='" . $ass['wb_id'] . "') && (region_id='" . $ass['region_id'] . "')"; $resu = mysql_query($qury); if(mysql_num_rows($resu) > 0){ $query2 = "UPDATE region_weapons SET ammount = 'ammount + " . $ass['extra'] . "' WHERE (weapon_id='" . $ass['wb_id'] . "') && (region_id='" . $ass['region_id'] . "')"; $res = mysql_query($query2); }else{ $query3 = "INSERT INTO region_weapons('region_id','weapon_id','ammount') VALUES ('" . $ass['region_id'] . "','" . $ass['wb_id'] . "','" . $ass['extra'] . "')"; $res = mysql_query($query3); } $query4 = "DELETE FROM countdown WHERE (wb_id='" . $ass['wb_id'] . "') && (region_id='" . $ass['region_id'] . "')"; $result = mysql_query($query4); header('location:./nationoverview.php'); } The php is loaded when the timer reaches zero and the script returns to nationoverview because of the header on the last line. No is there one problem, none of the query's is done:f Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted October 18, 2009 Author Share Posted October 18, 2009 Woohoow I solved it with: self.location="functions.php?func=updateart"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.