How to Create Form using CGI script
Last Updated :
04 Nov, 2023
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.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
import cgi
print ( "Content-type: text/html\n\n" )
form = cgi.FieldStorage()
username = form.getvalue( "username" )
print ( "<html>" )
print ( "<head>" )
print ( "<title>Success Page</title>" )
print ( "</head>" )
print ( "<body>" )
print ( "<p><strong><span style='font-size: 20px;'>{}</span></strong> Congratulations !!</p>" . format (username))
print ( "<p>You have successfully sent data from an HTML form to a CGI Python script.</p>" )
print ( "</body>" )
print ( "</html>" )
|
Output
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
Share your thoughts in the comments
Please Login to comment...