2

Probably a quick answer from you experts, but i am stumbling upon a interesting challenge that I can't wrap my head around.

I have a python .psp file that contains both a list mylist[] which gets populated on runtime and a javascript function expecting a list to dynamically crreate a form object and send it when the user clicks a button. There is a reason for the button as it is part of a table that has been generated on runtime. Each row contains a different set of items created from it's own myList[] I would like to pass the rows myList[] list to the javascript function basically if the user clicks the button.

Here's some of my code to help illustrate:

Javascript:

function post(path, paramaters, method) {
    method = method || "post";

    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);
    for(var key in parameters){
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", parameters[key]);
        form.appendChild(hiddenField);
    }

    //create form and submit
    document.body.appendChild(form);
    form.submit();
}

python Server Pages (PSP) using mod_python

 <%
    myList['item1', 'item2', 'item3', 'item3']

    req.write(<input type="button" value="Upload" onclick="postCert(\'/support/upload.psp\', myList,\'post\');" />)

    %>

upload.psp is expecting the the four items in the list....

Thanks for you help on this one.

-Jim

Jim
  • 613
  • 6
  • 16
  • 25

1 Answers1

2

Try this:

<%
  import json
  myList['item1', 'item2', 'item3', 'item3']
%>

<input type="button" value="Upload" onclick="postCert('/support/upload.psp', <%= json.dumps(myList) %>, 'post');" />
Julian D.
  • 5,464
  • 1
  • 25
  • 35
  • getting ImportError: No module named json now...import json – Jim Feb 08 '12 at 20:46
  • If the `json` package can't be found, try installing and importing `simplejson` instead - http://pypi.python.org/pypi/simplejson/2.0.9. Versions of Python before 2.6 don't have `json` in the standard lib. – Matt Luongo Feb 08 '12 at 20:49
  • Added the egg with sys.path.append...saying simplejson is undefined...I have a import simplejson statement... – Jim Feb 08 '12 at 20:59
  • Above works like a boss. Just double checked my quotes and it worked for me. – Jim Feb 08 '12 at 21:24