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

I use CGI to maintain a simple database at the server, but would like to provide sorting functions at the client side. For example, if the user did a search on the database and obtained a list of items, he/she can sort the result at the client side without sending the request back to the server. Is there a common solution for this problem? Should I send back the information as Javascript arrays or should I display them as HTML Form values? Any guide line for communication between CGI and javascript? Thanks, Molly

Replies are listed 'Best First'.
Re: Talk with Javascript
by merlyn (Sage) on Sep 06, 2000 at 21:45 UTC
Re: Talk with Javascript
by cwest (Friar) on Sep 06, 2000 at 21:02 UTC
    I don't think you really want communication, but, if you did check out WDDX. I think you'll find it at http://www.wddx.org.

    But for this case, I think WDDX could be over kill, try sending info back as javascript arrays.

    Follow up on what you chose, especially if it was WDDX, I'm curious about how you might use it.

    --
    Casey
    
(crazyinsomniac) Re: Talk with Javascript
by crazyinsomniac (Prior) on Sep 06, 2000 at 21:44 UTC
    Should be pretty simple,

    All you need to do is print

    <script language=javascript> <!--// var myaarray = new Array("item0","item1"); var myarray[0] = "f00f00f0"; function myClientSideSortFunction(foo) { //sortstuffnow //output result of sort to form field } //--> </script>
    with the rest of your generated file

    I suggest you use arrays, because forms can be changed by a user, unless you make them read only with javascript, or hidden.

    Another reason to use arrays, it takes less characters than form fields, plus less code.

     

    What kind of sort do you want to do?

    for javascript help see http://www.webreference.com/js/

     

    "cRaZy is co01, but sometimes cRaZy is cRaZy".
                                                          - crazyinsomniac
      Thanks for the help! But is it possible to do something like this: print "Content-type: text/html\n\n"; print "<html><head><title>Matches</title>\n"; print "<SCRIPT src=../sort.js>\n"; print "</script></head>\n"; print "<body onload=init(this.form)>\n"; In other words, can CGI send back a HTML page that includes a javascript source file? I don't want to display the javascript functions and variables directly in the CGI program. Thanks, Molly
        That's easy to do. Just open up your JavaScript source file, and do a nice tight loop like so:
        open (INPUT, $filename) or warn "Can't open $filename: $!"; print while (<INPUT>); close INPUT;
        You'll have to put that after the headers and in the appropriate spot of your HTML, but it's not tricky.

        I think you can also refer to an external JavaScript source file within the HTML, but maybe I'm confusing it with CSS. That's probably even easier.

        Somehow my sample code is not displayed correctly. Here is it again: print "Content-type: text/html\n\n"; print "<html><head><title>Matches</title>\n"; print "<script src=../sort.js>"; print "</script></head>\n"; print "<body onload=init(this.form)>\n"; In other words, can CGI send back a HTML page that includes a javascript source file? I don't want to display the javascript functions and variables directly in the CGI program. Thanks, Molly
Re: Talk with Javascript
by jreades (Friar) on Sep 07, 2000 at 02:19 UTC

    One issue not addressed yet is compatability -- do you need JavaScript 1.0 compatability or is 1.2 acceptible?

    This is an important issue because it significantly affects what you're able to do on the client side -- 1.2 added primitive regexp support (O'Reilly JavaScript guide, starting page 165) as well as more sophisticated array handling (O'Reilly JavaScript guid page 153) which would make your life much, much easier.

    Generally speaking, JavaScript 1.0 support corresponds to 3.x browsers (Netscape, IE) and 1.2 was available in the 4.x browsers. Things get trickier when you look at other browsers -- AOL is running IE's engine, but the versions are off so AOL 3 had something like IE 2-level support. Similar issues arise for Opera, iCab, and so on. The key point is, you need to know your audience in order to know what you have to play with.

    If you need 1.0 compatability you're going to have to do much more work on the server side unless you want to get into some pretty heavy JavaScript coding (which can bring you to new levels of frustration).

    Hope this helps.

Re: Talk with Javascript
by Fastolfe (Vicar) on Sep 06, 2000 at 22:18 UTC
    For some non-Javascript alternatives, you can try looking into XML and XSL in conjunction with Internet Explorer 5 or some ActiveX or Java applets. XML and XSL were made to do stuff like this. You would basically send your data in XML form to the browser/applet and use XSLT or equivalent to format the data how the user wants it to appear. This would include sorting. IE5 is capable of doing all of this natively, and this type of functionality is pretty simple to code, and, indeed, is generally "textbook" XSLT, so you shouldn't want for examples.

    I don't know how much this helps you, since you seem to be wanting to do this in Perl somehow (or else you wouldn't be here), but I wanted to throw the alternative out there.

      Have you ever tried to have XSL put out JavaScript? It puts a lot of work into validating that you are putting out valid XML which just happens to be a problem if you really want to pump out a little JavaScript as well...

      Not to mention that IE (note Netscape does not support it) has a number of bugs involving XML manipulations. Joy.

        You don't necessarily have to spit out the entire page in XML, just the data that you want rendered. Use an <object> tag or something to embed the data in your document, or use an ActiveX control or Java applet to do the XML rendering, and have it request the data in XML form on its own.

        Anyways, I was just offering that as a possible alternative for anyone else that's looking at doing something similar. It may not be useful in this case but it might be for other potentials.