using buttons in CGI

Discussion in 'Ruby' started by Asaf Asaf, Oct 23, 2008.

  1. Asaf Asaf

    Asaf Asaf Guest

    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
     
    Asaf Asaf, Oct 23, 2008
    #1
    1. Advertisements

  2. 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.
     
    Brian Candler, Oct 23, 2008
    #2
    1. Advertisements

  3. Asaf Asaf

    Asaf Asaf Guest

    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
     
    Asaf Asaf, Oct 23, 2008
    #3
  4. I think it does, so please post your code and output which shows
    otherwise.

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

    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.
     
    Brian Candler, Oct 23, 2008
    #4
  5. Asaf Asaf

    Asaf Asaf Guest

    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
     
    Asaf Asaf, Oct 23, 2008
    #5
  6. 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.
    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
     
    Brian Candler, Oct 23, 2008
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.