In the following form (essentially taken from web2py examples),
the "Enter" key does not work as expected, I am obliged
to click on the submit button.
=== test1/views/default/searchform.html ===
{{extend 'layout.html'}}
<p>Type something and press the button.</p>
<form>
<INPUT type="text" id='q' value=""/>
<INPUT type="button" value="submit"
onclick="ajax('{{=URL(r=request,f='search')}}',['q'],'target');"/>
</form>
<br/>
<div id="target"></div>
=== test1/controllers/default.py ===
#[...]
def searchform():
return dict()
#
def search():
chars = ""
if request.vars.q:
chars = request.vars.q # (to do: sanitize chars)
rows = db(db.contact.site_code.like('%'+chars+'%')|db.contact.customer_rsa.like('%'+chars+'%')).select()
return SQLTABLE(rows, truncate=48)
==========================
In my http://127.0.0.1:8000/test1/default/searchform page,
once the input field is filled with something, then:
- if I press enter, my browser (Firefox 3) apparently does
a GET of the same current URL ("searchform"), without passing parameters.
This is not what i expect.
- if I click on the submit button, i obtain the expected behavior,
i.e. the browser does a "POST /test1/default/search" passing
appropriately the "q=xxx" param.
How to obtain the same behavior whether the user hits Enter or he
clicks Submit?
Thanks
Had the same problem, this worked for me:
<form id="testform" onsubmit="return false">
Search for: <INPUT type="text" id='searchfor' value="" onchange="ajax('.....
That way, the form does not submit when enter is pressed, but onchange
triggers the ajax function and you get what you want
Horst