2011-09-12 125 views
0

我正在創建一個組合框之間存在依賴關係的模塊。有三個組合框:國家,州和地區。如果我在國家組合框中選擇印度,那麼狀態組合框將包含印度的所有國家,並且在區域情況下也是如此。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」,使你的第三個SQL語句是這樣的:

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

顯然「AJAX」是錯在那裏,我想你想從URL中的價值?您可以使用arg() function提取該這樣你就不會需要運行drupal_substr

如果你的路徑是ajax/12然後arg(0)將返回「AJAX」,arg(1)將返回12等。

希望幫助

UPDATE

如果您需要保存請求ID的形式做這樣的事情在你的表單功能:

$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。 M無法在ajax調用中發送此ID,多數民衆贊成在這種類型的問題即將到來, –

+0

難題,代碼更新以上 – Clive

+0

親愛的克萊夫你的更新代碼沒有工作。 –