title: How to post form data via ajax to a python script?
body: <p>I'm struggling with a python program and an ajax request. I'm trying to get some data from my Javascript into the python program, the normal method I've been using .getfirst(field name) doesn't work which I assume is because the request is via ajax (sorry, I'm quite new to all this) so I've tried using the following code</p>
  
  <p>Python:</p>
  
  <pre><code>import MySQLdb
  import cgi, cgitb
  
  def index(req):
  
      # Create instance of FieldStorage
      form = cgi.FieldStorage()
  
      # Get data from fields
      dtbox = form.getvalue('dt')
      tmbox = form.getvalue('tm')
  
      con = MySQLdb.connect('localhost', 'root', '', 'mydb')
  
      with con:
          cur = con.cursor(MySQLdb.cursors.DictCursor)
          s = "SELECT tmp, watts FROM currentcost WHERE dt ='" + dtbox + "' and tm like '" + tmbox + "%'"
          cur.execute (s)
          rows = cur.fetchall()
  
          x=""
          y=""
          for row in rows:
              x=x+row["watts"]+","
              y=y+row["tmp"]+","
  
      x="data:["+x+"]"
      y="data:["+y+"]"
  
      con.close()
  
      req.write(x)
  </code></pre>
  
  <p>Javascript snippet:</p>
  
  <pre><code>function draw(handleResponse) {
      $.ajax({
          url: "/currentcost.py",
          data: {dt: frm.dt, tm: frm.tm},
          success: function(response){
              handleResponse(response);
          }
      });
  
  &lt;form name="frm" target="ifrm"&gt;
      &lt;iframe name="ifrm" id="ifrm" style="display:none"&gt;&lt;/iframe&gt;
          &lt;fieldset style="width:300px"&gt;
              &lt;legend&gt;Chart Date and Time&lt;/legend&gt;
              Alter the date and time settings &lt;br&gt;
              Date:
              &lt;select name="dt"&gt;
  </code></pre>
  
  <p>I'm expecting the form values dt and tm to be transferred to the python program where it will pick them out and run through my select query ... all I get back is a blank :-(</p>
  
  <p>Thanks in anticipation of your help</p>
  
  <p>Chris</p>