using buttons in CGI

A

Asaf Asaf

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
 
B

Brian Candler

Asaf said:
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#submit-button

www.w3.org is an excellent resource for anything about HTML, XML, CSS
etc.
 
A

Asaf Asaf

Brian said:
Asaf said:
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#submit-button

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
 
B

Brian Candler

Asaf said:
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

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.
 
A

Asaf Asaf

Brian said:
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
 
B

Brian Candler

Asaf said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top