Open In App

How to Create Form using CGI script

Last Updated : 04 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

sss

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

cgi11

html.html

Step 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

cgiu122

CGI scripts

Step 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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads