Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ruby CGI form, how to catch SUBMIT

Status
Not open for further replies.

dainova

Programmer
Jul 2, 2002
14
US
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
 
Hi

The biggest problems :
[ul]
[li]The CGI module will not be [tt]require[/tt]d automagically. [highlight #fcc][tt]require[/tt] it[/highlight].[/li]
[li]The cgi object will not be instantiated automagically. [highlight #cfc]Instantiate it[/highlight].[/li]
[li]When the form is submitted, cgi.params['email'] will be an array with one element, so testing it with [tt]empty?[/tt] 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, [highlight #ccf]use [tt][][/tt] instead[/highlight].[/li]
[/ul]
Code:
[gray]#!/usr/local/bin/ruby[/gray]

[highlight #fcc][red]require[/red] [green][i]'cgi'[/i][/green][/highlight]

[highlight #cfc]cgi [teal]=[/teal] CGI[teal].[/teal]new[/highlight]

[b]if[/b] [highlight #ccf]cgi[teal][[/teal][green][i]'email'[/i][/green][teal]].[/teal]empty?[/highlight]
   msg1 [teal]=[/teal] [green][i]"Please enter info correctly"[/i][/green]
   msg2 [teal]=[/teal] [green][i]""[/i][/green]
[b]else[/b]
   msg1 [teal]=[/teal] [green][i]"Welcome username"[/i][/green]
   msg2 [teal]=[/teal] [green][i]"Can we send email to you at:  #{cgi['email']}"[/i][/green]
[b]end[/b]
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top