Shadowing Posted May 21, 2012 Share Posted May 21, 2012 (edited) 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 May 21, 2012 by Shadowing Link to comment Share on other sites More sharing options...
dsonesuk Posted May 21, 2012 Share Posted May 21, 2012 function popup(msg, address, width){var msg = "";rest of code Link to comment Share on other sites More sharing options...
Shadowing Posted May 21, 2012 Author Share Posted May 21, 2012 (edited) thanks for the reply dsonesuk made msg equal blank my end objective is adding the variable details to msg so the code below it can read it out side the ajax brackets Edited May 21, 2012 by Shadowing Link to comment Share on other sites More sharing options...
dsonesuk Posted May 21, 2012 Share Posted May 21, 2012 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 More sharing options...
Shadowing Posted May 21, 2012 Author Share Posted May 21, 2012 (edited) 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 May 21, 2012 by Shadowing Link to comment Share on other sites More sharing options...
Shadowing Posted May 21, 2012 Author Share Posted May 21, 2012 (edited) hmm got rid of var in front of details and still nothing. course i dont see why that would matter since its being added to msg Edited May 21, 2012 by Shadowing Link to comment Share on other sites More sharing options...
Shadowing Posted May 21, 2012 Author Share Posted May 21, 2012 if i add a alert before If(ready) i get details is undefined success: function(response) { details = (response.details); msg += details; } }); alert(details); if (ready) { Link to comment Share on other sites More sharing options...
dsonesuk Posted May 21, 2012 Share Posted May 21, 2012 try withdetails = (response); Link to comment Share on other sites More sharing options...
Shadowing Posted May 21, 2012 Author Share Posted May 21, 2012 (edited) 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 May 21, 2012 by Shadowing Link to comment Share on other sites More sharing options...
dsonesuk Posted May 21, 2012 Share Posted May 21, 2012 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 More sharing options...
Shadowing Posted May 22, 2012 Author Share Posted May 22, 2012 (edited) 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 May 22, 2012 by Shadowing Link to comment Share on other sites More sharing options...
dsonesuk Posted May 22, 2012 Share Posted May 22, 2012 alert(deatils) no! alert(details) yes! What where is (ready) supposed to refer to? Link to comment Share on other sites More sharing options...
Shadowing Posted May 22, 2012 Author Share Posted May 22, 2012 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 More sharing options...
dsonesuk Posted May 22, 2012 Share Posted May 22, 2012 add async: false, and compare result $.ajax({ url: "/ajax/map.php?functions=popup", type: 'POST', dataType: 'json', async: false, success: function(response) { details = "$test"; }}); alert(details); }); Link to comment Share on other sites More sharing options...
Shadowing Posted May 22, 2012 Author Share Posted May 22, 2012 (edited) 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 May 22, 2012 by Shadowing Link to comment Share on other sites More sharing options...
dsonesuk Posted May 22, 2012 Share Posted May 22, 2012 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 More sharing options...
Shadowing Posted May 22, 2012 Author Share Posted May 22, 2012 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 More sharing options...
dsonesuk Posted May 22, 2012 Share Posted May 22, 2012 error: function(){ alert("Oops... seems to be problem retrieving data, anything in success function will never run now! sorry") }, Link to comment Share on other sites More sharing options...
Shadowing Posted May 22, 2012 Author Share Posted May 22, 2012 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 More sharing options...
Shadowing Posted May 22, 2012 Author Share Posted May 22, 2012 (edited) 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 May 22, 2012 by Shadowing Link to comment Share on other sites More sharing options...
Shadowing Posted May 22, 2012 Author Share Posted May 22, 2012 that error control works, nice! just tested it. also i should mention the error doesnt go off in the above example Link to comment Share on other sites More sharing options...
dsonesuk Posted May 22, 2012 Share Posted May 22, 2012 Went through all this to get it to partially work withasync: false,and you leave it out! try it with this. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now