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

Javascript not working

The1corrupted
100+
P: 134
I don't know if this is the right forum or not, but right now, my Javascript, that is embedded in PHP, is not working! It works everywhere else but not here. HELP!!

[PHP]
<?php
$soldnumber=$_POST['soldier'];
$cavnumber=$_POST['cavalry'];
$artnumber=$_POST['artillary'];

if ($marray['havetank']==1) {
$tanknumber=$_POST['tank'];
$tanktotal=$tanknumber*$tankcost;
}
if ($marray['haveair']==1) {
$fnumber=$_POST['fighter'];
$bnumber=$_POST['bomber'];
$btotal=$fnumber*$fcost;
$btotal=$bnumber*$bcost;
}

$currmoney=$genarray['money'];

$soldtotal=($soldcost*$soldnumber);
$cavtotal=($cavcost*$cavnumber);
$arttotal=($artcost*$artnumber);

if ($soldnumber!=NULL OR $cavnumber!=NULL OR $artnumber!=NULL) {
if ($soldnumber!=NULL AND $soldnumber>0) {
if ($soldtotal<$currmoney) {
$currmoney=($currmoney-$soldtotal);
$totals2=$totals2.$soldnumber." soldiers.\n";
mysql_query("UPDATE `military` SET `soldiers`=`soldiers`+'$soldnumber' WHERE `id`='$id'");
mysql_query("UPDATE `users` SET `money`=`money`-'$soldtotal' WHERE `id`='$id'");
} else {
echo "<script language=\"javascript\" type=\"text/javascript\">alert(\"Not enough for soldiers!\")</script>";
}
}

if ($cavnumber!=NULL AND $cavnumber>0) {
if ($cavtotal<$currmoney) {
$currmoney=($currmoney-$cavtotal);
$totals2=$totals2.$cavnumber." cavalry.\n";
mysql_query("UPDATE `military` SET `cavalry`=`cavalry`+'$cavnumber' WHERE `id`='$id'");
mysql_query("UPDATE `users` SET `money`=`money`-'$cavtotal' WHERE `id`='$id'");
} else {
echo "<script language=\"javascript\" type=\"text/javascript\">alert(\"Not enough for cavalry!\")</script>";
}
}

if ($artnumber!=NULL AND $artnumber>0) {
if ($arttotal<$currmoney) {
$currmoney=($currmoney-$arttotal);
$totals2=$totals2.$artnumber." artillary.\n";
mysql_query("UPDATE `military` SET `artillary`=`artillary`+'$artnumber' WHERE `id`='$id'");
mysql_query("UPDATE `users` SET `money`=`money`-'$arttotal' WHERE `id`='$id'");
} else {
echo "<script language=\"javascript\" type=\"text/javascript\">alert(\"Not enough for artillary!\")</script>";
}
}

if ($tanknumber!=NULL AND $tanknumber>0 AND $genarray['havetank']==1) {
if ($tanktotal<$currmoney) {
$currmoney=($currmoney-$tanktotal);
$totals2=$totals2.$tanknumber." tanks.\n";
mysql_query("UPDATE `military` SET `tanks`=`tanks`+'$tanknumber' WHERE `id`='$id'");
mysql_query("UPDATE `users` SET `money`=`money`-'$tanktotal' WHERE `id`='$id'");
} else {
echo "<script language=\"javascript\" type=\"text/javascript\">alert(\"Not enough for tanks!\")</script>";
}
}

if ($fnumber!=NULL AND $fnumber>0 AND $genarray['haveair']==1) {
if ($ftotal<$currmoney) {
$currmoney=($currmoney-$ftotal);
$totals2=$totals2.$fnumber." fighters.\n";
mysql_query("UPDATE `military` SET `fighters`=`fighters`+'$fnumber' WHERE `id`='$id'");
mysql_query("UPDATE `users` SET `money`=`money`-'$ftotal' WHERE `id`='$id'");
} else {
echo "<script language=\"javascript\" type=\"text/javascript\">alert(\"Not enough for fighters!\")</script>";
}
}

if ($bnumber!=NULL AND $bnumber>0 AND $genarray['haveair']==1) {
if ($btotal<$currmoney) {
$currmoney=($currmoney-$btotal);
$totals2=$totals2.$bnumber." bombers.\n";
mysql_query("UPDATE `military` SET `bombers`=`bombers`+'$bnumber' WHERE `id`='$id'");
mysql_query("UPDATE `users` SET `money`=`money`-'$btotal' WHERE `id`='$id'");
} else {
echo "<script language=\"javascript\" type=\"text/javascript\">alert(\"Not enough for bombers!\")</script>";
}

echo "<script language=\"javascript\" type=\"text/javascript\">alert(\"You Purchased:\n\" + \"".$totals2."\");
parent.location=\"viewempire.php?id=\"".$_SESSION['user']."\"
</script>";
}
}
?>
[/PHP]
Nov 22 '07 #1
Share this Question
Share on Google+
6 Replies


Atli
Expert 5K+
P: 5,058
Hi.

How is it not working as you expect it to?

Could you show us the source from your browser, to see what exactly it is that your browser is getting.
Nov 23 '07 #2

The1corrupted
100+
P: 134
Actually, I've decided to trash that method. Instead, I have a new issue. I want there to be a confirmation message with javascript and if the user hits "Ok", then all the mysql_queries go through. Else, he's simply redirected so I'm just wondering how I can transfer variables from javascript to php.. And how I should go about structuring that...
Nov 24 '07 #3

Markus
Expert 5K+
P: 6,050
Actually, I've decided to trash that method. Instead, I have a new issue. I want there to be a confirmation message with javascript and if the user hits "Ok", then all the mysql_queries go through. Else, he's simply redirected so I'm just wondering how I can transfer variables from javascript to php.. And how I should go about structuring that...
Cookies maybe? A rough thing to do, but i know nothing!
Nov 24 '07 #4

P: 88
... so I'm just wondering how I can transfer variables from javascript to php.. And how I should go about structuring that...
One method, and the one that I'm using, is to use a XhttpRequest, and posting/sending (POST/GET) your variables to php.
Nov 24 '07 #5

The1corrupted
100+
P: 134
How does that all work?
Nov 24 '07 #6

P: 88
It goes by the name of AJAX, basic stuff:
http://www.w3schools.com/php/php_ajax_intro.asp

Also, if you do not need to send the variable immediately to php, rather you could wait until the nex page load, there are such alternatives, as a quick google search revealed; google javascript variable to php.

(btw, this is now officially a javascript question...)
Nov 24 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.