Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

perl javascript XML

by blenkhn (Acolyte)
on Sep 21, 2005 at 19:35 UTC ( [id://493937]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I have recently come across a great little script but I am having a hard time understanding it. The script does an XML call to a script which returns the information to the web page via XML.

Can perl communicate with the Javascript on the page to create the dropdwon lists?

The list is a chained selectbox. What a person selects from one list activates a second list box and fills it with the needed Items via XML.

If you could provide me with the steps to work this I would be extremely grateful

Thanks for all your help.

blenkhn

Replies are listed 'Best First'.
Re: perl javascript XML
by jZed (Prior) on Sep 21, 2005 at 19:41 UTC
    This is called AJAX (asynchronous JavaScript and XML). I am about to release a version of DBIx::Ajax which will provide Perl support for the kind of thing you are talking about. See RFC : AJAX + DBI = DBIx::LiveGrid for a first desctription. I am expanding that to provide support for all Ajax calls, not just livegrids.
Re: perl javascript XML
by philcrow (Priest) on Sep 21, 2005 at 19:41 UTC
    You probably want to use AJAX to do this sort of client side work. Look at HTML::Prototype which we're using for this type of support. We're managing checkboxes, but list boxes should work similarly.

    Phil

Re: perl javascript XML
by ickyb0d (Monk) on Sep 21, 2005 at 21:35 UTC
    I've recently been doing a lot of JavaScript/Perl work. I've also recently gotten into JSON when using javascript. In doing so, I've found JSON.pm to be very helpful in communication between the two.
      Ok, it looks good but maybe I don't understand as much as I would like to think I do. To me the process should be rather simple.

      Webpage javascript askes server script for information to fill first drop down.

      Server Script responds with an xml document.

      Webpage Javascript parses information and displays in second Dropdown list

      Process is repeated for the third dropdown list.

      Is this what the JSON module would do for me?

      Is it possible to setup the web-page to handle multiple sets of the dropdown lists? 10 for example? (I have found a method that the other 9 would not show up until the first is filled)

      Can you provide an example of it working?

      thanks for your help

        Sorry I should've said this earlier. JSON is more like an alternative to XML. To use JSON here's an example from a perl script called from an XMLHttpRequest. That's basically your standard AJAX technique.

        This method is described here. If you were still interested in trying an XML solution you can look here. Granted that's a php solution, but you'd just print out the XML as i do in the first snipet of code with the JSON.

        all in all it's just a matter of getting a string back from your perl script. Then using the request.responseText. Then doing whatever you want to that string; whether it's parsing XML or evaluating it to create a JavaScript Object. If your giving a string to your perl script, that's when you might use the JSON module to interpret the string into a perl object. If you still wanted to go the XML route, it's pretty much the same concept, except use an XML module to create/dump perl object from/to XML, then have the JavaScript interpret it. Looks like there's plenty XML modules on CPAN. Hope this helps.

        Employee.pcgi

        in this @employees is just an array of hashes
        #header required to properly recieve text/html response print "Content-type: text/html\n\n"; print '{"employees" : [' . "\n"; foreach my $employee(@employees) { print ' { "name" : "' . $employee->{name} . '",' . "\n" . '"company" : "' . $employee->{company} . '",' . "\n" . '"email" : "' . $employee->{email} . '",' . "\n" . '"website" : "' . $employee->{website} . '"'. "\n" . '},' } print ']}';

        And here's what the javascript might look like.

        employee.js
        //loading XML (JSON) data function loadXML(url) { //creating XMLHttpRequest Object... IE Only try{ req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { req = false; } } alert('contacting ' + url); //opening url req.open("GET",url, true); //when data is recieved, goto popuplate_list() req.onreadystatechange=populate_list; //GET method, send null req.send(null); } //populates a JSON object of employees function populate_list() { //response completed if(req.readyState == 4) { //response ok if(req.status == 200) { //evaluate string into a JSON object var records = eval('('+req.responseText+')'); //display records show_list(records); } } } //showing employees function show_list(records) { var size = records.employees.length; var employee_list = records.employees; for(var i=0; i<size; i++) { alert('Name ' + employee_list[i].name + '\n' + 'Company ' + employee_list[i].company + '\n' + 'Email ' + employee_list[i].email + '\n' + 'Website ' + employee_list[i].website); } } //contacting perl script loadXML('../backend/script/employee.pcgi');

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-18 02:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found