Sending form data to a text file

Discussion in 'Ruby' started by Will H., Jan 20, 2010.

  1. Will H.

    Will H. Guest

    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/.
    Will H., Jan 20, 2010
    #1
    1. Advertising

  2. Will H.

    Will H. Guest

    #!/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/.
    Will H., Jan 21, 2010
    #2
    1. Advertising

  3. Will H.

    Jeff Peng Guest

    在 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.
    Jeff Peng, Jan 21, 2010
    #3
  4. Will H.

    Will H. Guest

    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

    22
    Male
    2010-01-21 12:55:47 +0000
    --
    Posted via http://www.ruby-forum.com/.
    Will H., Jan 21, 2010
    #4
  5. Will H.

    Will H. Guest

    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/.
    Will H., Jan 21, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVzdGlu?=

    Problem Sending email form web form

    =?Utf-8?B?SnVzdGlu?=, Feb 21, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,493
    =?Utf-8?B?U2hhdW4=?=
    Feb 21, 2005
  2. Simon Rigby
    Replies:
    5
    Views:
    747
    Cowboy \(Gregory A. Beamer\)
    Oct 7, 2006
  3. Bjorn Sagbakken

    Pop-up form sending data to the calling form?

    Bjorn Sagbakken, Jun 30, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    498
    Bjorn Sagbakken
    Jun 30, 2007
  4. Jason Pfeifer
    Replies:
    1
    Views:
    123
    Logan Capaldo
    Jan 23, 2006
  5. Ferrous Cranus
    Replies:
    42
    Views:
    473
    Ferrous Cranus
    Sep 6, 2013
Loading...

Share This Page