kmullin5016 has asked for the wisdom of the Perl Monks concerning the following question:

I spent several days on this problem, and never fixed it. Perhaps, a wise perl monk can see my problem and explain it to me.

I have a form that I use this code for:

print $cgi->start_form(-name=>'new', -method=>"$FORM_METHOD", -action=>'secalert_display.cgi', -target=>'_top'); print $cgi->submit(-value=>'Enter a new record for a Secalert'); print $cgi->hidden(-name=>'MODE', -value=>'new'); print $cgi->endform;

It compiles cleanly, but at runtime (when I execute the CGI from a browser) and click on the 'Enter a new record for a Secalert' button, It doesn't work. The best I can figure is that it doesn NOT sent the hidden field along with the rest of the stuff.

Now, if I replace this CGI.pm code with simple print statements, as in:

print("<form name='new' method='$FORM_METHOD' action='secalert_display +.cgi' target='_top'>\n"); print("<input type='submit' value='Enter a new record for a Secalert'> +"); print("<input type='hidden' name='MODE' value='new'>\n"); print("</form>\n");

It works fine. Can somebody see what the problem with the CGI.pm version is?

Replies are listed 'Best First'.
Re: Whats wrong with this code.
by davorg (Chancellor) on Feb 20, 2007 at 16:50 UTC

    If there is a difference in the behaviour of these two pieces of code, then there must be a difference in the HTML that they produce.

    I suggest comparing the HTML produced by the two pieces of code. Any differences found will explain the difference in behaviour.

    Your next task will then be to work out how to change the non-functioning code so that it produces the same HTML as the functioning code.

Re: Whats wrong with this code.
by ikegami (Patriarch) on Feb 20, 2007 at 16:50 UTC

    Maybe you are being bitten by CGI's sticky fields. (Do you have a param named MODE?) Try

    print $cgi->hidden(-name=>'MODE', -value=>'new', -override=>1);
Re: Whats wrong with this code.
by derby (Abbot) on Feb 20, 2007 at 16:52 UTC

    The best I can figure is that it doesn NOT sent the hidden field along with the rest of the stuff.

    Why do you have to guess? Can't you view the page source? My quick run of your code shows that hidden field is there:

    <form method="post" action="secalert_display.cgi" enctype="multipart/form-data" target="_top" name="new"> <input type="submit" name=".submit" value="Enter a new record for a Secalert" /> <input type="hidden" name="MODE" value="new" /> </form>

    -derby
Re: Whats wrong with this code.
by imp (Priest) on Feb 20, 2007 at 16:58 UTC
    "It doesn't work" doesn't give us enough information to diagnose the problem.

    Does the form not submit? Then compare the html.
    Does the script receiving the form input behave incorrectly? Then we need to see that code to see what it is doing. Please provide the smallest working example that still demonstrates the problem. By reducing your code like this you will likely find the problem, and if not it will help us find the problem quickly without digging through 300 lines of code.

    The form looks ok, and the only significant difference I see is that you named the submit button '.submit' in the original code, and 'submit' in the 'simple print' version.

      Here is the HTML code generated by the use of CGI.pm, which does not work:

      <form method="post" action="secalert_display.cgi" enctype="application +/x-www-form-urlencoded" target="_top" name="new"> <input type="submit" name=".submit" value="Enter a new record for a Se +calert" /> <input type="hidden" name="MODE" value="new" /> </form>

      And here is the HTML generated by the use of the prints statements that DOES work.

      <form name='new' method='post' action='secalert_display.cgi' target='_ +top'> <input type='submit' value='Enter a new record for a Secalert'> <input type='hidden' name='MODE' value='new'> </form>
        You still didn't define what "does not work" actually means, and didn't include the code that is not handling the form input correctly.

        I know what I mean. Why don't you?

        So start with the HTML generated by CGI and then slowly modify it to be the same as the other, when it works you'll know what was breaking your receiving script. It looks like the only significant changes are the enctype and the name of the submit field so you should probably start by changing one of those. Once you know the problem it should be easy to figure out how to either fix your receiving script to handle it or change your generating script to produce the HTML you need.


        ___________
        Eric Hodges
        A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Whats wrong with this code.
by cdarke (Prior) on Feb 20, 2007 at 16:56 UTC
    I'm not CGI person, but on the simple perl side, in the call to start_form you have "$FORM_METHOD" without single quotes. In the print statement you have '$FORM_METHOD', inside double quotes, so the singles will be sent.

      so the singles will be sent.

      And? method="POST", method='POST' and method=POST are all valid in HTML.

        Fair enough, I'll get back into my box.