Sending form data to a text file

Hi,
I am having problems trying to get input from a html form and write the
form data to a text file called visitor_log.

Here's the code I have been working on:

login.rb:
#!/usr/local/ruby/bin/ruby
require "cgi"
cgi = CGI.new("html4Tr")
cgi.out{
  cgi.html{
    cgi.head{ "\n"+cgi.title{"Formula 1 Shop - Register"} } +
    cgi.body{ "\n"+
      cgi.form("post", "file.rb"){"\n"+
        cgi.h1 { "Formula 1 Shop" } + "\n"+
        cgi.h2 { "Register" } + "\n"+
        cgi.p { "Username:" } + "\n"+
        cgi.text_field("username") +"\n"+
        cgi.p { "Password:" } + "\n"+
        cgi.password_field("password") +"\n"+
        cgi.p { "Email address:" } + "\n"+
        cgi.text_field("email") +"\n"+
        cgi.p { "Age:" } + "\n"+
        cgi.text_field("age") +"\n"+
        cgi.p { "Gender:" } + "\n"+
        cgi.radio_group("gender", "Male", "Female") +"\n"+
        cgi.br +
        cgi.br +
        cgi.submit
      }
    }
  }
}

file.rb:
#!/usr/local/ruby/bin/ruby
require 'cgi'
cgi = CGI.new

time1 = Time.new
forename = cgi.text_field("username")
password = cgi.password_field("password")
email = cgi.text_field("email")
age = cgi.text_field("age")
gender = cgi.radio_group("gender")

open('../logs/visitor_log', 'a') { |f|
  f.puts forename
  f.puts password
  f.puts email
  f.puts age
  f.puts gender
  f.puts time1.inspect
}

···

--
Posted via http://www.ruby-forum.com/.

#!/usr/local/bin/ruby

require 'cgi'
cgi = CGI.new

time1 = Time.new
forename = cgi.params['username'] = ["value"]
password = cgi.params['password'] = ["value"]
email = cgi.params['email'] = ["value"]
age = cgi.params['age'] = ["value"]
gender = cgi.params['gender'] = ["value"]

open('../logs/visitor_log', 'a') { |f|
  f.puts forename
  f.puts password
  f.puts email
  f.puts age
  f.puts gender
  f.puts time1.inspect
}

I have changed the variables to cgi.params, and I have looked at the
ruby documentation but I still don't see how I can get the program to
work.

···

--
Posted via http://www.ruby-forum.com/.

在 2010-01-21四的 21:17 +0900,Will H.写道:

#!/usr/local/bin/ruby

require 'cgi'
cgi = CGI.new

Hi,

Though I never wrote a ruby CGI, but it's not that hard to write one
with class CGI.
I wrote and put this script into cgi-bin directory:

#!/usr/bin/ruby
require 'cgi'

cgi = CGI.new
user = cgi['username']
pass = cgi['password']

time = Time.now.strftime("%D %T")
cgi.out("text/plain") do
  "Time: #{time}\n" +
  "User: #{user}\n" +
  "Pass: #{pass}"
end

And access it with such url like:
http://127.0.0.1/cgi-bin/test.cgi?username=test&password=testpass

It just works fine.
You also want to check if webserver has the priviledge to write to the
file you like to create. Check webserver's error_log for any error.

Ruby's CGI document:
http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/

HTH.

Will H. wrote:

#!/usr/local/bin/ruby

require 'cgi'
cgi = CGI.new

time1 = Time.new
forename = cgi.params['username'] = ["value"]
password = cgi.params['password'] = ["value"]
email = cgi.params['email'] = ["value"]
age = cgi.params['age'] = ["value"]
gender = cgi.params['gender'] = ["value"]

open('../logs/visitor_log', 'a') { |f|
  f.puts forename
  f.puts password
  f.puts email
  f.puts age
  f.puts gender
  f.puts time1.inspect
}

I have changed the variables to cgi.params, and I have looked at the
ruby documentation but I still don't see how I can get the program to
work.

If anyone has the same problem, I have got the program working
by removing = ["value"] from cgi.params

visitor_log output:
will
Password
will@example.net
22
Male
2010-01-21 12:55:47 +0000

···

--
Posted via http://www.ruby-forum.com/\.

Jeff Peng wrote:

I wrote and put this script into cgi-bin directory:

#!/usr/bin/ruby
require 'cgi'

cgi = CGI.new
user = cgi['username']
pass = cgi['password']

time = Time.now.strftime("%D %T")
cgi.out("text/plain") do
  "Time: #{time}\n" +
  "User: #{user}\n" +
  "Pass: #{pass}"
end

And access it with such url like:
http://127.0.0.1/cgi-bin/test.cgi?username=test&password=testpass

Hi Jeff,
Thanks for the code, you posted just after I solved the problem myself.

···

--
Posted via http://www.ruby-forum.com/\.