Last Name : * * * ## Creating Forms and HTML The CGI library includes many methods for creating HTML, with a corresponding method for each HTML tag. Before using these methods, you must create a CGI object via `CGI.new`. To make tag nesting simpler, these methods take content as a code block, which returns a string as the tag's content. As shown below: ```ruby #!/usr/bin/ruby require "cgi" cgi = CGI.new("html4") cgi.out{ cgi.html{ cgi.head{ "n"+cgi.title{"This Is a Test"} } + cgi.body{ "n"+ cgi.form{ "n"+ cgi.hr + cgi.h1 { "A Form: " } + "n"+ cgi.textarea("get_text") + "n"+ cgi.br + cgi.submit } } } } * * * ## String Escaping When processing parameters in URLs or HTML form data, you need to escape specific special characters, such as quotes (`"`) and backslashes (``). The Ruby CGI object provides `CGI.escape` and `CGI.unescape` methods to handle the escaping of these special characters: ```ruby #!/usr/bin/ruby require 'cgi' puts CGI.escape('Zara Ali/A Sweet & Sour Girl') The result of the above code is: Zara+Ali%2FA+Sweet+%26+Sour+Girl Another example: ```ruby #!/usr/bin/ruby require 'cgi' puts CGI.escapeHTML('
Ruby Cgi
Ruby is a general-purpose language, not just one for web development, but its use in web applications and web tools is the most common.
With Ruby, you can not only write your own SMTP servers, FTP programs, or a Ruby web server, but you can also use Ruby for CGI programming.
Next, let's take some time to learn about Ruby's CGI programming.
* * *
## Web Browsing
To better understand how CGI works, we can look at the process of clicking a link or URL on a webpage:
* 1. Use your browser to access the URL and connect to the HTTP web server.
* 2. After receiving the request, the web server parses the URL and checks if the requested file exists on the server. If it does, it returns the file's content; otherwise, it returns an error message.
* 3. The browser receives the information from the server and displays the received file or error message.
CGI programs can be Ruby scripts, Python scripts, PERL scripts, SHELL scripts, C or C++ programs, etc.
* * *
## CGI Architecture Diagram

* * *
## Web Server Support and Configuration
Before you start CGI programming, ensure your web server supports CGI and that the CGI handler is configured.
Apache supports CGI configuration:
Set up the CGI directory:
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
All HTTP servers execute CGI programs stored in a pre-configured directory. This directory is called the CGI directory, and by convention, it is named `/var/www/cgi-bin`.
The extension for CGI files is `.cgi`, and Ruby can also use the `.rb` extension.
By default, Linux servers are configured to run the cgi-bin directory at `/var/www`.
If you want to specify a different directory for running CGI scripts, you can modify the `httpd.conf` configuration file as follows:
AllowOverride None
Options +ExecCGI
Order allow,deny
Allow from all
Add the `.rb` suffix in `AddHandler` so that we can access Ruby script files ending with `.rb`:
AddHandler cgi-script .cgi .pl .rb
* * *
## Writing CGI Scripts
The most basic Ruby CGI code is as follows:
```ruby
#!/usr/bin/ruby
puts "Content-type: text/htmlnn"
puts "This is a test"
You can save this code to a `test.cgi` file, upload it to the server, and give it sufficient permissions to execute as a CGI script.
If your site's address is `http://www.example.com/`, you can access the program via `http://www.example.com/test.cgi`, and the output will be: "This is a test.".
After the browser accesses this URL, the web server will find the `test.cgi` file in the site directory, then parse the script code through the Ruby interpreter and serve the HTML document.
* * *
## Using cgi.rb
Ruby can call the CGI library to write more complex CGI scripts.
The following code calls the CGI library to create a CGI script.
```ruby
#!/usr/bin/ruby
require 'cgi'
cgi = CGI.new
puts cgi.header
puts "This is a test"
In the code above, a CGI object is created and the header information is printed.
* * *
## Form Handling
Using the CGI library, you can obtain data submitted by a form (or parameters in a URL) in two ways. For example, the URL: `/cgi-bin/test.cgi?FirstName=Zara&LastName=Ali`.
You can use `CGI#[]` to directly get the parameters `FirstName` and `LastName`:
```ruby
#!/usr/bin/ruby
require 'cgi'
cgi = CGI.new
cgi['FirstName'] # =>
cgi['LastName'] # =>
Another way to get form data:
```ruby
#!/usr/bin/ruby
require 'cgi'
cgi = CGI.new
h = cgi.params # => {"FirstName"=>,"LastName"=>}
h['FirstName'] # =>
h['LastName'] # =>
The following code retrieves all keys:
```ruby
#!/usr/bin/ruby
require 'cgi'
cgi = CGI.new
cgi.keys # => ["FirstName", "LastName"]
If the form contains multiple fields with the same name, the values for that field will be stored in an array.
In the following example, three fields with the same name "name" are specified in the form, with values "Zara", "Huma", and "Nuha":
```ruby
#!/usr/bin/ruby
require 'cgi'
cgi = CGI.new
cgi['name'] # => "Zara"
cgi.params['name'] # => ["Zara", "Huma", "Nuha"]
cgi.keys # =>
cgi.params # => {"name"=>["Zara", "Huma", "Nuha"]}
**Note:** Ruby automatically determines the GET and POST methods, so there is no need to treat them differently.
Here is the related HTML code:
First Name :
Last Name : * * * ## Creating Forms and HTML The CGI library includes many methods for creating HTML, with a corresponding method for each HTML tag. Before using these methods, you must create a CGI object via `CGI.new`. To make tag nesting simpler, these methods take content as a code block, which returns a string as the tag's content. As shown below: ```ruby #!/usr/bin/ruby require "cgi" cgi = CGI.new("html4") cgi.out{ cgi.html{ cgi.head{ "n"+cgi.title{"This Is a Test"} } + cgi.body{ "n"+ cgi.form{ "n"+ cgi.hr + cgi.h1 { "A Form: " } + "n"+ cgi.textarea("get_text") + "n"+ cgi.br + cgi.submit } } } } * * * ## String Escaping When processing parameters in URLs or HTML form data, you need to escape specific special characters, such as quotes (`"`) and backslashes (``). The Ruby CGI object provides `CGI.escape` and `CGI.unescape` methods to handle the escaping of these special characters: ```ruby #!/usr/bin/ruby require 'cgi' puts CGI.escape('Zara Ali/A Sweet & Sour Girl') The result of the above code is: Zara+Ali%2FA+Sweet+%26+Sour+Girl Another example: ```ruby #!/usr/bin/ruby require 'cgi' puts CGI.escapeHTML('
Last Name : * * * ## Creating Forms and HTML The CGI library includes many methods for creating HTML, with a corresponding method for each HTML tag. Before using these methods, you must create a CGI object via `CGI.new`. To make tag nesting simpler, these methods take content as a code block, which returns a string as the tag's content. As shown below: ```ruby #!/usr/bin/ruby require "cgi" cgi = CGI.new("html4") cgi.out{ cgi.html{ cgi.head{ "n"+cgi.title{"This Is a Test"} } + cgi.body{ "n"+ cgi.form{ "n"+ cgi.hr + cgi.h1 { "A Form: " } + "n"+ cgi.textarea("get_text") + "n"+ cgi.br + cgi.submit } } } } * * * ## String Escaping When processing parameters in URLs or HTML form data, you need to escape specific special characters, such as quotes (`"`) and backslashes (``). The Ruby CGI object provides `CGI.escape` and `CGI.unescape` methods to handle the escaping of these special characters: ```ruby #!/usr/bin/ruby require 'cgi' puts CGI.escape('Zara Ali/A Sweet & Sour Girl') The result of the above code is: Zara+Ali%2FA+Sweet+%26+Sour+Girl Another example: ```ruby #!/usr/bin/ruby require 'cgi' puts CGI.escapeHTML('
YouTip