Help with Jquery ajax call

Hey everyone I’m attempting to take the value inputed in one of my forms, a country name, and query my database table for rows that have the corresponding country in the country column. I would then like to take those rows, and average together the values for a certain column, std_rate, of those rows. hen take that value and dynamically update anothe field in my form called PackagingMetric_std_rate.I’ve tried several different methods, but nothing seems to work! The value of the input field I want to dynamically change does nothing.

I have tried manually querying for the value I require and it returns fine.

This is my jquery statement…



	  var input_country = $('#country').val();


	    type: 'POST',

		data: input_country,

		dataType: 'json',

		url: 'country_rate.php',


		success: function(data) {









I have the following php file ‘country_rate’ contained in the same directory…

$host = "localhost";

$user = "admin";

$password = "password"; 

$database = "datbase";

$table = "packaging_metrics"; 

$country = $_POST['input_country'];

$connection = mysql_connect($host,$user,$password);

$dbs = mysql_select_db($database,$connection);

$query = mysql_query("SELECT AVG(rate_syr_mh)FROM packaging_metrics WHERE country LIKE '" . mysql_real_escape_string($country)."'". "AND std_rate != 0 


$std_rate = mysql_result($query,0); //retrive result from query

$std_rate = round($std_rate); //round original floating point query result to nearest decimal 10th place

$std_rate = json_encode($std_rate); //encode for retrieval by AJAX

What needs to mondified for this AJAX call to work and pass/return data from fields?

Are you returning/echoing $std_rate?

In your country_rate file, echo $std_rate at the end.

Tried that. Still no luck, I don’t even get an error to show up in the input box. I have no idea if the country_rate.php file is being accessed or not.

the data param from your jquery ajax call is wrong.

you need to pass an object like:


Or a string


This will populate $_POST[‘input_country’], otherwise, how would you expect PHP knows who is your “$country = $_POST[‘input_country’];” if you don’t send it ?

You need to learn jquery before you learn doing ajax tricks.

P.S: If you are using Yii, why you use mysql_connect and all the other shit ?

you should not have a file with server logic in the same dir as a view,

you should add your logic to a controller action, and then set the url to


And you should definately not use that servercode. You set your database in your config.

and as above why do you use those mysql commands?o_O

Study this code for submitting a form with all info in it.




			  type: "POST",

			  url: "//dashboard/create",

			  data: $(this).serialize(),

			}).done(function( data ) {





//another example:


			var content = 	$("#updateportletcontent").val();

			var title = $("#updateportlettitle").val();

			var id = $("#updateportletcontent").attr('name');



			  type: "POST",

			  url: "/dashboard/updateportletcontent",

			  data: {content:content,title:title,id:id},

			}).done(function( data ) {




Thanks for the help everyone.

I must have been confused regarding how I should be tackling this. The truth is that I have been following tutorials outside of the Yii context, for raw PHP I assume, because I cannot find any Yii tutorial that exemplifies doing something very similar to this. I do not want this call to happen on an ajax submit, but on a change to a particular field in my form for creating a model.

I think I understand the Jquery portion, could anyone give me, or direct me to an example of how to write the controller method for this?

If you have no clue about PHP OOP and how all this works, DO NOT USE YII.

Learn PHP OOP concepts first time, then learn what MVC is, then when you think you know what’s all about, learn it once again, then come here and learn yii, otherwise you’ll live a nightmare, trust me.

Thanks for the advice everyone. I have been researching and trying to learn the proper method to accomplish this. I have done the following but, I still have no change on my form. Is there anyway to see if the ajax call is even being made? Could anyone correct this code to figure out why I am not getting any change on the field in my form.

In my model I have the following function

(Note I no longer need to avoid zero entities)

	public function standardRate($country)



	$country_std_rate = Yii::app()->db->createCommand()      



	   ->where(array('like', 'country', '%'.$country.'%'))   



		echo CJSON::encode($country_std_rate); 


In my controller I have added this function as well as added ‘countryRate’ to my access control for all


public function countryRate()


		$input_country = $_POST['country'];


		$model = PackagingMetric::model()->standardRate($input_country);

		echo CJSON::encode($model); 








This is my Jquery call in my form




     var country =  $('#country').val(); 



                   type: 'POST', 

                   url: 'PackagingMetric/countryRate', 

                   data: {country:country}, 


			       success: function(data) 









However, nothing on my form changes with a value, I am using Yii-Debug-toolbar and not seeing any POST calls or any sql queries upon clicking the appropriate field on my form.

Yes, you can use Firefox’s Firebug or Safari / Chrome Inspector.

You could try replacing the callbacks


in the ajax call with


, just to see if the ajax call is even being made. It works in many such cases.


Please can you check whether are you able to get alert inside live click event.

give some alert message inside. when that is not function…then there is no use of this ajax call…


chandran nepolean

Yes i’m refering to the context when a .on click event fires the ajax call.