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

I recently took over a perl project and am not familiar with Perl. Any help would be appreciated?
I need to ask a question for confirmation ( whether they chose to delete data or not ) when clicking on a button. My understanding is that you can not achieve this with Perl. How do I allow a popup asking a question for confirmation.
Here is my code:
<%= $delete_all_skills->input( -form=>$form, -label=>'cancel', -value +=>"Delete All Skills", -javascript=>$question_javascript ) %>
my $question_javascript = 'onclick="askdata()"';
<SCRIPT LANGUAGE="JavaScript"> function askData() { doyou = confirm("Are you sure you want to delete ALL the skills? (OK = + Yes Cancel = No)"); //Your question. if (doyou == true) alert("All your skills have been deleted"); //If your question is answ +ered Yes. else if (doyou == false) alert("Your skills have not been deleted"); //If your question is answ +ered No. </SCRIPT> return( askdata ); }

edited: Wed May 14 17:10:29 by jeffa - title change (was: asking questions)

Replies are listed 'Best First'.
Re: Question about CGI and Javascript
by hardburn (Abbot) on May 13, 2003 at 17:58 UTC

    You can do it with pure Perl, but you won't be able to create a popup window like in JavaScript. Rather, you send it to an initial CGI that grabs the list of stuff to be deleted, tells the user "are you sure about this?", with a "Yes, I am sure" button at the bottom. That button gives the data to a second CGI that does the actual deletion.

    JavaScript will save you an extra server request each time. Also, you should be coding your pages in a such a way that you can still gain the benifits of JavaScript when its on in the browser, but won't stop you from using the web page if the browser has it shut off.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      1) Learn how CGI.pm works. You can find information at CPAN. If nothing else, learn how to get at the form variables using CGI.pm. Lincoln Stein has published a great book on this subject. (he wrote CGI.pm)
      2) It sounds like you already know javascript. This should give you a good start on the DOM (document object module).
      3) Use javascript to set form variables on the client, then use those values in your perl cgi script to accomplish the proper goal.

      Here is a simple bit of logic to get you started.
      For instance, if you have 2 buttons, lets say OK and Cancel, both of which ultimately submit the form. Also, lets say you have a form varriable called button_pressed. To get your CGI to know which button was pressed, have your javascript onClick() handler for each button do whatever pre-submission action it needs to take, and also, have it set the value of button_pressed accordingly. Then, in your CGI script, just query the value of the button_pressed form variable.

      Note, there are other ways to accomplish this same thing, this is just one of them.
      Thank you first. May I ask your expertise on how to do this? I recieved this project last week. How do I go about doing this. I was told that this could only be done in javascript. I'm sorry I am new to Perl, but excited to learn, especially since given the opportunity to learn.

        May I ask your expertise on how to do this?

        If you're willing to pay me for my time, sure :)

        Seriously, what I gave you above should get you started. The first CGI just needs to generate a form plus a "are you sure" message, and on confirmation, sends the list of stuff to be deleted to the second CGI, which does the actual work of deletion.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        Note: All code is untested, unless otherwise stated

Re: Question about CGI and Javascript
by benn (Vicar) on May 13, 2003 at 20:33 UTC
    I generally do 'delete' buttons like this - irrespective of what generation method one uses (CGI.pm, HERE docs etc.), the html comes out as...
    <input type='submit' name='delete_skills' value='Delete' onclick="return confirm('Are you sure?');">
    A 'No' click cancels the submission.

    As others have said though, Javascript should be a 'nice extra'. In 'friendly' apps, I generally check for JS support when logging users in - $q->submit({-name=>'login',-onclick=>"document.forms[0].got_javascript=1;"}) or similar - and store this in their session / cookie / whatever. Later, this can be checked to see if a confirmation stage is necessary.

    Of course, in 'critical' apps (or ones where 9 out of 10 pet-owners said their computers preferred anything but MSIE, and F12 is all you need to switch JS on/off), as again stated above, one should never rely on it.

    Cheers
    Ben.

Re: Question about CGI and Javascript
by Skeeve (Parson) on May 14, 2003 at 06:47 UTC
    Hi Anne-Marie!
    Tell us: how will your program be run?

    Is it on a websever with pages delivered to a webbrowser?
    - if so, JavaScript is fine but a CGI script on the server would be better because JavScript might be deactivated on the client browser. You'll have to understand the concept of CGI first to do that.

    Is it an application running on its own and you gave the JavaScript example just to make clear what you want to do because you know JavaScript?
    - if so, you might want to look at perl's modules for TCL/TK in order to get popups.

    HTH