in reply to CGI JS confusion

Even though you already found your answer, I wish to complain about your needless use of javascript.

(There are quite a many names I could call a web developer who relies too much on javascript, but my stance is, in a nutshell: everything that can be written without javascript, should be written without javascript, unless the effort needed to do that is extraordinary. And a lot of times, it is far from extraordinary. You can do a lot without JS.)

So you have a form. It looks like the image below.

# form that submits to kon.cgi Do you want to change data for $clrNr? [ Yes ] [ No ]

The "Yes" button goes to kon.cgi, and the "No" button has a javascript redirect to kon_add.html. This is not needed; nor will it work if the user has chosen to disable javascript. There are a few options to fix that.

What you could do is have two forms. One submits to kon.cgi, and the other to kon_add.html. Neither form will have data fields, but you are free to add data to one, if necessary.

# form that submits to kon.cgi Do you want to change data for $clrNr? [ Yes ] # form that submits to kon_add.html [ No ]

Of course, to the user it would look pretty much the same (well, after some fixing of the CSS at least) -- he doesn't see where which button submits to. So what about the code?

print $qry->start_form(-action => "http://skinnmaskin/cgi-bin/kon.cgi" +); print $qry->h4("Vill du \xE4ndra data f\xF6r $clrNr ?<br>"); print $qry->submit('Andra', 'Ja'); print $qry->end_form; print $qry->start_form(-action => "http://skinnmaskin/mx/kon_add.html" +); print $qry->submit(-name =>'andra', -value=>'Nej'); print $qry->end_form;

No, kon_add.html need not be a CGI script. Your web server will serve it normally.

What do we gain here? Just getting rid of the javascript dependency -- now the user can use both of the buttons even on the text-mode Lynx browser (It's a very good browser -- you ought to try it.) And so can the conservative web users who prefer browsing without javascript. We seem to be in the minority.


There is another option -- having kon.cgi redirect to the correct place. We need to give the submit buttons different names. (Well, strictly speaking, we don't, but it will save some headache in an indeterminate point of time in the future.)

print $qry->submit('Andra', 'Ja'), $qry->submit(-name =>'addera', -val +ue=>'Nej');

And the top of kon.cgi should be something like this:

if ($qry->param('addera')) { # the "no" button was clicked print $qry->redirect('http://skinnmaskin/mx/kon_add.html'); exit 0; }

Anyway, I've only learned javascript through osmosis, and wish that you chose a non-JS solution to your problem, but I suspect your problem could be fixed by adding a "return false" to your JS. This will "disable" the second submit button if the JS is run.

I hope this helps.

Replies are listed 'Best First'.
Re^2: CGI JS confusion
by SerZKO (Beadle) on Aug 09, 2012 at 20:46 UTC

    Hej Anonymous monk and thank for your reply,

    I have to say that I've tried your other option with redirect some days ago, but it didn't work as I send header earlier then check is done and it seems not to like it.

    But I think your first option is simply ingenious !! and I'll try that first thing in the morning. I didn't have a clue that action can be other then perl/cgi (couldn't find it anywhere in books I've read and was too afraid to try it).

    Now when it comes to JavaScript, I really would be very happy if I wouldn't need to learn it just to be able to do some actions here and there sporadically. I just don't have time for that.

    Anyhow, thanks a million for your time and help and perhaps you might wanna take a look at node How to uncheck one checkbox if some other is checked if you have some time over ?

    P.S. And just for a record - I'm not a web developer, I'm just trying to help some of my colleagues to collect some data and that's all... :-)

      I must warn you that with the two-form method the buttons won't be horizontally aligned by default. You should be able to fix this with a CSS rule of form { display: inline; }

        Oh, that's nothing. They don't need to be aligned at all :-). Besides, CSS is something I've never worked before...