http://qs1969.pair.com?node_id=1142925

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

Greetings, fellow monks
To help the interns at my town hospital set up their shift calendars I wrote an openoffice base macro (in OObasic, bleh) after some time I rewrote it in java to make it portable, then when I thought about the hassle of having untrained user open a terminal and type "java -jar mything --xls input.xls" I decided to set it up as a web application.

There started a painful trip, unknowing of frameworks like sails I wrote it in nodejs (even though I hate javascript with a passion) and cobbled together a rag tag session system to prevent users from having to register themselves.
After falling in love with perl I decided to get rid of that crap and rewrite it in perl using Catalyst.

After some fiddling around I managed to setup a session system using cookies so the server can keep track of data added by users during their sessions, problem is I am now coding the ability to delete any data they have entered and I hit quite a roadblock : how do I do that?
Data is submitted to the server in the url using a GET request and the following scheme :

http://127.0.0.1:3000/serviceform?nom_service=service&nb_jours_repos=1

This is a get request to record data, now to delete it I whipped up some javascript code to make users able to point and click at whatever data they want to delete (data is displayed that way in the template : )

[% FOREACH key IN services.keys -%] <li onclick="select(this)">service : [%key%] jours de re +pos: [%services.$key.nb_jours%] interieur : [%services.$key.interieur%]</li> [% END -%]

Because I save everything in a hash of hashs inside my session.

here is the form used to enter data that is sent to the server and displayed on the webpage :

<form> <label for="ns">nom du service </label> <input type="text" name="nom_service" id="ns" value="s +ervice"/> <label for="nbjdr">nombre de jours de repos</label> <input type="number" id="nbjdr" name="nb_jours_repos" +value=1 /> <label for="int">service d'intérieur?</label> <input type="checkbox" id="int" name="interieur" /> <input type="submit" value="ajouter"/> <input type="button" class="remove" onclick="remove()" + value="supprimer"/> </form>

The remove function called is a javascript one to delete the li nodes with the selected class. I can set any of those list item class to selected but what should happen when I press the delete button, I need to delete those entries on the server as well, not just in the client browser. I think some kind of request should be in order but I haven't the foggiest about how it should be handled, maybe a special case of submit with a hidden delete boolean set to true..? If anyone cares to give me a hint about the best way to do it it would be most welcome.

Replies are listed 'Best First'.
Re: Catalyst client to server interaction
by tangent (Parson) on Sep 24, 2015 at 15:45 UTC
    Have I got this - when a user clicks on one of the list items a JS function is called to select that item, and if the user then clicks on the Delete button you call another JS function to delete the list item from the DOM, but you also need to actually delete the item on the server?

    If correct, then what I would do is, instead of deleting the list item I would hide it or mark it in some way, and also give it a class - say 'for_delete'. Then when the delete button is clicked I would use JS to scan for any list items with the class 'for_delete', collect their ids and then send a request to the server either via an AJAX call or by appending the ids to the form and then submitting the form.

    You could re-write your template something like:

    [% FOREACH key IN services.keys -%] <li onclick="select(this)" id="[% key %]" class="">service : [% ke +y %] jours de repos: [%services.$key.nb_jours%] interieur : [%service +s.$key.interieur%]</li> [% END -%]
    You will also need to give your form a name or an id so you can select it and append the ids.

      Brilliant!
      many thanks to you sir, or madam, you just saw right through the veils of confusion my own lack of understanding set up around my problem!
      This is the solution I was looking for and I shall set forth on implementing it at once.

        And, wrap the action in an SQL Transaction. (In MySQL, use "InnoDB" tables so that you have transactions.) This way, all deletes will either all occur successfully or none at all.