1

I have this part of script from my GAE application which uses webapp2, which accepts data from a form using post,

class RenderMarksheet(webapp2.RequestHandler):
   def post(self):
     regno = self.request.get('content')  # Here's where I extract the data from the form
     ...
     ...
     ...
     self.response.out.write(template.render(templates/render.html, template_values))

And the web form which posts to this script,

<form action="/sign" method="post" name="inputform" onsubmit="return validate()">
    Register No : <input type="number" name="regno" placeholder="Your Register No."/> 
                  <input type="submit" value="Get-My-GPA!" >
</form>

Now, I want to manually pass a specific data (a register no.), without using the submit button from the form, to the python script( or the url, perhaps) , using Javascript, say a button that triggers a javascript method.

I have to POST the data using javascript(to implement AJAX). In python I do this, to post the data to a url,

import http.client, urllib.parse
params = urllib.parse.urlencode({'regno':10109104021})
headers = {"Content-type": "application/x-www-form-urlencoded",
       "Accept": "text/plain"}
conn = http.client.HTTPConnection("mydomain:8888")
conn.request("POST", "/sign", params, headers)
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()

How can I post the data to the url, via Jquery or Javascript?

4
  • 1
    If your data shows up in the url, then it's not doing a POST, it's doing a GET Commented Jun 26, 2012 at 21:46
  • 1
    What are you using as a trigger if not the submit button? Commented Jun 26, 2012 at 21:46
  • I'm unclear on what you're doing. Your series of events is unclear. Are you passing this register number after the form has been posted and you want to automatically post something else? What is the purpose of the link? Are you getting data from a webservice? You'll probably want to look at urllib2. Commented Jun 26, 2012 at 22:00
  • Forget about the <form>. Let the trigger be a button, that calls a javascript method. Now, how do I pass a specific register no. via GET or POST to the python script(from Javascript)? Commented Jun 26, 2012 at 22:18

2 Answers 2

2

fastest is to use jQuery and use $.post()

Sign up to request clarification or add additional context in comments.

10 Comments

I'm looking for the appropriate syntactical way cuzzea, I usually post data like this "myurl?var=data", but I need to know how to do that for 'url handlers'. If I get to know that, then I can use the jquery stuff.
The way you're used to doing it us using the GET method.
"myurl?var=data" is GET. The post is sent in the header en.wikipedia.org/wiki/POST_(HTTP). In jQuery you can use $.post() api.jquery.com/jQuery.post and add the data as a js object {a:1,b:'c'}
@cuzzea Yes, I think I am. So the real question is, "how do I submit a form using the POST method without a submit button?"
using jquery: $('#formid').submit()
|
1

I'm not 100% sure what you're trying to do, but after several rereads here's what I think you're trying to do.

you'll need to import urllib2

#import google classes
import urllib2

class RenderMarksheet(webapp2.RequestHandler):
   def parseResponse(self, response):
        #run some code in here to parse the results since it's an HTML document ... beautifulsoup, perhaps?


   def post(self):
     regno = self.request.get('content')  
     rawlink = "http://result.annauniv.edu/cgi-bin/result/result11gr.pl?regno="  
     link = rawlink+regno 
     try:
       result = urllib2.urlopen(link)
       gpa = parseResponse(result)
     except urllib2.URLError, e:
       self.response.out.write(e)

     template_values =  {'gpa': gpa}

     self.response.out.write(template.render(templates/render.html, template_values))

This method will

  1. Take the input from the form
  2. Build the link
  3. Request information from the annauniv webserver
  4. Parse the response from that server (you're on your own parsing that, but you'd have to do it regardless)
  5. Store the GPA a templates dictionary for use in your template

2 Comments

Thanks a lot swasheck, thats exactly what I'm trying to do except for one doubt. I passed the user filled reg no. via a form, and extracted the data in the script using regno = self.request.get('content'). Now,I'm trying to pass a reg-no. without using the <form> to the script. say, using a button that calls a javascript method. How do I go about this? thanks in advance.
I dont understand what you mean, sorry. I thought "content" was the regno which you were going to use to get GPA information using the annauniv.edu link (handled by urllib2).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.