title: POST data to python script
body: <p>I have an html page hosted at my local server, apache2, I want to send some data to a python script and manipulate it, I use the when I use the <strong>action</strong> tag of the form, It doesn't navigate to the next page and when I try Jquery the script doesn't get executed. Any help? Thanks in advance.</p>
  
  <p>Html:</p>
  
  <pre><code>&lt;body&gt;
    &lt;div id="Header"&gt;&lt;/div&gt;
      &lt;div id="wrapper"&gt;
      &lt;form name="login-form" class="login-form" action="/cgi-bin/test.py" method="post" onsubmit="validate();return false;"&gt;
        &lt;div class="header"&gt;
          &lt;h1&gt;Enter Your Credentials&lt;/h1&gt;
          &lt;span&gt;URL,Admin,Tenant,Password&lt;/span&gt;
        &lt;/div&gt;
        &lt;div class="content"&gt;
          &lt;input id="Uname" name="url" type="text" class="input username" placeholder="Keystone URL" /&gt;
          &lt;div class="user-icon"&gt;&lt;/div&gt;
          &lt;input id="Pname"name="admin" type="password" class="input password" placeholder="Admin" /&gt;
          &lt;div class="pass-icon"&gt;&lt;/div&gt;
          &lt;input id="oPass"name="password" type="password" class="input password" placeholder="Password" /&gt;
          &lt;div class="pass-icon"&gt;&lt;/div&gt;
          &lt;input id="tenant"name="tenant" type="password" class="input password" placeholder="Tenant" /&gt;
          &lt;div class="pass-icon"&gt;&lt;/div&gt;   
        &lt;/div&gt;
        &lt;div class="footer"&gt;
          &lt;input type="submit" name="submit" value="Submit" class="button"/&gt;
        &lt;/div&gt;
      &lt;/form&gt;
    &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>This way the script gets executed perfectly but doesn't navigate to next page.</p>
  
  <p>Javascript Function:</p>
  
  <pre><code>function validate() {
      var un = document.getElementById("Uname").value;
      var valid = false;
  
      var unArray = ["http://192.168.1.102:5000/v2.0"]; 
      for (var i = 0; i &lt; unArray.length; i++) {
          if ((un == unArray[i])) {
              valid = true;
              break; 
          }
      }
  
      if (valid) {
          alert("Login was successful"); 
          location.href = 'HomePage.html';
          document.login-form.action='/cgi-bin/test.py';
  
          return false; 
      } else {
          alert("Login was Unsuccessful");
          return false;
      }
  }
  </code></pre>
  
  <p>This is the Ajax call that I try to make:</p>
  
  <pre><code>$.ajax({
    type: "POST",
    url: "/cgi-bin/test.py",
    data: "stuff_for_python="+document.getElementById("Uname").value,
    success: function(response) {    
        alert(response);      
    },
    error: function(data) {
        alert(data.responseText);
    }
  });
  </code></pre>
  
  <p>Python Script:</p>
  
  <pre><code>#!/usr/bin/python
  import cgi, cgitb 
  from StringIO import StringIO
  import json
  from io import BytesIO
  import pycurl
  
  cgitb.enable()
  # Create instance of FieldStorage 
  form = cgi.FieldStorage() 
  
  # Get data from fields
  url = form.getvalue('stuff_for_python')
  
  print "Content-type: text/html\r\n\r\n\n"
  print
  
  print url
  f = open('/home/stack/writing.txt','wb')
  f.write(url)
  f.close()
  </code></pre>