をフォーム変数を投稿してjQueryのAJAXを通じて取得する私は、変数を投稿する必要がありますとAjaxにjQueryを使用してCodeIgniterのになります。CodeIgniterの -
私のデータベースには内容がありますが、コードは空の配列を返しています。 - array(0){}。 問題は、変数がフォームから転記されていないことです。ここに私のコードは次のとおりです。
フォームコード - ビュー:
<form id = "myform1" onsubmit="return false">
<select name="field_country">
<option value="0">Argentina</option>
<option value="1">Chile</option>
</select>
</form>
Javascriptを - ビューのインライン:
jQuery(document).ready(function($){
$("select[name='field_country']").change(function(){
var country_name= $("select[name='field_country'] option:selected").text();
$.post("<?= base_url(); ?>index.php/catalog/getCities/", {country:country_name}, function(data){
alert(data);
})
})
})
catalog.phpコントローラ - 機能:
function getCities(){
$country_name = $this->input->post('country', 'Argentina');
$result = $this->catalog_model->getCitiesNames($country_name);
var_dump($result);
}
catalog_modelモデル関数は:
function getCitiesNames($country_name) {
$cities=array();
$result = $this->db->query("SELECT title FROM cities WHERE country = '".$country_name."'");
foreach ($result->result() as $row) {
array_push($cities, $row->title);
}
return $cities;
}
どうもありがとうございました。私は問題を解決しましたが、問題を返す変数はありません。問題は、投稿されている間に変数が破壊されることでした。私は以前のように絶対パスの代わりにポストuriをちょうど "getCities"に変更して解決策を見つけました。私は、アプリが自動的に2つのデータ要求(1つのPOST、および1 GET)を生成するように見えたとして、しかし、今のおかげで解決し.Its)干渉している可能性があり疑うので、私は、多言語のURIの識別子を使用しています。 – Cruising2hell