2012-07-23 88 views
1

我正在使用使用其他搜索方法的电子商务引擎脚本。HTML表单元素到URL

取而代之的是URL使用GET这样的:

http://search.com/searchc?q=the+query 

它采用

http://search.com/searchc/the+query 

我怎样才能让一个表单POST或者GET来,因为这种形式,使URL

http://search.com/searchc/?q=the+query 


<form action="/searchc/" method="post"> 
    <input type="text" id="q" name="q"> 
    <input type="submit" value="go"> 
</form> 

也试过这个(获得或发布不适用于这两个)

<form action="/searchc/" method="post"> 
    <input type="text" id="" name=""> 
    <input type="submit" value="go"> 
</form> 

回答

1

可靠的方式有两个组件:客户端JavaScript操作,根据需要将表单提交到请求,以及(作为非JS情况的备份)一个简单的服务器端重定向实用程序,它从表单接收请求并修改后重定向它。

像这样(为GET情况下):

<form action="http://www.example.com/redirect" 
    onsubmit="location.href = document.getElementById('f1').value + 
    document.getElementById('q').value; return false"> 
<input type="text" id="q" name="f2"> 
<input type="submit" value="go"> 
<input type=hidden id=f1 name=f1 value="http://search.com/search/"> 
</form> 

这里http://www.example.com/redirect是一些服务器端的表单处理程序,只是读取表单字段并拿起命名为F1领域,F2,...,会连接他们成一个单一的字符串,并使用它作为一个URL重定向。作为CGI脚本,这将是

use CGI qw(:standard); 
$dest = ''; 
$i = 1; 
while(param('f'.$i)) { 
    $dest .= param('f'.$i++); } 
print "Location: $dest\n\n"; 
1
<form action="/searchc/" method="post" onsubmit="this.action+=this.q.value;return true"> 
    <input type="text" id="q"> 
    <input type="submit" value="go"> 
</form> 

空间将所需提交为%20

可以使用

this.action+=this.q.value.split(' ').join('+') 

,以取代他们

0

这是很奇怪的URL模式,但无论如何,你可以做什么如:

$(function() { 
    $('form').submit(function() { 
     var url = '/searchc/' + encodeURIComponent($(this).find('[name=q]').val()); 
     window.location = url; 
     return false; 
    });  
});