jimfog Posted May 30, 2013 Share Posted May 30, 2013 (edited) I have created a variable inside a function where it takes the value of the radio button in a form-chosen by the user...nothing strange till now. $(function john(){ $('.formBuzType').change(function() { var btype = $(this).val(); return btype; } ); }); I am talking about btype.The problem appears when I want to use the variable in ajax and send its value to a php script-from where it is inserted to a table...the variable seems to be empty, nothing is inserted to the db...here is the jquery ajax function: $(function() { $('.error').hide(); $("#savepersonal").click(function() { event.preventDefault(); $('.error').hide(); $.ajax({ type: "POST", url: "testajax.php", data: {"name":name,"lastname":lastname,"btype":btype} }); return false; }); }); If a put "btype":2...everything works ok 2 and gets passed to the db table.Name and lastname gets passed without problem to the db table. regarding btype, there must be a problem with the scope...but what. And another thing...is there in js an analog of var_dump found in php? Edited May 30, 2013 by jimfog Link to comment Share on other sites More sharing options...
thescientist Posted May 30, 2013 Share Posted May 30, 2013 you're not calling the function per se, rather the function is being executed by an event handler, not you calling the function from somwhere. the easiest option is to make btype a var outside the event handler (global), and assign it the value within the event handler. Your ajax function should then have access to it. use the console to debug, i.e. console.log('some string, var, object, array, etc'); a nice browser like chrome will provide a very nice error console for you to inspect things with. Link to comment Share on other sites More sharing options...
jimfog Posted June 2, 2013 Author Share Posted June 2, 2013 I tried the code below...according to the logic you described but with no results: $(function (){ $('.formBuzType').change(function() { var btype = $(this).val(); return btype; } ); }); var btype; And regarding the error console...I tried console.log('btype'); and just got undefined. The ajax function is left unchanged. Link to comment Share on other sites More sharing options...
dsonesuk Posted June 2, 2013 Share Posted June 2, 2013 var btype; // global $(function (){ $('.formBuzType').change(function() { //var btype = $(this).val(); using var restricts the scope of this variable to this function btype = $(this).val(); //global btype equals value //return btype; not required } ); }); so now the ajax function can read the new global value when run. $(function() { $('.error').hide(); $("#savepersonal").click(function(e) { //corrected missing event variable e.preventDefault(); $('.error').hide(); $.ajax({ type: "POST", url: "testajax.php", data: {"name":name,"lastname":lastname,"btype":btype} }); //return false; ?? don't see why this is required }); }); Link to comment Share on other sites More sharing options...
thescientist Posted June 3, 2013 Share Posted June 3, 2013 (edited) I tried the code below...according to the logic you described but with no results: $(function (){ $('.formBuzType').change(function() { var btype = $(this).val(); return btype; } ); }); var btype; And regarding the error console...I tried console.log('btype'); and just got undefined. The ajax function is left unchanged. remove the var from within the change event. casting it as var again will just reassign to that scope, which wouldn't be any different than how you had it. edit: and yes, the return statement isn't needed anymore. Edited June 3, 2013 by thescientist Link to comment Share on other sites More sharing options...
jimfog Posted June 3, 2013 Author Share Posted June 3, 2013 yes...the code works fine.I have completely forgotten that using the var keyword inside the function turns the variable into a local one. 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