2011-09-12 10 views
0

私は、コンボボックス間に依存関係があるモジュールを作成しています。コンボボックスは、国、州、および地区の3つあります。国コンボボックスでインドを選択すると、州コンボボックスにはインドのすべての州が含まれ、地区の場合も同じコンボボックスが表示されます。AJAXエラーDrupal-7

私はこれをAJAXを使用して行っています。これは、追加の形で適切に動作しますが、私はいずれかを更新しようと思ってたときに、次のエラーが発生します。

An AJAX HTTP error occurred. 
HTTP Result Code: 500 
Debugging information follows. 
Path: /drupal/?q=system/ajax 
StatusText: Service unavailable (with message) 
ResponseText: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ajax' in 'where clause': SELECT district_name, country_id,state_id FROM {ajax_district} where id = ajax; Array 
(
) 
in edit_district_form() (line 291 of /home/mansmu/www/drupal/sites/default/modules/ajaxtest/ajaxtest.district.inc). 

私のコードは次のとおりです。ここでは、コードの

//////////////////////////////////////////////////////// 
///////// FUNCTION FOR EDIT 
//////////////////////////////////////////////////////// 
function edit_district_form($form, &$form_state) { 
$request_url = request_uri(); 
// $request_id to get id of edit district. 
$request_id = drupal_substr(strrchr($request_url, '/'), 1); 

//// FOR country 
$rows = array(); 
$result = db_query("SELECT id,country_name from {ajax_country} order by country_name"); 
while ($data = $result->fetchObject()) { 
$id = $data->id; 
$rows[$id] = $data->country_name; 
} 

//// FOR state 
$state = array(); 
$result = db_query("SELECT id,state_name from {ajax_state} order by state_name"); 
while ($data = $result->fetchObject()) { 
$id = $data->id; 
$state[$id] = $data->state_name; 
} 

// $district_name varible to get name from database for a requested id. 
$district_name = db_query("SELECT district_name, country_id,state_id FROM {ajax_district} where id = ".$request_id); 
// Loop For show vehicle district name in text field to be update. 
foreach ($district_name as $district) {*/ 
$form['values']['edit_country_name'] = array(
'#type' => 'select', 
// '#default_value' => "{$district->country_id}", 
'#required' => TRUE, 
'#options' => $rows, 
'#ajax' => array(
'effect' => 'fade', 
'progress' => array('type' => 'none'), 
'callback' => 'state_callback_edit', 
'wrapper' => 'replace_edit_state', 
), 
); 

$form['values']['edit_state_name'] = array(
'#type' => 'select', 
// '#default_value' => "{$district->state_id}", 
'#required' => TRUE, 
'#options' => array(), 
// The prefix/suffix provide the div that we're replacing, named by #ajax['wrapper'] above. 
'#prefix' => '', 
'#suffix' => '', 
); 

$form['values']['edit_district_name'] = array(
'#type' => 'textfield', 
'#size' => 30, 
//'#default_value' => "{$district->district_name}", 
'#maxlength' => 80, 
'#required' => TRUE, 
); 
} 

$form['edit_district_submit'] = array(
'#type' => 'submit', 
'#value' => t('Update'), 
); 
$form['actions']['cancel'] = array(
'#markup' => l(t('Cancel'), 'district'), 
); 

return $form; 
} 
//////////////////////////////////////////////////////// 
///////// FUNCTION FOR AJAX CALL BACK 
//////////////////////////////////////////////////////// 
function state_callback_edit($form, &$form_state) { 
// An AJAX request calls the form builder function for every change. 
// We can change how we build the form based on $form_state. 
if (!empty($form_state['values']['edit_country_name'])) { 
$country_id = $form_state['values']['edit_country_name']; 
$rows1 = array(); 
$result = db_query("SELECT id, state_name from {ajax_state} where country_id = $country_id order by state_name"); 
while ($data = $result->fetchObject()) { 
$id = $data->id; 
$rows1[$id] = $data->state_name; 
} 
$form['edit_state_name']['#options'] = $rows1; 
} 
return $form['values']['edit_state_name']; 
} 

答えて

1

あなたのライン:

$request_id = drupal_substr(strrchr($request_url, '/'), 1);

はreturniです文字列「AJAX」をngの、あなたの第三SQL文は次のようになり作る:

SELECT district_name, country_id,state_id FROM {ajax_district} where id = ajax; 

は明らかに「アヤックスは」は間違っている、私はあなたがURLから値をしたいと思いますか?あなたはあなたがあなたのパスがajax/12が、その後arg(0)は、「AJAX」を返しますarg(1)がそうで12とを返します場合にはdrupal_substrなど

を実行する必要はありません、これを抽出するためにarg() functionを使用することができます。

フォームで要求IDを保存する必要がある場合は

UPDATE

を助けホープフォーム機能でこのような何かを:

$form['request_id'] = array('#type' => 'value', '#value' => $request_id); 

次にあなたが入るときあなたのAJAXコールバックは$form_state['values']['request_id']または$form['request_id']['#value']

+0

からお返事ありがとうございます。はい、私はURLからの値が欲しいですが、私はajaxを呼び出すとき、URLは何かシステム/ ajaxを編集する/ 12ここで、12は更新される私の地区のIDです。私はこのタイプの問題を私に伝えています。 –

+0

Gotcha、コードを上記に更新しました – Clive

+0

Dear Cliveあなたの更新コードはうまくいかなかった。 –