Jump to content


Photo

Search form default value


  • Please log in to reply
10 replies to this topic

#1 nikola

nikola

    Sr. Member

  • Members
  • PipPipPipPip
  • 224 posts
  • 81

  • LocationZagreb, Croatia

Posted 03 May 2012 - 02:30 AM

How can I add default value to search form input field, for instance: value="Enter your search term" when using:

<?php echo htmlentities($input->whitelist('term'), ENT_QUOTES, 'UTF-8'); ?>

that "occupies" input field value.

That default value ("Enter your search term") would have to be excluded from the search.

The behaveiour I'm looking for is similar to search field in PW admin that has value "Search"...
Check out my ProcessWire admin themes: Futura Remixed Admin Theme / Moderna Admin Theme / Futura Admin Theme

#2 DaveP

DaveP

    Sr. Member

  • Members
  • PipPipPipPip
  • 287 posts
  • 138

  • LocationChorley, UK

Posted 03 May 2012 - 03:03 AM

There are a few options, including

Javascript
<form action="SCRIPT-NAME" method="post">
<input type="text" value="Search..." name="search"
	onfocus="if (this.value == 'Search...') {this.value = '';}"
	onblur="if (this.value == '') {this.value = 'Search...';}" />
</form>

and in HTML5
<input type="text" name="search" placeholder="Search..."  />
which works in most up to date browsers (but I think not IE9, although I haven't checked recently).
Twitter : Facebook : GitHub : G+ : Blog : Powered by C8H10N4O2 and C10H14N2

#3 yellowled

yellowled

    Sr. Member

  • Members
  • PipPipPipPip
  • 183 posts
  • 117

  • LocationEutin, Germany

Posted 03 May 2012 - 05:53 AM

which works in most up to date browsers (but I think not IE9, although I haven't checked recently).

There are polyfills for the HTML5 placeholder attribute (i.e. pieces of usually javascript to replicate a certain feature in browsers who do not provide it natively). The Modernizr wiki on GitHub has a pretty comprehensive list of them.

Personally, I think placeholder doesn't need to be polyfilled, it's fine to not have it in older browsers. If that's not feasible, the polyfill by Mathias Bynens works pretty good.

#4 nikola

nikola

    Sr. Member

  • Members
  • PipPipPipPip
  • 224 posts
  • 81

  • LocationZagreb, Croatia

Posted 03 May 2012 - 07:41 AM

Thank you both, I ended up with a small bit of jQuery:

$('input[name=search]').attr({value: 'Search'});

Check out my ProcessWire admin themes: Futura Remixed Admin Theme / Moderna Admin Theme / Futura Admin Theme

#5 yellowled

yellowled

    Sr. Member

  • Members
  • PipPipPipPip
  • 183 posts
  • 117

  • LocationEutin, Germany

Posted 03 May 2012 - 08:23 AM

That will probably perform a search with the search term "Search" if somebody submits the search form without having entered actual search terms. This is not always desirable, and it does not happen if you use the placeholder attribute. (Just sayin'.)

#6 DaveP

DaveP

    Sr. Member

  • Members
  • PipPipPipPip
  • 287 posts
  • 138

  • LocationChorley, UK

Posted 03 May 2012 - 08:32 AM

And you could do the same without jQuery just by adding
value="Search"
to the <input> declaration. (Just sayin'.)
Twitter : Facebook : GitHub : G+ : Blog : Powered by C8H10N4O2 and C10H14N2

#7 nikola

nikola

    Sr. Member

  • Members
  • PipPipPipPip
  • 224 posts
  • 81

  • LocationZagreb, Croatia

Posted 03 May 2012 - 11:16 AM

I ended up using this script: http://webcloud.se/c...ery-Placeholder and it does exactly what I needed.
Check out my ProcessWire admin themes: Futura Remixed Admin Theme / Moderna Admin Theme / Futura Admin Theme

#8 AnotherAndrew

AnotherAndrew

    Sr. Member

  • Members
  • PipPipPipPip
  • 162 posts
  • 24

Posted 03 May 2012 - 11:22 AM

I have tried all of these suggestions and it doesn't work....Unhappy.

#9 nikola

nikola

    Sr. Member

  • Members
  • PipPipPipPip
  • 224 posts
  • 81

  • LocationZagreb, Croatia

Posted 03 May 2012 - 11:31 AM

If you use search form from default PW template, you could do the following with the above script mentioned.

<input type='text' name='q' id='search_query' value='<?php echo htmlentities($input->whitelist('q'), ENT_QUOTES, 'UTF-8'); ?>' />

Add HTML5 placeholder to input field like this:

<input type='text' name='q' id='search_query' placeholder='Search' value='<?php echo htmlentities($input->whitelist('q'), ENT_QUOTES, 'UTF-8'); ?>' />

Include the script above to your head section and trigger it like this (also in the head):

<script>
$(document).ready(function() {
$('input[name="q"]').placeholder();
});
</script>

Check out my ProcessWire admin themes: Futura Remixed Admin Theme / Moderna Admin Theme / Futura Admin Theme

#10 AnotherAndrew

AnotherAndrew

    Sr. Member

  • Members
  • PipPipPipPip
  • 162 posts
  • 24

Posted 03 May 2012 - 12:11 PM

Thanks Nikola. Turns out that I was not calling the correct character set in my meta tag. And all of the suggested methods above work now.

#11 yellowled

yellowled

    Sr. Member

  • Members
  • PipPipPipPip
  • 183 posts
  • 117

  • LocationEutin, Germany

Posted 03 May 2012 - 12:57 PM

I ended up using this script: http://webcloud.se/c...ery-Placeholder and it does exactly what I needed.


I have never used that particular one, but just from experience: placeholder polyfills can behave erratically in some form environments. Some of them use relative/absolute positioning to overlay the input element with a span which can have various side effects depending on the rest of the CSS. So always make sure you test this properly, it can come back and bite you. ;)

As always, all this depends on which browsers you have to/want to support. Then again, if you don't have to support old IE, polyfills are usually not necessary in the first place. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users