How can I add a Google search box to my website?

瘦欲@ 提交于 2019-11-26 18:07:59

问题


I am trying to add a Google search box to my own website. I would like it to search Google itself, not my site. There was some code I had that use to work, but no longer does:

<form method="get" action="https://www.google.com/search">
<input type="text" name="g" size="31" value="">
</form>

When I try making a search, it just directs to the Google homepage. Well, actually it directs here: https://www.google.com/webhp

Does anyone have a different solution? What am I doing wrong?


回答1:


Sorry for replying on an older question, but I would like to clarify the last question.

You use a "get" method for your form. When the name of your input-field is "g", it will make a URL like this:

https://www.google.com/search?g=[value from input-field]

But when you search with google, you notice the following URL:

https://www.google.nl/search?q=google+search+bar

Google uses the "q" Querystring variable as it's search-query. Therefor, renaming your field from "g" to "q" solved the problem.




回答2:


This is one of the way to add google site search to websites:

<form action="https://www.google.com/search" class="searchform" method="get" name="searchform" target="_blank">
<input name="sitesearch" type="hidden" value="example.com">
<input autocomplete="on" class="form-control search" name="q" placeholder="Search in example.com" required="required"  type="text">
<button class="button" type="submit">Search</button>
</form>



回答3:


There are couple of tricks that may help you. I guess, you are missing some configurations while using the Google code.

Look at these useful links:

  • Add a Google Search Box to Your Site
  • Getting Started - Google resources
  • Add Google Site Search to your Website Project
  • Configuring: Placing Google site search box and search results on your site



回答4:


Figured it out, folks! for the NAME of the text box, you have to use "q". I had "g" just for my own personal preferences. But apparently it has to be "q".

Anyone know why?



来源:https://stackoverflow.com/questions/13822927/how-can-i-add-a-google-search-box-to-my-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!