Jump to content

[SOLVED] php function inside javascript.


DEVILofDARKNESS

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Okay I solved it without any ajax :P

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);
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.