if you want "just an example" without the input field - here's one way
to do it (which decouples the id from the request.var name):
def index():
return dict()
def process():
# if you don't want to have to know the name of the var - 'test',
it
# gets a bit ugly - to get request.vars[0] in effect:
# x = dict(request.vars).values()[0]
x=request.vars.test.upper()
return x
# index.html:
{{extend 'layout.html'}}
<div id='test'>
MaSsImO wAs HeRe!
</div>
<div id="target">Result of your process() will be here<div>
<button onclick="my_ajax('process','test','target')"> click this</
button>
<script type="text/javascript"><!--
function my_ajax(u, s, t){
jQuery.ajax( {
type: 'POST',
url: u,
data: {test : document.getElementById(s).innerHTML},
success: function(msg) {
document.getElementById(t).innerHTML=msg;
}
} );
};
--></script>
On Jun 20, 1:58 pm, Yarko Tymciurak <[email protected]>
wrote:
> Well, maybe this is a good exercise / tutorial in debugging:
>
> On Jun 20, 9:02 am, mdipierro <[email protected]> wrote:
>
> # this just displays view/default/index.html as it is> def index():
> > return dict()
>
> # demo: get input from the client and do something with it
> # - split, so you can put a breakpoint, and see separately what was
> sent, what is returned> def process():
> > # return request.vars.test.upper()
>
> x = request.vars.test # note: this is rigid: couples
> process() to the id-tag: test
> return x.upper() # just uppercase the input
>
>
>
> > #index.html
> > {{extend 'layout.html'}}
>
> <!- A js debugger will show that there is an error: a missing id
> 'test';
> let's just add that to this input field;
>
> NOTE: this example has confused two important things:
>
> - if you had an input, and submitted, it's value would have been
> labeled by the 'name' tag - just to clarify that is true, let's rename
> it also
>
> - since id is what is being tagged, we'll also add a non-input id
> so you can see the full effect of this
> -->
> <input id='test' name='test_name'>
>
> <!- this id is where the result of your controller 'process()' will
> be placed;
> if you had anything in the div, it would be replaced
> -->
>
> > <div id="target"><div>
>
> <!- To see the next bug, you have to inspect the element "click me"
> in your browser;
>
> You will see that the call is formed as "ajax( '/my_app/
> process', ['test'], 'target')"
>
> First guess would make you think that you need to add the
> 'default' controller, but this is wrong too....
>
> It may not be clear - this is the ajax call defined in views/
> web2py_ajax.hml
>
> This in turn uses jQuery.ajax() --- and we won't get into
> debugging that here, but in general note that you could insert a
> breakpoint in your js by inserting a js call where
> you want to break: debugger;
>
> For now, note the documentation
> athttp://www.web2py.com/book/default/section/10/3?search=ajax
>
> I'll leave it to you as an exercise to see if adding a <base
> href=...> tag to the head of your document would allow you to use the
> output of URL()... I am not sure.
>
> > <button onclick="ajax('{{=URL(r=request,f='process')}}',
> > ['test'],'target')">click me</button>
>
> -->
>
> <button onclick="ajax('process', ['test'], 'target')">click me</
> button>
>
> Note that you could replace the <input id=....> field just as easily
> with something like:
>
> <span id='test'>All sorts of stuff ....</span>
>
> Hopefully, this has been helpful.
>
> - Yarko
>
>
>
>
>
> > On Jun 19, 5:42 am, ilovesss2004 <[email protected]> wrote:
>
> > > Hi,
> > > How can I let python controller get the value of a textfield when I
> > > press a button in html page (not a submit button)?
>
> > > Thanks a lot.