Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

CGI JS confusion

by SerZKO (Beadle)
on Aug 08, 2012 at 19:39 UTC ( [id://986347]=perlquestion: print w/replies, xml ) Need Help??

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

Hej Monks,

I was trying

my $JSCRIPT=<<END; window.location.href="http://skinnmaskin/mx/kon_add.html" END 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'), $qry->submit(-name =>'andra', -valu +e=>'Nej', onSubmit=>"$JSCRIPT"); print $qry->end_form;
But what I get is whatever I do thereafter I'm redirected no matter what button I clicked on ? I have to admit that I have no knowledge of JS whatsoever, I just found this JS code on the web.

Point is that I have just two buttons - when I click on one (with name 'Andra' and value 'Ja') I want one thing to happen and when I click on the other one (with value 'Nej') to be redirected.

Any help appreciated. Thanks in advance !

Replies are listed 'Best First'.
Re: CGI JS confusion
by Anonymous Monk on Aug 09, 2012 at 01:03 UTC

    Hi,

    Just guessing here, both buttons are submits so whichever is pressed is kicking off the Submit event.

    You could try creating the buttons as input type button and using onClick.

    Or I could be talking out of the wrong ofifice. It wouldn't be the first time.

    J.C.

      Thanks man, input type button was the solution !
Re: CGI JS confusion
by Anonymous Monk on Aug 09, 2012 at 20:24 UTC

    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.

      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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://986347]
Approved by chacham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-24 04:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found