Jump to content

problem with the variable


jimfog
 Share

Recommended Posts

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 by jimfog
Link to comment
Share on other sites

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

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

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

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 by thescientist
Link to comment
Share on other sites

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

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...