Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Ajax to refresh array

by packetstormer (Monk)
on Mar 24, 2011 at 10:17 UTC ( [id://895215]=perlquestion: print w/replies, xml ) Need Help??

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

Looking for some pointers really. I have a web page that will post information. I would like to show the user a list of their posted information. That is, everytime they hit the "Submit" button, the text they entered gets added to a list below, building a list for them.

I can do this by writing out to file then reading it back but its messy in more ways that one.

I have been looking at CGI::Ajax which looks promising but I can't seem to figure out a way to do what I need which is basically: Create an array for the session then add to it and show contents until session is over (I am not using any session tokens at all I would like not to)

Any pointers or suggestions?

Replies are listed 'Best First'.
Re: Ajax to refresh array
by GrandFather (Saint) on Mar 24, 2011 at 10:24 UTC

    Why not use a little JavaScript client side to populate the list using an "Add" button then send the list to the server when they click "Submit"?

    True laziness is hard work
Re: Ajax to refresh array
by dsheroh (Monsignor) on Mar 24, 2011 at 10:25 UTC
    You'll need to store the array-in-progress somewhere (session, database, hidden DOM elements in your AJAX replies...) because each AJAX transaction is an independent HTTP request, meaning that your environment does not automatically carry over from one to the next.

    The basic structure of your AJAX handler, then, will be something along the lines of:

    retrieve previous set of values add new user-submitted value to the set store new set of values send an HTML snippet back for CGI::Ajax to insert into the page
    If you show some of your current code, we can provide more detailed advice, but perhaps that brief outline will be enough to get you going.
Re: Ajax to refresh array
by dHarry (Abbot) on Mar 24, 2011 at 10:29 UTC

    You want to keep state for the session, a logical choice would be CGI::State. There are many ways to keep state (cookies, hidden fields etc.) all have their (dis)advantages. You can also Ajax for keeping state though I'm not sure that is the way to go for you. Where do you want to keep state on the client or on the server?

    Cheers

    Harry

Re: Ajax to refresh array
by sundialsvc4 (Abbot) on Mar 24, 2011 at 15:37 UTC

    My field-experience with helper modules like these has been mixed.   They sound helpful, and I am quite sure that they are, in appropriate circumstances, but I consistently find that, if I start out with one of them, I do not finish with any of them.   (Let me stress that this is meant as no slight whatsoever either against these packages, nor against their authors!)

    Usually, I find that I must knuckle-down and write JavaScript code, using some existing framework or another (Prototype, ExtJS, pick-one...) to do the necessary browser-specific heavy lifting on the client side ... which is now the side where everything that the end-user sees now takes place.   The user is no longer talking to “your Perl program,” running on a server someplace.   Instead, the user is talking to a JavaScript program running on his or her browser, and it is talking to your Perl program.   (Naturally, a CPAN module designed for use with the client-side framework that I have selected is used to decode the request and to encode the response.)   The unfortunate consequence of this, is that I am now writing and maintaining two programs, but c’est la guerre.

    Again to my way of thinking, the client is not “telling the server to call this-or-that Perl module.”   The client is “making a request,” and really doesn’t know or care how the server carries out that request.   This is the approach, and the mind-set, that works best for me.

    Session-cookies are, of course, fundamental to all HTTP protocols, so every incoming AJAX request will be accompanied by such a cookie (or, equivalently, by an authentication string which your client is obliged to keep and to send with every request).   Your server-side code has to validate this string with every request, and then use it to retrieve session-state data from the server-side store that you have made for that purpose.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-19 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found