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

1 Answer 1

2

Try this:

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

<input type="button" value="Upload" onclick="postCert('/support/upload.psp', <%= json.dumps(myList) %>, 'post');" />
Sign up to request clarification or add additional context in comments.

4 Comments

getting ImportError: No module named json now...import json
If the json package can't be found, try installing and importing simplejson instead - pypi.python.org/pypi/simplejson/2.0.9. Versions of Python before 2.6 don't have json in the standard lib.
Added the egg with sys.path.append...saying simplejson is undefined...I have a import simplejson statement...
Above works like a boss. Just double checked my quotes and it worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.