search block form opens search page (search/node) without results

decibel.places - April 30, 2008 - 15:25

I have a search block form on my front page (actually, on all pages except /search* )

When you enter a term and search, it opens the search page (search/node) in the content, but without search results. If you search again from the search page, you get the results.

I tried playing around with the search.module, trying to get the keys in the original search url (search/node/"keys"), but can't find the place in the module code.

site url http://4m.netsperience.org/

try searching for "lorem" or "ipsum" not much content yet

That doesn't look like the

WorldFallz - April 30, 2008 - 17:38

That doesn't look like the stock search block-- how did you create it? Can you post the code?

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

search block code

decibel.places - May 1, 2008 - 14:23

It is a standard search block positioned in the content area.

I use js to remove the text value from the submit button, and css to replace it with a custom background image. On some pages, the text value persists.

This is code from the page source:

<script type="text/javascript">
  <!--
document.getElementById("edit-submit").value = "";
  //-->
</script>

<div id="block-search-0" class="block block-search">
<div class="content"><form action="/search/node"  accept-charset="UTF-8" method="post" id="search-block-form">
<div><div class="container-inline"><div class="form-item">
<input type="text" maxlength="128" name="search_block_form_keys" id="edit-search-block-form-keys"  size="15" value="" title="Enter the terms you wish to search for." class="form-text form-autocomplete" />
</div>
<input class="autocomplete" type="hidden" id="edit-search-block-form-keys-autocomplete" value="http://4m.netsperience.org/search_autocomplete/autocomplete" disabled="disabled" /><input type="submit" name="op" id="edit-submit" value=""  class="form-submit" />

<input type="hidden" name="form_id" id="edit-search-block-form" value="search_block_form"  />
</div>
</div></form>
</div>
</div>

I tried changing the form action in the module to add the search keys like the page does, but nothing was added to the url.

Here's the css:

#block-search-0 {
position: absolute;
top: 0px;
margin-left: 275px;
}

#edit-submit {
width: 43px;
height: 28px;
border: none;
color: none;
background: url(http://4m.netsperience.org/sites/all/files/go.gif);
margin-top: 20px;
vertical-align: -13px;
*vertical-align: -3px;
}

#edit-keys {
vertical-align: 0px;
}

And I use some js to detect Safari and reposition stuff

    // sniff Safari

    isSafari3 = false;
    if(devicePixelRatio) isSafari3 = true;
    if (isSafari3){
document.getElementById("edit-submit").style.verticalAlign = "10%";
document.getElementById("menu-84").style.backgroundPosition = "0px .65em";
document.getElementById("menu-85").style.backgroundPosition = "0px .65em";
document.getElementById("menu-86").style.backgroundPosition = "0px .65em";
document.getElementById("menu-87").style.backgroundPosition = "0px .65em";
document.getElementById("menu-88").style.backgroundPosition = "0px .65em";
document.getElementById("menu-89").style.backgroundPosition = "0px .65em";
document.getElementById("menu-90").style.backgroundPosition = "0px .65em";
document.getElementById("menu-91").style.backgroundPosition = "0px .65em";
document.getElementById("menu-92").style.backgroundPosition = "0px .65em";
document.getElementById("menu-93").style.backgroundPosition = "0px .65em";
}

~are you netsperienced? http://netsperience.org

Try adding: <input

WorldFallz - May 1, 2008 - 14:33

Try adding:

<input type="hidden" name="form_token" id="a-unique-id" value="<?php print drupal_get_token('search_block_form'); ?>"

after the form_id field. That fixed this problem for me.
===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

what file?

decibel.places - May 1, 2008 - 19:54

Where does the code go?

I looked in theme templates, search module, block module. I can't find the file that assembles the html for the search block.

~are you netsperienced? http://netsperience.org

Sorry-- i didn't grasp that

WorldFallz - May 1, 2008 - 20:40

Sorry-- i didn't grasp that was a "view source" dump.

After a closer look at the code, it actually looks like you are loading a custom search module called "search_autocomplete"-- which is a contrib. So you should really check with the issues queue for that module.

Sorry, i hadn't noticed that earlier, I just thought it was a custom block or search-block-form.tpl.php file.

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

not search_autocomplete

decibel.places - May 2, 2008 - 01:04

Good idea

I looked over the search_autocomplete module, then disabled it.

The block search form still opens a search page without the results. When you search on THAT search form, you get results.

Even with search_autocomplete, it worked (sort-of) that way - the inner search form produced results, and was autocomplete enabled.

I still think somehow the keys are not getting passed to the url.

Where is "search-block-form.tpl.php" ? I looked all over - I'm using a modified Framework theme and Drupal 5.7 (for ecommerce support).

And where did you put that

<input type="hidden" name="form_token" id="a-unique-id" value="<?php print drupal_get_token('search_block_form');>

-- I'd like to try it.

~are you netsperienced? http://netsperience.org

Where is

WorldFallz - May 2, 2008 - 02:52

Where is "search-block-form.tpl.php" ? I looked all over - I'm using a modified Framework theme and Drupal 5.7 (for ecommerce support).

It would be in your theme directory if you had one-- most themes don't tho.

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

no search-block-form.tpl.php in Framework Theme

decibel.places - May 2, 2008 - 04:36

no search-block-form.tpl.php in the Framework Theme

No header, no footer, primary links at top, secondary links in left sidebar

I placed the search block in the CONTENT so I need to find the engine that fills that container in the page.tpl.php

Can I modify it without seriously forking? I forked a site a couple of years ago and regretted it, client couldn't find any one else to work on it when I was indisposed.

Anyway, it's a straightforward search form, only modified the submit button appearance with css. Why wouldn't it generate search results? the Url it generates is simply "search/node" without keys, the search page generates the Url "search/node/term1+term2 etc." with search term keys.

I just reuploaded the v 5.7 search module directory in case I made any errant edits in it, but with the fresh files it is behaving the same.

~are you netsperienced? http://netsperience.org

I'm honestly not sure why,

WorldFallz - May 2, 2008 - 13:12

I'm honestly not sure why, if you're back to default search, you're still having the problem-- unless search_autocomplete did something somewhere it didn't undo.

In any case, you don't have to fork drupal to have a custom search. You can just override the default search block with your own, that's what I did. Here's what works for me:

In template.php for your theme add:

<?php
function phptemplate_search_block_form($form) {
 
/**
   * This snippet catches the default searchbox and looks for
   * search-block-form.tpl.php file in the same folder
   * which has the new layout.
   */
 
return _phptemplate_callback('search-block-form', array('form' => $form));
}
?>

Then create a search-block-form.tpl.php file (in the theme directory) and add this:

<?php
<input type="text" maxlength="128" name="search_block_form_keys" id="edit-search_block_form_keys" onfocus="if(this.value=='search...') this.value='';" onblur="if(this.value=='') this.value='search...';" value="search..."  size="25"  class="form-text" />
<
input type="hidden" name="form_id" id="edit-search-block-form" value="search_block_form" />
<
input type="hidden" name="form_token" id="a-unique-id" value="<?php print drupal_get_token('search_block_form'); ?>
" />
?>

When I had the same problem as you (empty search results page) it was because I was missing the drupal_get_token hidden form field. Once I added it, it worked great.

Maybe you can try this-- make sure it works. I have this running right now on a fresh D5.7, so if it doesn't work for you then there has to be some cruft somewhere. If it does work, then just adapt the search-block-form.tpl.php for your own needs.

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

no dice

decibel.places - May 2, 2008 - 17:15

I made the changes, your search form has the same behavior, opening "search/node" without the keys.

I reverted to the default search form.

I tried dropping the search tables in the db, and ran update.php, but didn't create new search tables, so I restored the backups.

I still think the problem is the block search form is not adding the search terms/keys to the url.

Is there a way to insert the "drupal_get_token hidden form field" into the default form with PHP? (I'm not that advanced)

~are you netsperienced? http://netsperience.org

Something is really screwy

WorldFallz - May 2, 2008 - 17:47

Something is really screwy somewhere--

Is there a way to insert the "drupal_get_token hidden form field" into the default form with PHP? (I'm not that advanced)

The default search form does that that already-- it uses the the form api. I do it manually because I'm not using the form api.

The next thing I would do is disable all non-core modules and try search again. If it works, then enable them one at a time until you find the offender.

Also, you don't want to drop the tables, just empty them I think. But if you click the "reindex" button at admin/settings/search it will do that.

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

just the core modules, still no fix

decibel.places - May 2, 2008 - 19:09

I disabled all non-core modules and still the same problem.

I re-enabled nice_menus because it was throwing off the page structure without the left sidebar menu.

Where is the form api code? Maybe I made an edit to it when working on customizing the search form with the image submit button...

I reuploaded a fresh copy of the search module code, the entire directory (a couple of steps back).

I remember looking around and setting "value" to "" to remove the text from the submit button (doesn't work everywhere yet) and I see that the js I included for that comes b4 the search form, so probably doesn't work to reset the button value. Maybe I messed with a keys value and made it null - that would explain why "search/node" doesn't have keys attached.

But I thought reuploading the fresh search module code would take care of that.

Or maybe auto complete changed something, relevant to the keys.

So the form api uses the "drupal_get_token hidden form field"?

I went to admin/settings/search and looked over stuff, I didn't change the defaults anyway, and reindexed the site (all 3 nodes).

~are you netsperienced? http://netsperience.org

Where is the form api code?

WorldFallz - May 2, 2008 - 19:44

Where is the form api code? Maybe I made an edit to it when working on customizing the search form with the image submit button...

But I thought reuploading the fresh search module code would take care of that.

It's in search.module-- starts around line 120 IIRC. Search for "search_block". Form API forms all have a token built-in as a security measure (but you wont see it in the code, just by viewing the source). See http://drupal.org/node/178896 for more info.

And I would think that getting a fresh search.module should have fixed anything. Did you look over template.php? that's another place stuff can be overriden and muck things up.

Other than that, I think I'm out of ideas. 8-(.

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

$form['#action'] = url('search/node'); >> THEME SEARCH WORKS !!!

decibel.places - May 2, 2008 - 23:35

On line 1048 of search.module there is this code:

$form['#action'] = url('search/node');

Which is the action of the block search form.

I added

$form['#action'] = url('search/node'. '/lorem');

and the search produced the url "search/node/lorem" but still displayed the search page without results. Then when I go to that url (by hitting enter in the address bar of the browser without changing the URL) I get the search results with the same URL search/node/lorem.

It doesn't matter what search term(s) are entered in the block search form (or not terms), they are not added to the URL - just the fudge I added in the module code.

I tried

$form['#action'] = url('search/node/'. $keys);

and

$form['#action'] = url('search/node/'. trim($form_values[$form_id .'_keys']));

but both of these snippets just generated the "search/node/" url with no keys

On line 1020 of search.module

/**
* Process a search form submission.
*/
function search_form_submit($form_id, $form_values) {
  $keys = $form_values['processed_keys'];
  if ($keys == '') {
    form_set_error('keys', t('Please enter some keywords.'));
    // Fall through to the drupal_goto() call.
  }

  $type = $form_values['module'] ? $form_values['module'] : 'node';
  return 'search/'. $type .'/'. $keys;
}

I don't think this code is active, because a search without any terms entered does not trigger the error, just loads the search/node page like any other search.

The problem is the $keys are not getting passed to the URL (however, when I manually added the key "/lorem" it didn't activate the search function until after the search page loaded.)

I tried again deleting the search module and replacing it with fresh code from the default Drupal 5.7 package.

I checked the template.php of the Framework Theme, no unusual references to search or form (actually just the commented-out code I added to load the custom search form template you suggested).

I am thinking I might try using the theme search form. I had trouble making it appear in the location on the page where I want it, which is why I used the search block, but that might "solve" the problem - or at least sweep it under the carpet!

THEME SEARCH WORKS FINE!!!!

now just the positioning....

wonder why the block search didn't work...

~are you netsperienced? http://netsperience.org

Search block - placed on one page only causes same result...

sfinerty - May 6, 2008 - 21:02

I am using the standard Search block provided in Drupal (5.7) core and have found that if I want it to only appear on the front page of the site (or any single page for that matter), it will not display results but rather takes you to the search/node page which is blank. If I re-enter the search request, the results are shown.

Any ideas? I'm not doing anything else to the code and I have tried it on a few different single pages to rule out Panels (which I am using on the front page). As long as the block is configured to 'Appear on all pages' it works fine...

Any help would be appreciated - is this a bug?

Shelley ;)

Search block does not work, but the search page works

maxc - May 7, 2008 - 01:26

If I request a search from search block, it returns empty results. If I fill in the keyword and request from the /search/node page, it returns results.

After checking the search.module, I found why:

function search_view() {
...
  if (!isset($_POST['form_id'])) {
...
    $results = search_data($keys, $type);
...
}

So I overwrite the block form in template.php(you can create search-block-form.tpl.php though) by removing the hidden field named "form_id", it works.
function mytheme_search_block_form($form) {
  return '<form action="/search/node"  accept-charset="UTF-8" method="post" id="search-form" class="search-form">
          <div><div class="container-inline"><div class="form-item">
           <input type="text" maxlength="255" name="keys" id="edit-keys"  size="20" value="" class="form-text" />
          </div>
          <input type="submit" name="op" id="edit-submit" value="Search"  class="form-submit" />
          <input type="hidden" name="form_token" id="edit-form_token" value="' . drupal_get_token('mytheme_search_block_form') . '" />
          </div>
          </div></form>';
}

Made it work....

tilo - May 22, 2008 - 15:21

Hi
I had the same problem and it was because of a very simple reason:
When you limit the 'search block' to specific nodes, search seems not be able to display the results on a new search page.

This is how it works:

  1. Open search block configuration
  2. In section 'Show block on specific pages:' set radio button to 'Show on only the listed pages.'
  3. In the page list, add search/* in a new line.

That's all...

Thank you so much tilo

tikitikita - May 23, 2008 - 08:38

I was going crazy with this!
Your solution worked like a charm.

THANK YOU THANK YOU

edward.peters@d... - June 12, 2008 - 13:09

Thank you tilo!!! I too have been struggling with this, and you solved it! Thanks for sharing your solution. What a great community Drupal is!

Note for those who use multiple themes on one site...

j.a.w. - November 4, 2008 - 00:16

My thanks also to tilo. This fix worked for me as well ... but with a twist ...

I use multiple themes on one site and the page I was attempting to add the Search block to was not the default theme so the block did not appear on the search page (which used the default theme) therefore I wasn't getting the results. Setting the the search page to use the same theme as the one with the Search block worked as did adding the Search block to all the themes. This might be obvious to some but it took a bit for me to get so I figured I'd share.

Thanks again, I could never have built my first Drupal site without all the good advice I found in these forums.

I have tried this over and

UNarmed - November 25, 2009 - 10:48

I have tried this over and over again and it simply dosent work for me =/

I need my search box to display on every page, how will that effect it?

I stil have the problem where the search block takes me to the search page and when i search from there i get the correct results.

Sigh turns out my theme had a

UNarmed - November 25, 2009 - 11:02

Sigh turns out my theme had a rubish search-block-form.tpl.php override that caused the rubish problem in the first place =/

All fixed now tho =]

 
 

Drupal is a registered trademark of Dries Buytaert.