-->

Creating a Google search box using HTML form

2020-07-18 03:51发布

问题:

So I am trying to insert a search form on my website that will redirect the user with their query to Google Search.

I used to be able to do this using:

<form role="search" method="get" id="searchform" class="searchform" action="http://www.google.co.uk/search?hl=en-GB&source=hp&q=">
  <div id="gform">
    <label class="screen-reader-text" for="s"></label>
    <input type="text" value="" name="s" id="s">
    <input type="submit" class="btn fa-input" value="&#xf002;" style="font-family: FontAwesome;">
  </div>
</form>

However, this no longer works. I'm guessing this is because Google have changed the search URL, but I am unable to replicate this effect by using Google's current URL.

Any idea how to fix this?

回答1:

Hi all it seems I have solved the problem myself

<div id="gform">
  <label class="screen-reader-text" for="s"></label>
  <input type="text" value="" name="q" id="s">
  <input type="submit" class="btn fa-input" value="&#xf002;" style="font-family: FontAwesome;">	
</div>
The key here that resolved my issue is name="q". This is what you need to make google search work; basically this is because google expects q as the name



回答2:

This snippet creates a simple google search box

<form action="http://www.google.com/search" method="get">
    <input type="text" name="q"/>
    <input type="submit" value="search" />
</form>