Drupal 6 将变量从表单传递到内容,如何?

发布于 2024-08-07 00:04:08 字数 846 浏览 4 评论 0原文

我用这个简单的形式创建了一个块(左)。 现在我想在页面(中心)上处理并显示结果 我该怎么办?

输入:

name = James
surname = Bond

我想要的输出:

<div style="color:red">Welcome, James Bond</div>

这是我编写并工作的块。

<?php

echo drupal_get_form('myForm');

function myForm($form_state) {
 $form['name'] = array(
  '#type' => 'textfield',
  '#title' => t('Name'),
  '#size' => 20,
  '#maxlength' => 10
 );
 $form['surname'] = array(
  '#type' => 'textfield',
  '#title' => t('Surname'),
  '#size' => 20,
  '#maxlength' => 10
 );
 $form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Save')
 );
return $form;
}

function myForm_submit($form,&$form_state)
{
 //??
};

现在我需要显示输出:)。

请不要建议使用 VIEWS 或任何其他插件。

我想从内到外学习Drupal。不是相反;)

I created a BLOCK (left) with this simple form.
Now I want to PROCESS and DISPLAY results on PAGE (center)
How can I do it ?

inputs:

name = James
surname = Bond

output I want :

<div style="color:red">Welcome, James Bond</div>

here is a BLOCK which i wrote and works.

<?php

echo drupal_get_form('myForm');

function myForm($form_state) {
 $form['name'] = array(
  '#type' => 'textfield',
  '#title' => t('Name'),
  '#size' => 20,
  '#maxlength' => 10
 );
 $form['surname'] = array(
  '#type' => 'textfield',
  '#title' => t('Surname'),
  '#size' => 20,
  '#maxlength' => 10
 );
 $form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Save')
 );
return $form;
}

function myForm_submit($form,&$form_state)
{
 //??
};

Now I need to display the output :).

Please don't suggest to use VIEWS or any other addon.

I want to learn Drupal from Inside out. Not the other way around ;)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁把谁当真 2024-08-14 00:04:08

嗯,这在一定程度上取决于你想如何做事。由于您正在学习如何制作 drupal 模块,因此您可能希望从

  1. 实现 hook_menu()
  2. 处理重定向的一般方法是使用 drupal_goto()。然而,在这种情况下,使用 $form_state['redirect'] 更为合适,正如 Henrik 在他的评论中所解释的那样。
  3. 对于您要重定向到的网址,您应该有一个回调函数,您可以在其中放置逻辑,设置 hook_menu 的方式和回调函数将决定如何获取可用的变量。您可能想查看 arg() 函数,它是什么一般用于从url中获取值。
  4. 通过过滤器运行用户输入,以确保他们没有发布像脚本标签等令人讨厌的东西,使用 check_plain
  5. 返回一个主题函数,或者创建你自己的函数,查看 theme()hook_theme()

有更快的方法可以做到这一点,但是这样做,您将为 drupal 可以缓存的每个搜索结果生成 url,这很好。另外,不依赖于帖子参数,人们可以为搜索结果添加书签

另一件事是,您可能希望对表单进行一些基本验证。这将是一个很好的学习实践。那看起来像这样:

/**
 * Validation handler for myForm.
 */
function myForm_validate($form, &$form_state) {
    $name = $form_state['values']['name'];
    // do some checks to $name.
    if ($error) {
        form_set_error('name', t('error message to be displayed, showing the value of the field: @name', array('@name' => $name);
    }
};

Well, it depends a bit on how you want to do things. Since you are learning how to make a drupal module, you might want to start with an implementation of hook_menu(). This hook is used to define menu items, which basically means that you can register urls with that function. Going that route you can:

  1. Implement hook_menu()
  2. A general way of handling redirects is using drupal_goto(). However, in this case it is much more fitting to use the $form_state['redirect'] as Henrik explained in his comment.
  3. For the url you are redirecting to, you should have a call back function which is where you put your logic, the way you setup the hook_menu and the callback function will determine how you get your variables available. You probably want to look into the arg() function which is what generally is used to get the values from the url.
  4. Run the user input through a filter to make sure that they haven't posted nasty stuff like script tags ect, use check_plain
  5. return a theme function, alternatively make your own, look at theme() and hook_theme()

There are quicker ways to do this, but doing it this way, you will generate urls for every search result that drupal can cache, which is nice. Also not being dependent on the post parameters people can bookmark the search results

Another thing is that you might want to put some basic validation to your form. That would be a good practice to learn. That would look something like this:

/**
 * Validation handler for myForm.
 */
function myForm_validate($form, &$form_state) {
    $name = $form_state['values']['name'];
    // do some checks to $name.
    if ($error) {
        form_set_error('name', t('error message to be displayed, showing the value of the field: @name', array('@name' => $name);
    }
};
紫竹語嫣☆ 2024-08-14 00:04:08

您可以在表单中实现 AHAH,并指定页面内容区域内的一个元素作为“包装器”(将在其中放置回调函数结果的元素)。但是,在尝试之前,您需要了解欧宝先生的极好的建议。

You could implement AHAH in your form, and specify an element inside your page's content area as the 'wrapper' (the element in which the results of the callback function will be placed). But, you would need to understand the excellent advice of Mr. Opel before you even attempt it.

梦一生花开无言 2024-08-14 00:04:08

使用 drupal_set_html_head() 将一串脚本写入页面的 head 部分怎么样?我在特定页面上执行此操作(获取用户纬度和经度并将它们传递到我的 gMap 函数中),但我对直接从 hook_form_submit() 执行相同的操作感兴趣。我已经做了一些尝试,显然我正在输出脚本字符串,但从提交调用该函数似乎不起作用。

How about using drupal_set_html_head() to write a string of script to the head section of the page? I am doing this on specific pages (getting user latitude and longitude and passing them into my gMap function), but I am interested in dong the same thing directly from hook_form_submit(). I have made a few tries at it, and I am obviously outputting the script string but calling the function from submit doesn't seem to work.

绿光 2024-08-14 00:04:08

如果提交函数创建了一个充满 html 输出的页面;将其传递给页面回调的最佳方法是什么?我怀疑作为 url 上的参数传递是否有效。

我塞进了一个会话变量,但也许有更好的方法?

if the submit function creates a page full of html output; what is the best way to pass this to a page callback? i doubt that passing as an arg on the url would work.

i stuffed into a session var but maybe a better way?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文