如何在 Drupal 中创建表单自引用?或者有其他选择吗?

2021-12-30 00:00:00 forms php drupal

我想在我的网站上制作一个自我引用的表格.或者,如果这不是一个选项,例如,我将如何显示我在我的网站中进行的搜索结果?

I would like to make a form I have in my website self referencing. Or if that's not an option, how would I go for, for example, showing the results of a search I make in my site?

我有一个网站,您可以在其中搜索地点,它会根据您的喜好返回地点列表.目前,每次用户搜索时,我的脚本都会创建一个新节点,但这不再方便.如何更改它以便更改页面内容并看到结果而不是搜索表单?

I have a site in which you search for places and it returns a list of places for your preferences. At the moment my script creates a new node every time a user searches but this isn't convenient anymore. How do I change it so that the page content is changed and I see the results instead of the search form?

谢谢,

推荐答案

您应该将您的表单重定向到一个页面,该页面传递一个带有用户搜索内容的字符串的查询字符串,然后在您的搜索中使用 $_GET['search_param']/restuls 页面来处理将向用户显示的内容.

You should redirect your form to a page passing a query string with the string of what the user searched and then use $_GET['search_param'] in your search/restuls page to handle what will be displayed to the user.

function yourform_form($form_state) {
    $form = array();
    //$form['your_search_field']
    $form['#submit'][] = 'yourform_form_submit';
    return $form;
}

function yourform_form_submit(&$form, $form_state) {
    $query = 'search_param='. $form_state['values']['your_search_field'];
    drupal_goto('search/results', query);
}

如果您使用的是 Drupal 7,您的提交功能应该如下所示:

If you're using Drupal 7 your submit function should look like:

 function yourform_form_submit(&$form, $form_state) {
        $options['query']['search_param'] = $form_state['values']['your_search_field'];
        drupal_goto('search/results', $options);
    }

提交后,您应该被重定向到 http://yoursite.com/search/results?search_param=my_search_value

After you submit you should be redirected to http://yoursite.com/search/results?search_param=my_search_value

请注意,流行的搜索引擎使用此技术:

Note that this technique is used by popular search engines:

https://www.google.com/search?q=my_search_value

相关文章