INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Ruby CGI form, how to catch SUBMIT

Ruby CGI form, how to catch SUBMIT

(OP)
Hi,
I'm trying to catch Submit button event on my ruby, CGI script, all theory says that I should have name="mybutton" for this , but in my case I have form like below, without any Name parameter.
I also don't see the <name> on my form, should it be there ? How I can refer this entered fields on this form in my script?


How I can catch if Submit button has been pressed?  Or value for user_name or email being entered? I put some dummy check on the top of script for now, so far no matter what I tried nothing helped.

Thanks
Dai

CODE

#!/usr/local/bin/ruby

email="vvvv@ryba.com"   ### dummy for test

if 1 > 2   #cgi.params['email'].emptyi?   ###???????
   msg1 = "Please enter info correctly"
   msg2 = ""
else
   msg1 = "Welcome username"
   msg2 = "Can we send email to you at:  #{email}"
end


puts "Content-type: text/html"
puts "Server: Apache/1.3.3.7 (Unix)  (Red-Hat/Linux) # Server information "
puts

puts <<HTML
<!DOCTYPE html>
<html>
<head>
<title>A First Ruby CGI Script</title>
<style type="text/css">
        body {background-color:#111;color:#aff;font-family:Helvetica,Arial,Verdana,sans-serif;font-size:12px;}
        h1 {color:#aaa}
</style>
</head>

<body>
<h1>This is a form!</h1>
<br> <br>
<form action="" method="post" accept-charset="utf-8">
<p>
<label for="email" style="width:100px;float:left">Email</label>
<input type="text" name="email" value="" id="email">
</p>
<p>
<input type="submit" value="Continue &rarr;">
</p>
</form>


<br>
<br>  #{msg1}
<br>  #{msg2}
<br>
</body>
</html>
HTML

RE: Ruby CGI form, how to catch SUBMIT

Hi

The biggest problems :
  • The CGI module will not be required automagically. require it.
  • The cgi object will not be instantiated automagically. Instantiate it.
  • When the form is submitted, cgi.params['email'] will be an array with one element, so testing it with empty? will always be false. Either test each element of cgi.params['email'] or if you are sure there will always be a single email field, use [] instead.

CODE --> Ruby ( fragment )

#!/usr/local/bin/ruby

require 'cgi'

cgi = CGI.new

if cgi['email'].empty?
   msg1 = "Please enter info correctly"
   msg2 = ""
else
   msg1 = "Welcome username"
   msg2 = "Can we send email to you at:  #{cgi['email']}"
end
Regarding the submit button, actually that applies to any form element : if it has no name attribute, will not be included in the submitted data. Note that depending on the browser and the way of activating the submission, the submit button may still be skipped, even if it has name attribute.
 

Feherke.
http://feherke.github.com/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close