Sending form data to a text file

W

Will H.

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
}
 
W

Will H.

#!/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.
 
J

Jeff Peng

在 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.
 
W

Will H.

Will said:
#!/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
(e-mail address removed)
22
Male
2010-01-21 12:55:47 +0000
 
W

Will H.

Jeff said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top