How to Create Form using CGI script
Last Updated :
28 Apr, 2025
HTML forms are a fundamental part of web development, allowing users to input data and interact with web applications. Web developers often use Common Gateway Interface (CGI) scripts to process and send the data submitted through these forms. In this article, we'll explain what Python CGI scripts are and guide you through how we can create a form using CGI Script.
Create Form Using CGI Scripts
Below are the steps by which we can create a form and run using Python CGI Script:
Step 1: Implementation
First, create one folder in htdocs folder named "python" and then create two files in that folder html.html and also python.py. We will write our HTML code inside the html.html file and Python CGI code inside the python.py file.
File Structure

Step 2: Write HTML File (html.html)
In this step, we will write our HTML file code in which we are creating a form. Inside this file that will later send the input data to our python.py file that will process it and print the output in the web browser.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Simple Form</title>
</head>
<style>
h2 {
color: green;
}
</style>
<body>
<h2>GeeksforGeeks</h2>
<form action="python.py" method="post">
<input type="text" name="username" >
<input type="submit" name="Submit">
</form>
</body>
</html>
Output
html.htmlStep 3: Write Python CGI Script (python.py)
We can also simply run the HTML file and its a optional step but we are also handling the input data from the form so we are making this CGI Script.
Python3
#!C:\Program Files\Python311\python.exe
import cgi
print("Content-type: text/html\n\n")
# This line sends an HTTP header specifying that the content is HTML.
# Import the Common Gateway Interface (CGI) module to handle form data.
form = cgi.FieldStorage()
# Create a FieldStorage object to parse and store the data submitted via the form.
username = form.getvalue("username")
# Retrieve the value associated with the "username" field from the form data.
# HTML header
print("<html>")
print("<head>")
print("<title>Success Page</title>")
print("</head>")
print("<body>")
# Format the username with <strong> and <span> for larger text
print("<p><strong><span style='font-size: 20px;'>{}</span></strong> Congratulations !!</p>".format(username))
# Display a congratulatory message with the submitted username in larger text.
# Print the second line on a new line
print("<p>You have successfully sent data from an HTML form to a CGI Python script.</p>")
# Display a message indicating that the form data was successfully processed.
# Close the HTML tags
print("</body>")
print("</html>")
Output
CGI scriptsStep 4: Configuration and Start the Xampp Server
You can refer Create a CGI Script for complete configuration and how we can start the server to run out CGI Script.
Step 5: Run the Script
In this step, open your xampp server and start the apche server and write the url like show
127.0.0.1/python/html.html
Video Demonstration
Similar Reads
How to generate webpages using CGI scripts? In this article, we will explore the process of generating webpages using CGI scripts. Furthermore, we will discuss the importance of CGI script implementation in facilitating dynamic content creation. Additionally, we'll delve into the intricacies of linking one page to another by creating multiple
4 min read
How to create a simple CGI script? In this article, we will explore how to create a simple CGI (Common Gateway Interface) script on a Windows machine but before that, we need some basic ideas about these technologies ( CGI Script, HTTP, Web Server ).CGI Script: A CGI script is a program that runs on a web server and generates dynamic
2 min read
How to handle form data in CGI Scripts? In this article, we'll guide you through the process of creating a registration form in HTML and then using a CGI script to fetch and display the submitted user data in the form of a card, along with a success message. Registration forms are a fundamental component of many web applications, allowing
4 min read
How to create a form using Django Forms ? This article explains how to create a basic form using various form fields and attributes. Creating a form in Django is very similar to creating a model, you define the fields you want and specify their types. For example, a registration form might need fields like First Name (CharField), Roll Numbe
2 min read
How to get form data using JavaScript/jQuery? The serializeArray() method creates an array of objects (name and value) by serializing form values. This method can be used to get the form data. Syntax: $(selector).serializeArray() Parameter: It does not accept any parameter. Return Value: It returns all the value that is inside the inputs fields
1 min read