Newb requires help with javascript submit

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • javascript_noob12

    #1

    Newb requires help with javascript submit

    Hi.

    I am trying to create a CGI script that will use Perl to read a text file to
    get a list of values, then turn that list into a menu on a website. The menu
    will be a form with a JavaScript "submit" function. . When someone selects a
    menu-item, the form will submit the value of that list-entry to another CGI
    page.

    The problem I'm encountering is that the script I've written submits every
    value of the form rather than the one specific menu item's value. This means
    that, on the receiving CGI page, I'm unable to tell which one value has been
    selected because I've received them all.

    As near as I can tell, the flaw in my scripting is in the JS line (but just
    in case I'm wrong, I've CC'd this to PERL.MISC).

    I am very new to JS and am having a very difficult time comprehending it.
    Before I display the code I've written, I'd like to also ask for
    recommendations for good beginner-guide-books for JS. Is O'Reilly's "Rhino"
    book a good starting place?

    Regards and thanks.

    Here's the relevant code:


    print "<FORM ACTION='/cgi-bin/sub_cat_view.cg i' NAME='myform' METHOD=
    'get' target='splash' >";
    open (CATDAT, "<categories.da t");

    while (<CATDAT>){
    @categories = split /#/, "$_";
    $cat_length = @categories;
    $loop=0;
    print "<H4>";

    while ($loop < $cat_length){
    print "<A
    HREF='javascrip t:document.myfo rm.submit()'>$c ategories
    [$loop]</A><BR>";
    print "<INPUT TYPE='hidden' NAME='$categori es[$loop]'
    VALUE='cat
    egory'>";
    $loop++;
    }
    }
    close (CATDAT);
    print "</FORM>";


  • Lee

    #2
    Re: Newb requires help with javascript submit

    javascript_noob 12 said:[color=blue]
    >
    >Hi.
    >
    >I am trying to create a CGI script that will use Perl to read a text file to
    >get a list of values, then turn that list into a menu on a website. The menu
    >will be a form with a JavaScript "submit" function. . When someone selects a
    >menu-item, the form will submit the value of that list-entry to another CGI
    >page.
    >
    >The problem I'm encountering is that the script I've written submits every
    >value of the form rather than the one specific menu item's value. This means
    >that, on the receiving CGI page, I'm unable to tell which one value has been
    >selected because I've received them all.
    >
    >As near as I can tell, the flaw in my scripting is in the JS line (but just
    >in case I'm wrong, I've CC'd this to PERL.MISC).[/color]

    First, you need to learn HTML.
    There is no way in HTML to submit only specific elements of a form.

    You shouldn't really post CGI code to a Javascript group.
    It's better to post a sample of HTML that demonstrates the problem.

    One thing that you do need to know about Javascript is that it's
    a bad idea to submit a form via the javascript: pseudo-protocol in
    the HREF attribute of a link. If there's a reason to use links
    instead of buttons, use the onclick handler, not the HREF attribute.

    It seems like what you really want is a form with a single hidden
    element whose value is set by Javascript before the form is submitted
    when you click the link.

    Comment

    • Scott Bryce

      #3
      Re: Newb requires help with javascript submit

      javascript_noob 12 wrote:
      [color=blue]
      > As near as I can tell, the flaw in my scripting is in the JS line[/color]

      You are probably right. But the code you posted is Perl.

      What you should do, if you want an answer to your question, is post the
      HTML/JavaScript that the Perl script produces to comp.lang.javas cript.
      Make sure you follow whatever posting guidelines comp.lang.javas cript has.

      Comment

      • Joe Smith

        #4
        Re: Newb requires help with javascript submit

        javascript_noob 12 wrote:
        [color=blue]
        > The problem I'm encountering is that the script I've written submits every
        > value of the form rather than the one specific menu item's value.[/color]

        That's the way a submit button works.
        You could modify your perl program to output many "<FORM ACTION=" lines
        instead of just one.
        -Joe

        Comment

        • Jeff 'japhy' Pinyan

          #5
          Re: Newb requires help with javascript submit

          [posted & mailed]

          On Mon, 6 Sep 2004, javascript_noob 12 wrote:
          [color=blue]
          >The problem I'm encountering is that the script I've written submits
          >every value of the form rather than the one specific menu item's value.
          >This means that, on the receiving CGI page, I'm unable to tell which one
          >value has been selected because I've received them all.[/color]
          [color=blue]
          > while ($loop < $cat_length){
          > print "<A
          >HREF='javascri pt:document.myf orm.submit()'>$ categories
          >[$loop]</A><BR>";
          > print "<INPUT TYPE='hidden' NAME='$categori es[$loop]'
          >VALUE='cat
          >egory'>";
          > $loop++;
          > }[/color]

          As a Perl note, I'd suggest rewriting that as a for statement:

          for my $cat (@categories) {
          print qq{
          <a href="javascrip t:document.myfo rm.submit()">$c at</a><br>
          <input type="hidden" name="$cat" value="category ">
          };
          }

          Your probably is half JS, half HTML. A form will submit *all* the data in
          it. You either need to make a separate form for EACH link (which I do NOT
          suggest), or else come up with a new mechanism for submitting the
          appropriate data.

          I don't understand why you make the *name* of the field to be passed
          variable, and the value constant. Here's how I'd do it:

          print qq{
          <form method="..." action="somethi ng.cgi" name="myform">
          <input type="hidden" name="cat" value="">
          };

          for my $cat (@categories) {
          print qq{
          <a href="javascrip t:document.myfo rm.cat.value='$ cat';
          document.myform .submit()">$cat </a><br>
          };
          }

          Now, when you click on a link, it sets the value of the 'cat' hidden
          field, and then submits the form.

          I'm not sure why you *need* a form, when you can just make links like

          <a href="something .cgi?cat=$cat"> $cat</a>

          but maybe there's more you're not telling us.

          --
          Jeff "japhy" Pinyan % How can we ever be the sold short or
          RPI Acacia Brother #734 % the cheated, we who for every service
          Senior Dean, Fall 2004 % have long ago been overpaid?
          RPI Corporation Secretary %
          http://japhy.perlmonk.org/ % -- Meister Eckhart


          Comment

          Working...