# Using buttons in CGI

**URL:** <https://www.ruby-forum.com/t/using-buttons-in-cgi/151591>\
**Category:** Ruby\
**Created:** [October 23, 2008, 9:44am UTC](https://www.ruby-forum.com/t/using-buttons-in-cgi/151591 "2008-10-23T09:44:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![asafdasa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@asafdasa](https://www.ruby-forum.com/u/asafdasa)\
**Post date:** [October 23, 2008, 9:44am UTC](https://www.ruby-forum.com/t/using-buttons-in-cgi/151591/1 "2008-10-23T09:44:10Z")

</div>

Hi all,

I have a problem, I have table such that each line on it is textfield  
(something that I defined). I want to make a button, which isn’t the  
‘submit’ or ‘reset’ button, such that when I click on it, a new line on  
this table will be made.  
After big number of attempts I succeed making two forms, such that on  
one form I have ‘submit’ button, that clicking on it update the page  
with a new line, my problem is : if someone write something on the table  
(on one of the text-fields) then clicking on this button won’t save the  
data that he entered. and because the ‘submit’ button that responsable  
to the new line isn’t in the form of the table, I can’t get the data of  
the table using cgi.params[…] after the refreshing of the page with  
the new line.

if anyone have any solution to my problem, or if someone knows how to  
continue my solution such that it will finally work, I will be  
thankfull.

p.s : any helpfull data about the using of buttons in CGI will be great  
too…

Asaf

---

<div class="post-metadata">

**Author:** ![asafdasa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@asafdasa](https://www.ruby-forum.com/u/asafdasa)\
**Post date:** [October 23, 2008, 11:08am UTC](https://www.ruby-forum.com/t/using-buttons-in-cgi/151591/2 "2008-10-23T11:08:49Z")

</div>

Asaf A. wrote:

> I have a problem, I have table such that each line on it is textfield  
> (something that I defined). I want to make a button, which isn’t the  
> ‘submit’ or ‘reset’ button, such that when I click on it, a new line on  
> this table will be made.  
> After big number of attempts I succeed making two forms, such that on  
> one form I have ‘submit’ button, that clicking on it update the page  
> with a new line, my problem is : if someone write something on the table  
> (on one of the text-fields) then clicking on this button won’t save the  
> data that he entered. and because the ‘submit’ button that responsable  
> to the new line isn’t in the form of the table, I can’t get the data of  
> the table using cgi.params[…] after the refreshing of the page with  
> the new line.

You just need another submit button within the original form, something  
like this (untested):

  

Then in your CGI you check the value of the “submit” parameter to see  
whether it was “new row”, in which case you perform the alternate action  
of re-rendering the form with an additional row.

For more info go to [www.w3.org](http://www.w3.org), click on “HTML” in the left-hand side,  
then under “What” click “HTML 4.01”, then click on “Forms” (17) in the  
quick table of contents.

This will let you navigate to:  
[http://www.w3.org/TR/html401/interact/forms.html#submit-button](http://www.w3.org/TR/html401/interact/forms.html#submit-button)

[www.w3.org](http://www.w3.org) is an excellent resource for anything about HTML, XML, CSS  
etc.

---

<div class="post-metadata">

**Author:** ![asafdasa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@asafdasa](https://www.ruby-forum.com/u/asafdasa)\
**Post date:** [October 23, 2008, 11:44am UTC](https://www.ruby-forum.com/t/using-buttons-in-cgi/151591/3 "2008-10-23T11:44:03Z")

</div>

Brian C. wrote:

> Asaf A. wrote:
> 
> > I have a problem, I have table such that each line on it is textfield  
> > (something that I defined). I want to make a button, which isn’t the  
> > ‘submit’ or ‘reset’ button, such that when I click on it, a new line on  
> > this table will be made.  
> > After big number of attempts I succeed making two forms, such that on  
> > one form I have ‘submit’ button, that clicking on it update the page  
> > with a new line, my problem is : if someone write something on the table  
> > (on one of the text-fields) then clicking on this button won’t save the  
> > data that he entered. and because the ‘submit’ button that responsable  
> > to the new line isn’t in the form of the table, I can’t get the data of  
> > the table using cgi.params[…] after the refreshing of the page with  
> > the new line.
> 
> You just need another submit button within the original form, something  
> like this (untested):
> 
>   
> 
> Then in your CGI you check the value of the “submit” parameter to see  
> whether it was “new row”, in which case you perform the alternate action  
> of re-rendering the form with an additional row.
> 
> For more info go to [www.w3.org](http://www.w3.org), click on “HTML” in the left-hand side,  
> then under “What” click “HTML 4.01”, then click on “Forms” (17) in the  
> quick table of contents.
> 
> This will let you navigate to:  
> [http://www.w3.org/TR/html401/interact/forms.html#submit-button](http://www.w3.org/TR/html401/interact/forms.html#submit-button)
> 
> [www.w3.org](http://www.w3.org) is an excellent resource for anything about HTML, XML, CSS  
> etc.

Hi Brian,

as I see the value of the submit button doesn’t pass to the next form, I  
see that when I using HTML page and pressing on the ‘submit button’.  
so, I understand that I should check it not by using cgi.params[…][],  
but by other command.  
can you tell me if what I am say is right ? and if so can you tell me  
hoe to check the value of the submit button.

another question :  
if I want that one of the submit buttons will update the current form  
with the adittional line, and the other submit button will update other  
form, how can I do it using only one form and two submit buttons ?

I appriciate your quick answer,  
Thanks,  
Asaf

---

<div class="post-metadata">

**Author:** ![asafdasa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@asafdasa](https://www.ruby-forum.com/u/asafdasa)\
**Post date:** [October 23, 2008, 12:10pm UTC](https://www.ruby-forum.com/t/using-buttons-in-cgi/151591/4 "2008-10-23T12:10:36Z")

</div>

Brian C. wrote:

> In both cases, the data is POSTed back to your CGI. It is up to you to  
> take the appropriate action depending on which submit button was  
> pressed. In one case you will re-display the data entry form, containing  
> the already-entered data plus a new blank row. In the second case you  
> perform whatever action you were normally taking on the submit.

I read on the ‘programming Ruby’ that the submit button will update the  
name of the fprm that appear on the form parameters :  
for ex. @cgi.form(“get”,“abcdc.rb”)  
will update the abcdc.rb form and will show this form, with no option  
for me to change it.

so can you please explain to me how can I get the data back and how can  
I handle it.

thanks again,  
Asaf

---

<div class="post-metadata">

**Author:** ![asafdasa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@asafdasa](https://www.ruby-forum.com/u/asafdasa)\
**Post date:** [October 23, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/using-buttons-in-cgi/151591/5 "2008-10-23T12:02:19Z")

</div>

Asaf A. wrote:

> as I see the value of the submit button doesn’t pass to the next form

I think it does, so please post your code and output which shows  
otherwise.

Especially show the value of cgi.params[‘submit’].inspect

This assumes you have in  
your HTML.

> another question :  
> if I want that one of the submit buttons will update the current form  
> with the adittional line, and the other submit button will update other  
> form, how can I do it using only one form and two submit buttons ?

In both cases, the data is POSTed back to your CGI. It is up to you to  
take the appropriate action depending on which submit button was  
pressed. In one case you will re-display the data entry form, containing  
the already-entered data plus a new blank row. In the second case you  
perform whatever action you were normally taking on the submit.

---

<div class="post-metadata">

**Author:** ![asafdasa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@asafdasa](https://www.ruby-forum.com/u/asafdasa)\
**Post date:** [October 23, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/using-buttons-in-cgi/151591/6 "2008-10-23T12:26:42Z")

</div>

Asaf A. wrote:

> I read on the ‘programming Ruby’ that the submit button will update the  
> name of the fprm that appear on the form parameters :  
> for ex. @cgi.form(“get”,“abcdc.rb”)  
> will update the abcdc.rb form and will show this form, with no option  
> for me to change it.

But first you need to look at what cgi.params contains; the  
documentation for @cgi.submit, and look at what HTML it creates; and at  
the w3 documentation for , which explicitly states  
that a form can have multiple submit buttons with different values.

I find it’s always wise not to trust my understanding of what I’ve read  
until I’ve demonstrated for myself whether it works in the way I thought  
it did.

> so can you please explain to me how can I get the data back and how can  
> I handle it.

Here is a fully-working example.

#!/usr/local/bin/ruby -w

require ‘cgi’

def show\_form(cgi, rows)  
cgi.form do  
content = “”  
# content \<\< cgi.params.inspect  
rows.each do |row|  
content \<\< cgi.text\_field(“row”, row)  
content \<\< cgi.br  
end  
content \<\< cgi.submit(“new row”, “submit”)  
content \<\< cgi.br  
content \<\< cgi.submit(“go”, “submit”)  
content  
end  
end

cgi = CGI.new(“html4”)  
rows = cgi.params[“row”]

case cgi.params[“submit”].first  
when “go”  
cgi.out do  
CGI.escapeHTML(“You submitted #{rows.inspect}”)  
end

when “new row”  
cgi.out do  
show\_form(cgi, rows + [""])  
end

else  
cgi.out do  
show\_form(cgi, rows)  
end  
end
