在我的网站上保存搜索关键词

时间:2012-10-16 14:00:10

标签: javascript html google-search

我正在开发一个网站,我正在使用Google搜索栏,我使用的代码

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

现在问题是我希望保存为搜索输入的关键词我尝试使用此代码

<form  id="form_body" method="get" action="http://www.google.com/search">
    <p>Google Search<input type="text" name="q"/>
    <input type="button" onclick="save1()" value="Search"/></p>
</form>

其中save1是我的javascript函数,它不起作用(完全不起作用)

2 个答案:

答案 0 :(得分:2)

当您在表单中挂钩提交按钮的click事件时,在页面卸载时不​​会触发/停止触发。相反,挂钩submit事件以确保您的代码在提交页面之前触发

<form  id="form_body" method="get" action="http://www.google.com/search" onsubmit="return save1();">               
<p>Google Search<input type="text" name="q"/>
<input type="submit" value="Search"/></p>

答案 1 :(得分:0)

在提交按钮时创建一个ajax调用,该按钮提取文本输入值,然后将其发送到php文件以将值存储在数据库中。