Tuesday, April 5, 2016

Processing HTML form data with Python and PHP Script

In today's post, am going to work through "how to Process HTML form with Python and PHP Script".

Html forms on websites are processed using a server-side programming language such as PHP, Python, Ruby, Java, Asp, Perl etc. Therefore, your website server most have support for the programming language you wish to use in processing the form.

In this tutorial am going to use Apache web server on a local windows PC. You can run Apache on Windows via easy to use tools such as WAMP, XAMPP, or AMPPS.

I assumed you have the XAMPP web server and Python2 installed. However, you are free to use any of the web servers listed above with the compatible version of python.

Let's get started....

Html form

The html form we are going to use has just three (3) elements; two text fields and a submit button. The code for the form is as below;-


Note that the "action" attribute is empty, it most be changed accordingly in the next step for php or python.

Now go to your XAMPP htdocs directory and create a folder name it "project". That is your location will look like this: C:\xampp\htdocs\project. Copy the html code above and save it in a file named "myForm" with .html extension at this location "C:\xampp\htdocs\project" then move to the next stage of this tutorial below.

Result of the above code file on the browser is as the image below;-

Your file location should look like: http://localhost/project/myForm.html


Processing Html form with PHP Script

PHP is supported on XAMPP and all the web servers listed above by default.

Create a file named "process_form" with the code below and save it in a file with .php extension at this location "C:\xampp\htdocs\project" where the html form file (myForm.html) above is located.


Edit the html form "action" attribute to point to the php file above (process_form.php).


Now make sure XAMPP Apache server is running, then load the form (http://localhost/project/myForm.html) on your browser. Fill it and submit, it will be processed with (process_form.php) and display the out put as below;-





Processing Html form with Python Script

The approach is similar to that of PHP above, but with little modifications as follow.

While the AMPPS server has python support by default, you need to configure python for XAMPP and WAMP. To configure Python to run on XAMPP server, read this post.

Create a file named "process_form" with the code below and save it in a file with .py or .cgi extension at this location "C:\xampp\htdocs\project" where the html form file (myForm.html) above is located.


Edit the html form "action" attribute to point to the (cgi or py) python file above (process_form.cgi).


Now make sure XAMPP Apache server is running, then load the form (http://localhost/project/myForm.html) on your browser. Fill it and submit, it will be processed with (process_form.php) and display the out put as below;-



Download tutorial files here

That is it! Thank you for reading

11 comments:

  1. I'm using ampps
    I tried your python example and when I submit the form I get this error

    Forbidden
    You don't have permission to access /project/process_form.cgi on this server.
    Please help me

    ReplyDelete
    Replies
    1. Am yet to use ampps, consider asking on their official list/forum. Thanks

      Delete
  2. Hello, It's not working for me. I tried to do the first one examples. In the action field of html I entered process_form.php. From localhost/project/myForm.html I've entered in the fields some data but insteed of opening the process_form.php it was trying to open the .cgi file and i get only the content of the .cgi file
    how is that? how can i fix it?

    ReplyDelete
    Replies
    1. @Antonina Panayiotou, I think your server isn't properly configured or it isn't running at all. What server are you using?

      Delete
    2. i'm running with flask, which a is a framework and works fine as a server

      Delete
  3. __init__ self.fp = sys.stdin.buffer AttributeError: 'NoneType' object has no attribute 'buffer' I am getting this error. How cani resolve it?

    ReplyDelete
  4. Kimberly BuenasedaJune 6, 2017 at 8:04 AM

    hi when im running the .py file. The program is not running it only show the code of the .py file. How can fix this? Im using xampp

    ReplyDelete
    Replies
    1. Setup XAMPP properly, see; https://umar-yusuf.blogspot.com.ng/2016/04/how-to-run-python-on-xampp-web-server.html

      Delete
  5. Nothing is Impossible

    ReplyDelete