in reply to Re: copying perl array to javascript array
in thread copying perl array to javascript array

Greetings ikegami
I tried using your code. Its works fine except for the fact that the last array value is followed by the whole html code in the form.
For eg:- If the array values are A,B,C,D and i add them in the drop down menu then the last option(D) is followed by the whole html code. So I have four options in the drop down box A,B,C,D<htmlcode>
Am i doing something wrong?
See code below.
sub serialize_string_list { return join('|', map { (defined($_) ? do { local $_=$_; s/\^/^1/g; s/\|/^2/g; $_ } : '^0' ) } @_ ); } sub populate{ @n=(); my $dt; my $row; #course contains the values fetched from the database foreach $row (@$course) { ($dt) = @$row; push(@n,$dt); } return serialize_string_list @n; }; //deserializes the array function deserialize_string_list(s) { var fields = s.split("|") var i; for (i=0; i<fields.length; i++) { if (fields[i] == "^0") { fields[i] = null; } else { var re; re = /\^2/g; fields[i] = fields[i].replace(re, "|"); re = /\^1/g; fields[i] = fields[i].replace(re, "^"); } } return fields; } //calling Sajax wrapper function from a javascript function function do_populate() { x_populate(do_populate_cb); } function do_populate_cb(s) { var arr = deserialize_string_list(s); var i; for (i=0; i<arr.length; i++) { with(document.doc1.selCourseNm) { options[i]=new Option(arr[i],arr[i]); } } }

Replies are listed 'Best First'.
Re^3: copying perl array to javascript array
by ikegami (Patriarch) on Aug 02, 2005 at 19:36 UTC

    Did you forget to close your SELECT tag?

    How about you show us runnable code that exhibits the problem.

    By the way. I can't find any documentation for Option's contructor. In fact, according to the documentation, nodes should be created using createElement and createTextNode methods. What follows uses the correct methods.

    function do_populate_cb(s) { var arr = deserialize_string_list(s); var options = document.doc1.selCourseNm.options; var i; // Remove any options it currently has. options.length = 0; // Add new options. for (i=0; i<arr.length; i++) { var option = document.createElement('OPTION'); option.value = arr[i]; option.innerText = arr[i]; options.add(option); } }
      Greetings Monks, Sorry for the late response. I was away so cudnt post earlier.
      I'm still having the same old problem in which the html code gets appended to the drop down options. You can see the live form at http://tigger.uic.edu/htbin/survey-send.pl
      Try selecting an option from the CourseDept dropdown and the CourseName combobox will be dynamically populated. Check out the last option in the Coursename drop down menu. It has the html course appended to it. Also when you change the CourseDept option the CourseName combo box shows some of the old options aswell. This was not happening before. It started happening only after i started using Sajax.
      I'm new to web stuff esp. perl and using perl javascript together is giving me problems.
      Can you please help?
      Thanks
        I've tried that link, but the javascript code didn't work for me - I get a scrambled dropdown box in CourseName.

        My general solution is to get the code to work *without* javascript first (i.e. use request/response type reloading), and when you've done that, try to improve on it with AJAX technologies - and if you can't get it to work on a specific browser, fall back to the level you *know* that works.

        I'm biased because a) I've been using linux since 1999 or something, so the most "popular" browser isn't available to me. b) I've designed a few websites for government agencies that required the web accessibility guidelines (priority one).

        By the way, XMLHttpRequest code isn't all that difficult if you know how to do javascript. I believe this article from apple is good introduction. You'll be able to write your own code (with a little experimentation).

        Also, you could take a look at HTML-Prototype: it's a browser-agnostic, reasonably well documented library for perl, that should take care of most of your problems.