Contact US

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.

Students Click Here

Ruby CGI form, how to catch SUBMIT

Ruby CGI form, how to catch SUBMIT

Ruby CGI form, how to catch SUBMIT

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.




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

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

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

puts <<HTML
<!DOCTYPE html>
<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}

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

<br>  #{msg1}
<br>  #{msg2}

RE: Ruby CGI form, how to catch SUBMIT


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 )


require 'cgi'

cgi = CGI.new

if cgi['email'].empty?
   msg1 = "Please enter info correctly"
   msg2 = ""
   msg1 = "Welcome username"
   msg2 = "Can we send email to you at:  #{cgi['email']}"
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.


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! Already a Member? Login

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