Jump to content

scopes


Shadowing
 Share

Recommended Posts

Hey guys im having some issues with reading a variable How do I get the variable "msg = msg+details;"so that the code at the bottom of the script can use the variable "the code that starts with"if(ready)"

function popup(msg, address, width){    			   $.ajax({		  						  url: "/ajax/map.php?functions=popup",		  						  type: 'POST',		  						  dataType: 'json',		  						  data: {  								address: address									 },		  						  success: function(response) {								   var details = (response.details);					  msg = msg+details;  	}	}); 	if (ready) {		if (typeof width === "undefined"){			width = default_width;		}		$('#pup').html(msg).width(width).show();		var t = getTarget(arguments.callee.caller.arguments[0]);		$(t).unbind('mouseout').bind('mouseout',			function(e){				$('#pup').hide().width(default_width);			}		);	}}}

Edit: I hate how my brackets all get out of place and formating goes crazy when posting

Edited by Shadowing
Link to comment
Share on other sites

Before anything changes it set it outside usingvar msg; rest of code... this will make it global, do not use var against it anywhere else as in a function function popup(msg, address, width){var msg;} as will cause this msg variable scope to be from within this function only, and not that of the global variable outside the function.

Link to comment
Share on other sites

yah var is only in front of it once oh is it because i have var in front of details?

function popup(msg, address, width){var msg;   $.ajax({		     url: "/ajax/map.php?functions=popup",		     type: 'POST',		     dataType: 'json',		     data: {  	address: address   },		  	success: function(response) {				details = (response.details);  	 msg += details;	 	}	}); 	if (ready) {		// use default width if not customized here		if (typeof width === "undefined"){			width = default_width;		}		// write content and display		$('#pup').html(msg).width(width).show();		// make sure popup goes away on mouse out		// the event obj needs to be gotten from the virtual		//   caller, since we use onmouseover='popup(msg)'		var t = getTarget(arguments.callee.caller.arguments[0]);		$(t).unbind('mouseout').bind('mouseout',			function(e){				$('#pup').hide().width(default_width);			}		);	}} 

Edited by Shadowing
Link to comment
Share on other sites

i did alert inside the success bracket for details using details = (response); and it gave me object object, so i guess it needs the key. So i tried thisdetails = "testing"; and put alert again in the same spot above if(ready) and got undefine

Edited by Shadowing
Link to comment
Share on other sites

Test alert directly after success function

success: function(response) {                             alert(response.details);        //details = (response.details);          // msg += details;

Check that you a definitely getting a correct response before doing anything else with it.What version of jquery are you using? older versions have a problem unless you use beforeSend: function(x) { if(x && x.overrideMimeType) { x.overrideMimeType("application/j-son;charset=UTF-8"); } },

$.ajax({				     url: "/ajax/map.php?functions=popup",				     type: 'POST',           async: false, //Adjust async to true if you want this to AJAX call to run at the same time as other things     beforeSend: function(x) {      if(x && x.overrideMimeType) {       x.overrideMimeType("application/j-son;charset=UTF-8");      } },   				       dataType: 'json',			     data: { 	    address: address   },		     	    success: function(response) {				   	    details = (response.details); 		 msg += details;	     	    }	    });

Link to comment
Share on other sites

using jquery version 1.7.2 Yah alert works with details inside the success bracket.what happends if you run this code dsonsuk. do you get details undefined?

$(function(){ $.ajax({			   url: "/ajax/map.php?functions=popup",			  type: 'POST',		      dataType: 'json',		      data: {  	   },				success: function(response) {			 		 details = "$test";	}}); alert(deatils); }

Edited by Shadowing
Link to comment
Share on other sites

I ran this exact code copy and pasted it. and firebut says details is undefinedif you can read this and i cant what the heck lol its a onmouseover pop up box function I got from someone. Its the only code in my entire site I didnt write my self lolmy javascript is so bad

$(function(){ $.ajax({					      url: "/ajax/map.php?functions=popup",						  type: 'POST',			   dataType: 'json',			      data: {  		   },				   	    success: function(response) {									 details = "$test";	    }}); alert(details); });

Link to comment
Share on other sites

details is not defineddid a copy and paste of the code i used. this way you know im using exactly what i tried.

$(function(){$.ajax({					  url: "/ajax/map.php?functions=popup",						type: 'POST',		   dataType: 'json',			  async: false,									success: function(response) {				   				 details = "$test";	   }}); alert(details); });

if I do this i get no undefine errors but the alert box does not pop up.

$(function(){$.ajax({					      url: "/ajax/map.php?functions=popup",						  type: 'POST',			   dataType: 'json',			     async: false,  							 	    success: function(response) {									 details = "$test";				   alert(details);			  	    }});  });

Edited by Shadowing
Link to comment
Share on other sites

Something in the php response is failing, and causing the success function not to be triggered, details is not being assigned the value of '$test' and the alert is not being triggered. if i use

$(function(){ $.ajax({										     url: "json_prob02Response.php",											    type: 'POST',			        dataType: 'json',					         async: false,     //beforeSend: function(x) {     // if(x && x.overrideMimeType) {      // x.overrideMimeType("application/j-son;charset=UTF-8");      //} //},						   		    success: function(response) {									   								 details = "$test";                                                                 alert("No1 :"+details);		    }}); alert("No2 :"+details); });

I get both alerts because the response from php file ran successfully, But! if i rename file to non existent file name 'json_prob02Responsex.php, I get the same problem you are getting now 'details not defined' and no alerts whatsoever.

Link to comment
Share on other sites

oh you are right the php page was failing. I forgot it needed the address variable :)alert is working now out side the success bracket.so some how it must of been failing earlier to then.i was changing so much stuff hard to tell. I think im going to go ahead and try to duplicate by going back add this all to that orginal function in my orginal post and see if i can add details to msg now.

$(function(){$.ajax({					      url: "/ajax/map.php?functions=popup",						  type: 'POST',			   dataType: 'json',			     async: false,	  data: {  	    address: '210235'   },							 	    success: function(response) {									 details = "test";									    }}); alert(details); });

Link to comment
Share on other sites

that was a interesting theory :) went back and replicated the first exampleand the php script wasnt failing at that time. I even went a step further and checked the variable msg and address after success bracket both values existedalso i went ahead on the php file and made it where it didnt need the address variable passing onto it.

function popup(msg, address, width){var msg;  $.ajax({		      url: "/ajax/map.php?functions=popup",		      type: 'POST',		      dataType: 'json',		      data: {   	address: address   },		   	success: function(response) {			 	 var details = (response.details); 		 								  msg = msg+details;	 	alert(details); // this works  	} }); alert(details); // this doesnt work      if (ready) {	    if (typeof width === "undefined"){		    width = default_width;	    }	    $('#pup').html(msg).width(width).show();	    var t = getTarget(arguments.callee.caller.arguments[0]);	    $(t).unbind('mouseout').bind('mouseout',		    function(e){			    $('#pup').hide().width(default_width);		    }	    );    } }

Link to comment
Share on other sites

thats wierd i changedvar details = (response.details); to details = (response.details); and now now it says undefine error but then when i activate it the 2nd time the alert workswith putting alert only out side the success bracket. oh thats awesome error control im going to use that. maybe not that long of a error thoughso like do i just put that error control anywhere does it not matter in what order?i'll put the name of the function in the error message

Edited by Shadowing
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...