Forum: Ruby using buttons in CGI

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6c63d3e026a564fac6ca85ae792b4444?d=identicon&s=25 Asaf Asaf (asafdasa)
on 2008-10-23 09:44
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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-23 11:08
Asaf Asaf 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):

<form action="foo.cgi">
  <input type="submit" name="submit" value="new row">
  <br />
  <input type="submit" name="submit" value="submit">
</form>

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, 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#s...

www.w3.org is an excellent resource for anything about HTML, XML, CSS
etc.
6c63d3e026a564fac6ca85ae792b4444?d=identicon&s=25 Asaf Asaf (asafdasa)
on 2008-10-23 11:44
Brian Candler wrote:
> Asaf Asaf 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):
>
> <form action="foo.cgi">
>   <input type="submit" name="submit" value="new row">
>   <br />
>   <input type="submit" name="submit" value="submit">
> </form>
>
> 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, 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#s...
>
> 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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-23 12:02
Asaf Asaf 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 <input type="submit" name="submit" value="foo"> 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.
6c63d3e026a564fac6ca85ae792b4444?d=identicon&s=25 Asaf Asaf (asafdasa)
on 2008-10-23 12:10
Brian Candler 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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-23 12:26
Asaf Asaf 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 <input type="submit">, 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
This topic is locked and can not be replied to.