站点 A:开发中 (PHP) 站点 B:http://www.apprenticesearch.com/
我想在站点 A 上包含一个输入框;在我输入查询并按回车后,打开一个新选项卡以显示 B 的搜索结果。就好像我直接将搜索查询输入到 B 中一样。
例如,我搜索“testing”,检查 B 的搜索结果,我看到以下内容。我如何将查询从站点 A 传递到 B?
<div id="search">
<form action="/Resources/SiteSearch" id="siteSearchForm" method="post">
<label for="search-box">
SEARCH</label>
<div id="search-box-wrapper">
<input type="text" id="search-box">
</div>
<input id="searchText" name="searchText" type="hidden" value="testing"><input id="siteSearchUrl" name="siteSearchUrl" type="hidden" value="http://yboss.yahooapis.com/ysearch/limitedweb?format=xml&sites=www.apprenticesearch.com&q=testing">
<input type="image" src="/userfiles/images/E/buttons/go.png" id="search-button" value="GO" siteurl="www.apprenticesearch.com"><!-- www.apprenticesearch.com -->
</form>
</div>
最佳答案
您需要做的就是在站点 A 的表单中包含一个 target="_blank",请求在站点 B 上的工作方式也必须包含 siteSearchUrl 输入
<form id="apprenticeForm" action="http://www.apprenticesearch.com/Resources/SiteSearch" method="POST" target="_blank" onsubmit='submitSearch()'>
<input type="text" name="searchText" id="searchText" value=""/>
<input type="submit" value="Submit"/>
<input id="siteSearchUrl" name="siteSearchUrl" type="hidden" value="http://yboss.yahooapis.com/ysearch/limitedweb?format=xml&sites=www.apprenticesearch.com&q="/>
</form>
在尝试提交表单后,站点 B 没有读取 q 参数,我相信这是因为输入转义了 &
而不是 '&'。
我已经更新了 HTML,这里是在提交表单之前更新字段的 javascript
function submitSearch()
{
q = document.getElementById("searchText").value;
document.getElementById("siteSearchUrl").value = 'http://yboss.yahooapis.com/ysearch/limitedweb?format=xml&sites=www.apprenticesearch.com&q=' + q;
return true;
}
您需要使用 javascript 进行额外输入的原因与站点 B 使用 javascript 在其 script 中填充 siteSearchUrl 的原因相同。在发送请求之前。
查看服务器的工作原理:站点 B 将该请求发送到/Resources/SiteSearch,其中 SiteSearch(String searchtext, String siteSearchUrl)
函数在其服务器上被调用。如果您直接进入没有发布参数的那个页面,您会发现 System.ArgumentNullException Parameter name: uriString
被抛出,因为没有从服务器设置 siteSearchUrl。
如果要分析siteSearchUrl的参数:
- URL
yboss.yahooapis.com
用于 Yahoo's BOSS API服务 sites
参数告诉 API 要搜索和显示在结果中的网站q
参数是要搜索的查询
关于php - 搜索另一个站点的搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20062753/