imoen
07-16-2009, 06:19 PM
I need to make a javascript that when I am at a page within a game I play and click the javascript, it will get the value of a table cell, separate them to x and y variables and then put the values into a link that opens. The open link is of a php script that uses $_GET for the values of the variables to query my database.
I am terrible at javascript and have been trying to do this for 2 days now with no success. So I hope I can find help here.
Here's the scripts so it is easier to understand:
This is part of the html page that is from the game. I need to have the javascript get 432|608 and assign x=432 and y=608 in this case, although those number change, the place in the table that that they are displayed do not change.
<td>Coordinates:</td>
<td><a href="game.php?village=19392&s=map&x=432&y=608">432|608</a></td>
<html>
<head>
<title></title>
<meta http-equiv="Content-Language" content="de">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Page-Enter" content="revealTrans(Duration=0.1,Transition=4)">
<link rel="stylesheet" type="text/css" href="kingsage.css">
<link rel="shortcut icon" href="favicon.ico" type="image/ico">
<script src="js/kingsage.js?hash=1e4c479f8ba66202f3b1bfb74414eef4"
type="text/javascript"></script>
<script
src="js/mootools-1.2-core-nc.js?hash=1e4c479f8ba66202f3b1bfb74414eef4"
type="text/javascript"></script>
<script
src="js/mootools-1.2-more.js?hash=1e4c479f8ba66202f3b1bfb74414eef4"
type="text/javascript"></script>
<script src="js/map_src.js?hash=1e4c479f8ba66202f3b1bfb74414eef4"
type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
//<![CDATA[
lang = new Array();
lang['DAY'] = 'Day';
lang['DAYS'] = 'Days';
//]]>
</script>
<div
style="background: rgb(0, 0, 0) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"
align="center">
<table style="border-collapse: collapse; height: 100%;" width="100%"
cellpadding="0" cellspacing="0">
<tbody>
<tr valign="top">
<td width="50%" align="right">
<br>
</td>
<td style="width: 840px;">
<table
style="border-collapse: collapse; width: 840px; height: 100%;"
cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="lay_content" valign="top"><a name="head2"></a>
<div class="contentpane">
<table class="borderlist" style="width: 420px;">
<tbody>
<tr>
<th colspan="2">Abandoned settlement</th>
</tr>
<tr>
<td>Coordinates:</td>
<td><a href="game.php?village=19392&s=map&x=432&y=608">432|608</a></td>
</tr>
<tr>
<td>Points:</td>
<td>390</td>
</tr>
<tr>
<td>Player:</td>
<td><br>
</td>
</tr>
<tr>
<td>Alliance:</td>
<td><a href="game.php?village=19392&s=info_ally&id="></a><br>
</td>
</tr>
</tbody>
</table>
<br>
</div>
</td>
</tr>
</tbody>
</table>
</td>
<td width="50%" align="left">
<br>
</td>
</tr>
</tbody>
</table>
</div>
<div id="settlement" style="display: none;"></div>
<script type="text/javascript">
//<![CDATA[
startCounter();
//]]>
</script>
</body>
</html>
Here's the php script that will process the x and y
<?php
putenv("TZ=Europe/Berlin");
session_start();
include("includes/config.php");
$username = $_GET['username'];
$pass = $_GET['pass'];
$x = $_GET['x'];
$y = $_GET['y'];
$now = time();
//confirm user can make claim
global $conn;
$q = "SELECT password FROM users WHERE username = '$username' AND active = 1";
$result = mysql_query($q,$conn);
if(!$result || (mysql_numrows($result) < 1))
{
$data = "Your identity can not be authenticated";
}
else
{
$dbarray = mysql_fetch_array($result);
if($pass == $dbarray['password'])
{
$continue = true;
}
else
{
$data = "Your password can not be authenticated";
}
}
//if can claim then check the village
if($continue)
{
$q = "SELECT * FROM `claims` WHERE x = '$x' AND y = '$y' AND state = 1";
$village = mysql_query($q, $conn);
$result = mysql_fetch_assoc($village);
if(!$result)
{
$can_claim = true;
}
elseif($result)
{
if($result['username'] == $username)
{
$data = "You already have an active claim on ($x|$y)";
}
elseif($result['username'] <> $username)
{
$claimer = $result['username'];
$data = "$claimer already has an active claim on ($x|$y)";
}
}
if($can_claim == true)
{
global $conn;
$claim_made = time();
$claim_expires = $claim_made + 432000;
$state = 1;
$q = "INSERT INTO `claims` (x, y, username, claim_made, claim_expires, state) VALUES ('$x', '$y', '$username', '$claim_made', '$claim_expires', '$state')";
$add_claim = mysql_query($q, $conn);
if($add_claim)
{
$data = "Success! You claimed ($x|$y)";
}
else
{
$data = "Error: Your claim on ($x|$y) did not complete.";
}
}
echo "<SCRIPT>alert(\"$data\");</SCRIPT>";
}
?>
The javascript should link to this php script and send the variables like this: 'http://twv-kingsage.tw-family.us/auto_add_claim.php?username=username&pass=md5_password&x="+x+"&y="+y'
Can someone tell me how to extract the values for x and y from the page and get them to the link?
Thanks for the help, it is much appreciated.
I am terrible at javascript and have been trying to do this for 2 days now with no success. So I hope I can find help here.
Here's the scripts so it is easier to understand:
This is part of the html page that is from the game. I need to have the javascript get 432|608 and assign x=432 and y=608 in this case, although those number change, the place in the table that that they are displayed do not change.
<td>Coordinates:</td>
<td><a href="game.php?village=19392&s=map&x=432&y=608">432|608</a></td>
<html>
<head>
<title></title>
<meta http-equiv="Content-Language" content="de">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Page-Enter" content="revealTrans(Duration=0.1,Transition=4)">
<link rel="stylesheet" type="text/css" href="kingsage.css">
<link rel="shortcut icon" href="favicon.ico" type="image/ico">
<script src="js/kingsage.js?hash=1e4c479f8ba66202f3b1bfb74414eef4"
type="text/javascript"></script>
<script
src="js/mootools-1.2-core-nc.js?hash=1e4c479f8ba66202f3b1bfb74414eef4"
type="text/javascript"></script>
<script
src="js/mootools-1.2-more.js?hash=1e4c479f8ba66202f3b1bfb74414eef4"
type="text/javascript"></script>
<script src="js/map_src.js?hash=1e4c479f8ba66202f3b1bfb74414eef4"
type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
//<![CDATA[
lang = new Array();
lang['DAY'] = 'Day';
lang['DAYS'] = 'Days';
//]]>
</script>
<div
style="background: rgb(0, 0, 0) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"
align="center">
<table style="border-collapse: collapse; height: 100%;" width="100%"
cellpadding="0" cellspacing="0">
<tbody>
<tr valign="top">
<td width="50%" align="right">
<br>
</td>
<td style="width: 840px;">
<table
style="border-collapse: collapse; width: 840px; height: 100%;"
cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="lay_content" valign="top"><a name="head2"></a>
<div class="contentpane">
<table class="borderlist" style="width: 420px;">
<tbody>
<tr>
<th colspan="2">Abandoned settlement</th>
</tr>
<tr>
<td>Coordinates:</td>
<td><a href="game.php?village=19392&s=map&x=432&y=608">432|608</a></td>
</tr>
<tr>
<td>Points:</td>
<td>390</td>
</tr>
<tr>
<td>Player:</td>
<td><br>
</td>
</tr>
<tr>
<td>Alliance:</td>
<td><a href="game.php?village=19392&s=info_ally&id="></a><br>
</td>
</tr>
</tbody>
</table>
<br>
</div>
</td>
</tr>
</tbody>
</table>
</td>
<td width="50%" align="left">
<br>
</td>
</tr>
</tbody>
</table>
</div>
<div id="settlement" style="display: none;"></div>
<script type="text/javascript">
//<![CDATA[
startCounter();
//]]>
</script>
</body>
</html>
Here's the php script that will process the x and y
<?php
putenv("TZ=Europe/Berlin");
session_start();
include("includes/config.php");
$username = $_GET['username'];
$pass = $_GET['pass'];
$x = $_GET['x'];
$y = $_GET['y'];
$now = time();
//confirm user can make claim
global $conn;
$q = "SELECT password FROM users WHERE username = '$username' AND active = 1";
$result = mysql_query($q,$conn);
if(!$result || (mysql_numrows($result) < 1))
{
$data = "Your identity can not be authenticated";
}
else
{
$dbarray = mysql_fetch_array($result);
if($pass == $dbarray['password'])
{
$continue = true;
}
else
{
$data = "Your password can not be authenticated";
}
}
//if can claim then check the village
if($continue)
{
$q = "SELECT * FROM `claims` WHERE x = '$x' AND y = '$y' AND state = 1";
$village = mysql_query($q, $conn);
$result = mysql_fetch_assoc($village);
if(!$result)
{
$can_claim = true;
}
elseif($result)
{
if($result['username'] == $username)
{
$data = "You already have an active claim on ($x|$y)";
}
elseif($result['username'] <> $username)
{
$claimer = $result['username'];
$data = "$claimer already has an active claim on ($x|$y)";
}
}
if($can_claim == true)
{
global $conn;
$claim_made = time();
$claim_expires = $claim_made + 432000;
$state = 1;
$q = "INSERT INTO `claims` (x, y, username, claim_made, claim_expires, state) VALUES ('$x', '$y', '$username', '$claim_made', '$claim_expires', '$state')";
$add_claim = mysql_query($q, $conn);
if($add_claim)
{
$data = "Success! You claimed ($x|$y)";
}
else
{
$data = "Error: Your claim on ($x|$y) did not complete.";
}
}
echo "<SCRIPT>alert(\"$data\");</SCRIPT>";
}
?>
The javascript should link to this php script and send the variables like this: 'http://twv-kingsage.tw-family.us/auto_add_claim.php?username=username&pass=md5_password&x="+x+"&y="+y'
Can someone tell me how to extract the values for x and y from the page and get them to the link?
Thanks for the help, it is much appreciated.