librelist archives

« back to archive

get value from javascript

get value from javascript

From:
Mihai Sandu
Date:
2011-12-20 @ 08:14
Hi, I want to know how to get a value from a javascript function.
I tried with request.form but it doesn't work.
Thanks.

Re: [flask] get value from javascript

From:
Jesse Panganiban
Date:
2011-12-20 @ 08:54
You can access the passed values from the request object. If you set the 
contentType and the dataType to 'application/json' and 'json' 
respectively, you can get it from request.json object. It's a dict of 
values passed.

Use this:

from flask import request

# you controller

@app.route('/')
def index():
if request.method == 'POST':
     request.json['key']

On Tuesday, 20 December, 2011 04:14 PM, Mihai Sandu wrote:
> Hi, I want to know how to get a value from a javascript function.
> I tried with request.form but it doesn't work.
> Thanks.

Re: [flask] get value from javascript

From:
Ryo Mikami
Date:
2011-12-20 @ 08:55
Hi Mihai.
What do you mean?
You send request with "post" method?
If you send with "get" method, you can get it via "request.args.get('{key}')".
Or you can get json param via "request.json".

On Tue, Dec 20, 2011 at 5:14 PM, Mihai Sandu <voyagersm@gmail.com> wrote:
> Hi, I want to know how to get a value from a javascript function.
> I tried with request.form but it doesn't work.
> Thanks.

Re: [flask] get value from javascript

From:
Mihai Sandu
Date:
2011-12-20 @ 09:11
@app.route('/filter', methods=['POST', 'GET'])
def filter():
        if request.method == 'POST':
            print request.json['chosen']
        return render_template('main.html')


and the template (main.html) :
 <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
            function GetSelectedItem()   {
                len = document.filters.severity.length;
                i = 0;
                chosen = "";
                for(i=0; i<len; i++)   {
                    if(document.filters.severity[i].selected)  {
                        chosen=chosen + document.filters.severity[i].value
+ "\n"
                    }
                }
                return chosen
            }
        </script>

<form name="filters" action="{{ url_for('filter') }}" method=post
id="filters">
 Severity:
      </br>
      <select name="severity" multiple="multiple">
       <option value="0">Emergency
       <option value="1">Alert
       <option value="2">Critical
       <option value="3">Error
       <option value="4">Warning
       <option value="5">Notice
       <option value="6">Informational
       <option value="7">Debug
       </select>
<input type="submit" value="Set filters" onclick=GetSelectedItem()>


On Tue, Dec 20, 2011 at 10:55 AM, Ryo Mikami <tmp@mikamix.net> wrote:

> Hi Mihai.
> What do you mean?
> You send request with "post" method?
> If you send with "get" method, you can get it via
> "request.args.get('{key}')".
> Or you can get json param via "request.json".
>
> On Tue, Dec 20, 2011 at 5:14 PM, Mihai Sandu <voyagersm@gmail.com> wrote:
> > Hi, I want to know how to get a value from a javascript function.
> > I tried with request.form but it doesn't work.
> > Thanks.
>

Re: [flask] get value from javascript

From:
Indra Gunawan
Date:
2011-12-29 @ 01:14
In your case yu need save the value to some hidden field.

function GetSelectedItem() {
  // your code
  // return chosen

  $('#chosen').setValue(chosen); // set hidden field value
}

<form name="filters" ...>
  <input type="hidden" name="chosen" id="chosen" value="{{ chosen }}" />

</form>

if request.method == 'POST':
    print request.form.get('chosen');

coderbuzz
On Dec 20, 2011 4:13 PM, "Mihai Sandu" <voyagersm@gmail.com> wrote:

> @app.route('/filter', methods=['POST', 'GET'])
> def filter():
>         if request.method == 'POST':
>             print request.json['chosen']
>         return render_template('main.html')
>
>
> and the template (main.html) :
>  <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
>             function GetSelectedItem()   {
>                 len = document.filters.severity.length;
>                 i = 0;
>                 chosen = "";
>                 for(i=0; i<len; i++)   {
>                     if(document.filters.severity[i].selected)  {
>                         chosen=chosen + document.filters.severity[i].value
> + "\n"
>                     }
>                 }
>                 return chosen
>             }
>         </script>
>
> <form name="filters" action="{{ url_for('filter') }}" method=post
> id="filters">
>  Severity:
>       </br>
>       <select name="severity" multiple="multiple">
>        <option value="0">Emergency
>        <option value="1">Alert
>        <option value="2">Critical
>        <option value="3">Error
>        <option value="4">Warning
>        <option value="5">Notice
>        <option value="6">Informational
>        <option value="7">Debug
>        </select>
> <input type="submit" value="Set filters" onclick=GetSelectedItem()>
>
>
> On Tue, Dec 20, 2011 at 10:55 AM, Ryo Mikami <tmp@mikamix.net> wrote:
>
>> Hi Mihai.
>> What do you mean?
>> You send request with "post" method?
>> If you send with "get" method, you can get it via
>> "request.args.get('{key}')".
>> Or you can get json param via "request.json".
>>
>> On Tue, Dec 20, 2011 at 5:14 PM, Mihai Sandu <voyagersm@gmail.com> wrote:
>> > Hi, I want to know how to get a value from a javascript function.
>> > I tried with request.form but it doesn't work.
>> > Thanks.
>>
>
>

Re: [flask] get value from javascript

From:
Ryo Mikami
Date:
2011-12-21 @ 02:27
Ok, Mihai, Whath do you want to do?
What's the problem?

You are trying to get "json" param at server but your client code
passes it as normal post param(not json) via form.
And your client javascript function GetSelectedItem() does nothing(It
returns value but you don't use it).

If you just want to get "severity", you can do it like this.

request.form['severity']

On Tue, Dec 20, 2011 at 6:11 PM, Mihai Sandu <voyagersm@gmail.com> wrote:
> @app.route('/filter', methods=['POST', 'GET'])
> def filter():
>         if request.method == 'POST':
>             print request.json['chosen']
>         return render_template('main.html')
>
>
> and the template (main.html) :
>  <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
>             function GetSelectedItem()   {
>                 len = document.filters.severity.length;
>                 i = 0;
>                 chosen = "";
>                 for(i=0; i<len; i++)   {
>                     if(document.filters.severity[i].selected)  {
>                         chosen=chosen + document.filters.severity[i].value +
> "\n"
>                     }
>                 }
>                 return chosen
>             }
>         </script>
>
> <form name="filters" action="{{ url_for('filter') }}" method=post
> id="filters">
>  Severity:
>       </br>
>       <select name="severity" multiple="multiple">
>        <option value="0">Emergency
>        <option value="1">Alert
>        <option value="2">Critical
>        <option value="3">Error
>        <option value="4">Warning
>        <option value="5">Notice
>        <option value="6">Informational
>        <option value="7">Debug
>        </select>
> <input type="submit" value="Set filters" onclick=GetSelectedItem()>
>
>
> On Tue, Dec 20, 2011 at 10:55 AM, Ryo Mikami <tmp@mikamix.net> wrote:
>>
>> Hi Mihai.
>> What do you mean?
>> You send request with "post" method?
>> If you send with "get" method, you can get it via
>> "request.args.get('{key}')".
>> Or you can get json param via "request.json".
>>
>> On Tue, Dec 20, 2011 at 5:14 PM, Mihai Sandu <voyagersm@gmail.com> wrote:
>> > Hi, I want to know how to get a value from a javascript function.
>> > I tried with request.form but it doesn't work.
>> > Thanks.
>
>

Re: [flask] get value from javascript

From:
Mihai Sandu
Date:
2011-12-21 @ 08:22
All I want do to is to return the multiple selections from a list to flask.
And if I return only "severity" I get the first selection only, that I come
with the javascript function.

On Wed, Dec 21, 2011 at 4:27 AM, Ryo Mikami <tmp@mikamix.net> wrote:

> Ok, Mihai, Whath do you want to do?
> What's the problem?
>
> You are trying to get "json" param at server but your client code
> passes it as normal post param(not json) via form.
> And your client javascript function GetSelectedItem() does nothing(It
> returns value but you don't use it).
>
> If you just want to get "severity", you can do it like this.
>
> request.form['severity']
>
> On Tue, Dec 20, 2011 at 6:11 PM, Mihai Sandu <voyagersm@gmail.com> wrote:
> > @app.route('/filter', methods=['POST', 'GET'])
> > def filter():
> >         if request.method == 'POST':
> >             print request.json['chosen']
> >         return render_template('main.html')
> >
> >
> > and the template (main.html) :
> >  <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
> >             function GetSelectedItem()   {
> >                 len = document.filters.severity.length;
> >                 i = 0;
> >                 chosen = "";
> >                 for(i=0; i<len; i++)   {
> >                     if(document.filters.severity[i].selected)  {
> >                         chosen=chosen +
> document.filters.severity[i].value +
> > "\n"
> >                     }
> >                 }
> >                 return chosen
> >             }
> >         </script>
> >
> > <form name="filters" action="{{ url_for('filter') }}" method=post
> > id="filters">
> >  Severity:
> >       </br>
> >       <select name="severity" multiple="multiple">
> >        <option value="0">Emergency
> >        <option value="1">Alert
> >        <option value="2">Critical
> >        <option value="3">Error
> >        <option value="4">Warning
> >        <option value="5">Notice
> >        <option value="6">Informational
> >        <option value="7">Debug
> >        </select>
> > <input type="submit" value="Set filters" onclick=GetSelectedItem()>
> >
> >
> > On Tue, Dec 20, 2011 at 10:55 AM, Ryo Mikami <tmp@mikamix.net> wrote:
> >>
> >> Hi Mihai.
> >> What do you mean?
> >> You send request with "post" method?
> >> If you send with "get" method, you can get it via
> >> "request.args.get('{key}')".
> >> Or you can get json param via "request.json".
> >>
> >> On Tue, Dec 20, 2011 at 5:14 PM, Mihai Sandu <voyagersm@gmail.com>
> wrote:
> >> > Hi, I want to know how to get a value from a javascript function.
> >> > I tried with request.form but it doesn't work.
> >> > Thanks.
> >
> >
>

Re: [flask] get value from javascript

From:
Ryo Mikami
Date:
2011-12-22 @ 01:13
You never "Return" something from client to server.
"Client Sends Request" and "Server Returns Response".
If you want to get something at server, you have to send it from client.
Your js code does not send request, so you have to fix it.

Sorry but It's not flask matter. It's about http, html, and javascript.
If you want to know how to send request param as json format via javascript,
you can find document like this
http://stackoverflow.com/questions/191881/serializing-to-json-in-jquery.

On Wed, Dec 21, 2011 at 5:22 PM, Mihai Sandu <voyagersm@gmail.com> wrote:
> All I want do to is to return the multiple selections from a list to flask.
> And if I return only "severity" I get the first selection only, that I come
> with the javascript function.
>
>
> On Wed, Dec 21, 2011 at 4:27 AM, Ryo Mikami <tmp@mikamix.net> wrote:
>>
>> Ok, Mihai, Whath do you want to do?
>> What's the problem?
>>
>> You are trying to get "json" param at server but your client code
>> passes it as normal post param(not json) via form.
>> And your client javascript function GetSelectedItem() does nothing(It
>> returns value but you don't use it).
>>
>> If you just want to get "severity", you can do it like this.
>>
>> request.form['severity']
>>
>> On Tue, Dec 20, 2011 at 6:11 PM, Mihai Sandu <voyagersm@gmail.com> wrote:
>> > @app.route('/filter', methods=['POST', 'GET'])
>> > def filter():
>> >         if request.method == 'POST':
>> >             print request.json['chosen']
>> >         return render_template('main.html')
>> >
>> >
>> > and the template (main.html) :
>> >  <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
>> >             function GetSelectedItem()   {
>> >                 len = document.filters.severity.length;
>> >                 i = 0;
>> >                 chosen = "";
>> >                 for(i=0; i<len; i++)   {
>> >                     if(document.filters.severity[i].selected)  {
>> >                         chosen=chosen +
>> > document.filters.severity[i].value +
>> > "\n"
>> >                     }
>> >                 }
>> >                 return chosen
>> >             }
>> >         </script>
>> >
>> > <form name="filters" action="{{ url_for('filter') }}" method=post
>> > id="filters">
>> >  Severity:
>> >       </br>
>> >       <select name="severity" multiple="multiple">
>> >        <option value="0">Emergency
>> >        <option value="1">Alert
>> >        <option value="2">Critical
>> >        <option value="3">Error
>> >        <option value="4">Warning
>> >        <option value="5">Notice
>> >        <option value="6">Informational
>> >        <option value="7">Debug
>> >        </select>
>> > <input type="submit" value="Set filters" onclick=GetSelectedItem()>
>> >
>> >
>> > On Tue, Dec 20, 2011 at 10:55 AM, Ryo Mikami <tmp@mikamix.net> wrote:
>> >>
>> >> Hi Mihai.
>> >> What do you mean?
>> >> You send request with "post" method?
>> >> If you send with "get" method, you can get it via
>> >> "request.args.get('{key}')".
>> >> Or you can get json param via "request.json".
>> >>
>> >> On Tue, Dec 20, 2011 at 5:14 PM, Mihai Sandu <voyagersm@gmail.com>
>> >> wrote:
>> >> > Hi, I want to know how to get a value from a javascript function.
>> >> > I tried with request.form but it doesn't work.
>> >> > Thanks.
>> >
>> >
>
>