Jump to content

Issue when using external java script file $how to display errors with firebug?


Recommended Posts

for starters anyone know how to use firebug cause I cant figure it out. Like where does it show errors at? guess i can look in the firebug forum anyways.

 

my ajax script works perfectly with the java/ajax script on the page but as soon as I make it in a seperate file for the page only half of the ajax script works. I"m posting the code by not showing me passing all the variables so its smaller. I tested the php page zero errors. so no fatal errors stoping the page, but still like i said posting the java script on the page works fine. Its just when I make it as a included link. Maybe my idea of how including a java script is all wrong and i can only call java script functions from a external script?

 

<script type="text/javascript" src="/system_lords/jquery/evaluate.js"></script>

 

 

$(document).ready(function() {        

$("#evaluate").click(function(event) {          
	event.preventDefault();
	$.ajax({            

		url: "/System_Lords/ajax/include_evaluate.php",            

		type: 'POST',            

		dataType: 'json',            

		data: { 

		attacker_jaffa: $('#jaffa').val(),           

		defender_jaffa: '<?php echo $defender_jaffa; ?>'         
    

			},            

			success: function(response) {              

				$('#evaluate_box').html(response.evaluate); 

				}        

				});        

				});      

				});  

 

 

And here is the php file that the jquery is reading

 

<?php
require("../functions/function_battle.php");


/*/ ******************************************** EVALUATE THE ATTACK **************************************************************** /*/ 
	// evaluates rather or not the player will win the battle


	// prevents sending a blank form / if all fields are empty
	if(empty($_POST['attacker_jaffa']) &&  empty($_POST['attacker_staff_cannons']) && empty($_POST['attacker_bombers']) && empty($_POST['attacker_mother_ships'])) {



				echo json_encode(array("evaluate" => 

					"<table width='100%' align='center' border='1' cellspacing='1' cellpadding='8'>
					<tr class='odd'><th style='font-size:1em; letter-spacing:2px;'>You havn't selected any units to attack with.</th></tr>
					</table>"));

}else{






//******************************************************Attacker Points****************************************************************************************************					

							// amount * setting
				$o_jaffa = ($_POST['attacker_jaffa'] * $_POST['offense_jaffa']);	
				$o_staff_cannons = ($_POST['attacker_staff_cannons'] * $_POST['offense_staff_cannons']);							
				$o_bombers = ($_POST['attacker_bombers'] * $_POST['offense_bombers']);	
				$o_mother_ships = ($_POST['attacker_mother_ships'] * $_POST['offense_mother_ships']);
							// raw attack points of all attacker units
				$attacker_points = ($o_jaffa + $o_staff_cannons + $o_bombers + $o_mother_ships);

						// figures the bonus's for the attacker
				$attacker_bonus = ($attacker_points * $_POST['attackers_weapons_tech']);
						// adds the attacker poitns with the bonus to give a total
				$attacker_total = ($attacker_points + $attacker_bonus);	


//******************************************************Defender Points****************************************************************************************************					
										// amount * setting
						$d_jaffa = ($_POST['defender_jaffa'] * $_POST['defense_jaffa']);	
						$d_staff_cannons = ($_POST['defender_staff_cannons'] * $_POST['defense_staff_cannons']);
						$d_bombers = ($_POST['defender_bombers'] * $_POST['defense_bombers']);
						$d_death_gliders = ($_POST['defender_death_gliders'] * $_POST['defense_death_gliders']);
						$d_mother_ships = ($_POST['defender_mother_ships'] * $_POST['defense_mother_ships']);
						$d_staff_turrets = ($_POST['defender_staff_turrets'] * $_POST['defense_staff_turrets']);
						$d_staff_fortress = ($_POST['defender_fortress'] * $_POST['defense_fortress']);

						// adding up all the defense points of all the units
				$defender_points = ($d_jaffa + $d_staff_cannons + $d_bombers + $d_death_gliders + $d_mother_ships);
						// figures out how much defense points turrets and fortress gives
				$defender_bonus = ($defender_points * $d_staff_turrets) + ($defender_points * $d_staff_fortress) + ($defender_points * $_POST['defenders_weapons_tech']);
						// figures out how much defense with battle chance gives
				$chance = ($defender_points + $defender_bonus * $_POST['battle_chance']);
			 			// adds the total points plus the bonus plus the chance for the total
			 	$defender_total = ($defender_points + $defender_bonus);

/*/ ****************************************** Decides who wins the battle *************************************************************************************************** /*/				

list($condition_attacker,$condition_defender,$attacker_rate) = win_lose($attacker_total,$defender_total,$_POST['battle_chance']);


	$attacker_total = number_format($attacker_total);
	$defender_total = number_format($defender_total);
	$attacker_rate = $attacker_rate;



echo json_encode(array("evaluate" => 

"<table width='100%' align='center' border='1' cellspacing='1' cellpadding='8'>
<tr class='odd'><th style='font-size:1.2em; letter-spacing:2px;'>Evaluate</th></tr>
<tr><td>Attacker Points: $attacker_total</td></tr>  
<tr><td>Defender Points: $defender_total</td></tr>  
<tr><td>Chance of Success: $attacker_rate</td></tr>

</table>"));


}

 

 

Notice how I have a if else statement in the php file. the first statement works. where its checking for empty inputs. but for some reason when the statement is reading the else statement nothing happends. so because its reading the first side of the statement ajax is working i guess. the largest problem is i cant see any errors cause i cant figure out how firbug works on showing errors.

Your only passing two params (attacker_jaffa and defender_jaffa) to the php.

 

ps:

  JavaScript errors show up in the console in firebug.

  Your code is terribly formatted, try and make you indentation more consistant and 2-4 spaces is enough.

  You should be sending back just relevant data via json, you can then use jQuery to build your markup to house the data in.

 

thanks thorpe. yah i removed all the variables but two that i was passing cause it was a list of like 30 variables lol.

 

the script works when its on the page but when im using the script from an  external file the if statement works but the else statement does not. The if statement checks variables if its empty so i know its passing variables correctly. So i guess there isnt anything obvious that im doing wrong.

when using an external page it should be exactly same as placing the code on the page right? shouldnt matter what order i add in the external java script right? Cause java script is always read after php code anyways.

 

Oh wait i just thought of something. the differance between the if statement and the else statement is that the if statement is using all global variables from the page and the else statement is not. so if im grabing variables that are not global does the flow of the page matter as to where i include the external java script file?

 

on firebug i see where it shows the java script code. but its poorly formated. is this error area a window of its own? thanks for the feed back on my formating. Ive still let to get a good organized writing down. it is alot better then it used to be though lol. I dont know why i tab it over so much i need to stop that too.

Oh wait i just thought of something. the differance between the if statement and the else statement is that the if statement is using all global variables from the page and the else statement is not. so if im grabing variables that are not global does the flow of the page matter as to where i include the external java script file?

 

Ajax makes a completely separate request. None of the variables in the page it is called from are available to it, only what you pass along to it.

yah i meant global on the page that the jquery is getting the variables from to pass to the php file.

im passing all my variables like this.

 

After some more testing I figured out whats going on. apparently i cant past variables onto a external java script page? If i dont use an external page and put the code in the head section of the page the code does the same as using the external java script file instead.

 

If i put the java script code at the very bottom of the page it works but if i put the external java script link to the bottom of the page it doesnt work either. All senerios still make the first if statement work and thats because with the varaibles not being passed its still reading the if statement as true which is input fields are blank.

 

So if im passing variables i have to have the code at the bottom of the page so the flow of the page works with it. Which makes sense but it sucks that i cant pass variables when using a external java script file.

 

$(document).ready(function() {        

$("#evaluate").click(function(event) {          
	event.preventDefault();
	$.ajax({            

		url: "/System_Lords/ajax/include_evaluate.php",            

		type: 'POST',            

		dataType: 'json',            

		data: { 

			attacker_jaffa: $('#jaffa').val(),           
			attacker_staff_cannons: $('#staff_cannons').val(),
			attacker_bombers: $('#bombers').val(),            
			attacker_mother_ships: $('#mother_ships').val(),

			defender_jaffa: '<?php echo $defender_jaffa; ?>',            
			defender_staff_cannons: '<?php echo $defender_staff_cannons; ?>',
			defender_bombers: '<?php echo $defender_bombers; ?>',            
			defender_death_gliders: '<?php echo $defender_death_gliders; ?>',
			defender_mother_ships: '<?php echo $defender_mother_ships; ?>',            
			defender_staff_turrets: '<?php echo $defender_staff_turrets; ?>',
			defender_fortress: '<?php echo $defender_fortress; ?>',            

			attackers_weapons_tech: '<?php echo $attackers_weapons_tech; ?>',            
			defenders_weapons_tech: '<?php echo $defenders_weapons_tech; ?>',
			offense_jaffa: '<?php echo $offense_jaffa; ?>',            
			offense_staff_cannons: '<?php echo $offense_staff_cannons; ?>',
			offense_bombers: '<?php echo $offense_bombers; ?>',            
			offense_mother_ships: '<?php echo $offense_mother_ships; ?>',
			defense_jaffa: '<?php echo $defense_jaffa; ?>',            
			defense_staff_cannons: '<?php echo $defense_staff_cannons; ?>',
			defense_bombers: '<?php echo $defense_bombers; ?>',            
			defense_death_gliders: '<?php echo $defense_death_gliders; ?>',
			defense_mother_ships: '<?php echo $defense_mother_ships; ?>',            
			defense_fortress: '<?php echo $defense_fortress; ?>',
			battle_chance: '<?php echo $battle_chance; ?>'            

			},            

			success: function(response) {              

				$('#evaluate_box').html(response.evaluate); 

				}        

				});        

				});      

				});   

 

ahh I fixed the issue haha I dont know why I didnt think about this before.

 

I just had to use php to require the file. Now im passing variables to the external java script page.

 

 require("/jquery/evaluate.js"); 

 

stead of

 

<script type="text/javascript" src="/system_lords/jquery/evaluate.js"></script>

 

 

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.