Don't bother guys. The entire code doesn't work. I tried and i fail. So let's try to solve the important part first. I tried to follow hallsofvallhala's specifications on how to build your code with ajax and this is what i got. The problem is that is doesn't insert anything into my table.
Please take your time and read the entire scripts carefully to help me spot the mistake.
In military_research.php
Code: Select all
<link href = "MainScreenStyle.css" rel="stylesheet" type="text/css" />
<?php
include 'Research.php';
print"<td><div id='Military_tree_div'>";
print"<form name='ships_form'>";
include 'militaryres_buttons.php';
print"</form>";
print"</div>";
?>
In militaryres_buttons.php
Code: Select all
<?php
echo "<form method='post' action='javascript:researched(33)'><input type='submit' name='fighter' value='Fighter'></form><br>";
//other buttons doesn't matter only fighter for now.
echo "<form method='post' action='javascript:researched(34)'><input type='button' name='bomber' value='Bomber'></form><br>";
print"<input type='button' value='Frigate' disabled name='frigate' onclick='enable_destroyer()'><br>";
print"<input type='button' value='Destroyer' disabled name='destroyer' onclick='enable_cruiser()'><br>";
print"<input type='button' value='Cruiser' disabled name='cruiser' onclick='enable_dreadnought()'><br>";
print"<input type='button' value='Dreadnought' disabled name='dreadnought' onclick='enable_carrier()'><br>";
print"<input type='button' value='Carrier' disabled name='carrier' onclick='enable_fighter_bomber()'><br>";
print"<input type='button' value='Heavy fighter' disabled name='h_fighter' onclick='enable_super_carrier()'>";
print"<input type='button' value='Heavy bomber' disabled name='h_bomber' onclick='enable_super_carrier()'><br>";
print"<input type='button' value='Super carrier' disabled name='super_carrier'>";
?>
In function.js
Code: Select all
function researched(res_id)
{
//alert(playertag);
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
//idk what to write in here. Main idea is that this if should enable the next button, frigate (bomber is already enabled)
}}
var queryString = "?res_id=" + res_id;
//alert(queryString);
ajaxRequest.open("GET", "research_index.php" + queryString, true);
ajaxRequest.send(null);
}
And in research_index.php
Code: Select all
<link href = "MainScreenStyle.css" rel="stylesheet" type="text/css" />
<?php
include_once 'connect.php';
if (isset($_SESSION['player']))
{
$player=$_SESSION['player'];
$playerinfo = "SELECT * from players where name='$player'";
$playerinfo2 = mysql_query($playerinfo) or die ("Could not get player stats!");
$playerinfo3 = mysql_fetch_array($playerinfo2);
}
else
{
echo "Not logged in <br><br> <a href='login.php'> Login </a>";
exit;
}
$player_id=$playerinfo3['id'];
$res_id=$_GET['res_id'];
$res_info= "SELECT * from researches WHERE Research_id = $res_id";
$res_info2= mysql_query($res_info) or die("Could not select research");
$res_info3= mysql_fetch_array($res_info2);
if($res_id == 33)//in the table of all available technologies (Researches) the fighter has the id = 33. So if the id is 33 it should insert into the researched table the fighter technology
{
$researched_table = "INSERT INTO researched(Player_id,Name) VALUES ('$player_id','$res_info3[Name]')"; // it doesn't insert anything into the researched table
mysql_query($researched_table) or die("Could not insert research");;
}
?>
Now, if i was hallsofvallhala i would insert this code
Code: Select all
if (isset($_SESSION['player']))
{
$player=$_SESSION['player'];
$playerinfo = "SELECT * from players where name='$player'";
$playerinfo2 = mysql_query($playerinfo) or die ("Could not get player stats!");
$playerinfo3 = mysql_fetch_array($playerinfo2);
}
else
{
echo "Not logged in <br><br> <a href='login.php'> Login </a>";
exit;
}
$player_id=$playerinfo3['id'];
into military_research.php, the first php script , but i don't understand how will the ajax function get the player_id to insert it into queryString together with res_id. In his tutorial ajax/javascript tutorial pid for me comes from nowhere

)
Code: Select all
function traveled(direction)
{
//alert(playertag);
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
//var test = ajaxRequest.responseText;
//document.getElementById('title').innerHTML=ajaxRequest.responseText;
//window.location.href = "test.php";
//alert(ajaxRequest.responseText);
playertag = ajaxRequest.responseText;
}}
var newplayertag = playertag.split("v");
var queryString = "?pid=" + pid + "&direction=" + direction + "&playertag=" + newplayertag[1];
//alert(queryString);
ajaxRequest.open("GET", "travelmap.php" + queryString, true);
ajaxRequest.send(null);
setTimeout("travel();", 500);
}
Please take your time and read the entire scripts carefully to help me spot the mistake. This portion of coding kept me from going on for days!!!