啊哈“追加”方法未按预期呈现表单元素

发布于 2025-01-04 09:50:51 字数 1508 浏览 6 评论 0原文

我正在尝试使用 AHAH 将一行表单元素添加到我的表单中。我的表单显示在表格中,因此我需要将表单元素添加为一行。

我对“添加更多”按钮使用以下代码:

$form['addmore'] = array(
  '#type' => 'button',
  '#value' => t('Add More'),
  '#ahah' => array(
    'path' => 'mymodule/add-more',
    'wrapper' => 'mytable-tbody', // mytable-tbody is the id of my form table tbody
    'method' => 'append',
    'effect' => 'fade',
  ),   
);

然后,我为定义的路径定义一个菜单路径,其回调函数执行以下操作:

$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;

$form['customer-6-first_name'] = array(
  '#type' => 'textfield',
  '#title' => '',
  '#size' => '15',
  '#maxlength' => 128,
);
... ... (more such form elements)

$cells = array();
$cells[] = drupal_render($form['customer-6-first_name']);
... ... (more such form renders within the cells array)

$output = '<tr>';
foreach ($cells as $cell) {
  $output .= _theme_table_cell($cell);
}
$output .= " </tr>\n";

drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
drupal_json($output);

单击“添加更多”时,我确实得到一个新行,并且表单元素也会显示,但是它们没有 id 和名称,因此没有用处。

我使用“追加”方法,因此我只需为一行编写代码。我确信我做错了什么,但在过去的 4-6 个小时里一直无法弄清楚。

I'm trying to add a row of form elements to my form using AHAH. My form is displayed in a table and therefore I need to add my form elements as a row.

I use the following code for my "Add More" button:

$form['addmore'] = array(
  '#type' => 'button',
  '#value' => t('Add More'),
  '#ahah' => array(
    'path' => 'mymodule/add-more',
    'wrapper' => 'mytable-tbody', // mytable-tbody is the id of my form table tbody
    'method' => 'append',
    'effect' => 'fade',
  ),   
);

Then I define a menu path for the defined path, the callback function of which does the following:

$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;

$form['customer-6-first_name'] = array(
  '#type' => 'textfield',
  '#title' => '',
  '#size' => '15',
  '#maxlength' => 128,
);
... ... (more such form elements)

$cells = array();
$cells[] = drupal_render($form['customer-6-first_name']);
... ... (more such form renders within the cells array)

$output = '<tr>';
foreach ($cells as $cell) {
  $output .= _theme_table_cell($cell);
}
$output .= " </tr>\n";

drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
drupal_json($output);

On clicking on "Add More", I do get a new row, and the form elements are also displayed, however they do not get an id and a name and therefore are useless.

I'm using the method "append" so that I have to write the code for just one row. I'm sure I'm doing something wrong, but have not been able to figure that out over the last 4-6 hours.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文