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.
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:
data:{input_country:$("#input_country").val()}
Or a string
data:'input_country='+$("#input_country").val()
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 ?
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()
->select('AVG(rate_syr_mh)')
->from('packaging_metrics')
->where(array('like', 'country', '%'.$country.'%'))
->queryRow();
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);
Yii::app()->end();
$this->render('countryRate',array(
'model'=>$model,
));
}
This is my Jquery call in my form
$('#PackagingMetric_std_rate').live('click',function()
{
var country = $('#country').val();
$.ajax({
type: 'POST',
url: 'PackagingMetric/countryRate',
data: {country:country},
success: function(data)
{
$('#PackagingMetric_std_rate').html(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.