Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Try this:

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

<input type="button" value="Upload" onclick="postCert('/support/upload.psp', <%= json.dumps(myList) %>, 'post');" />
share|improve this answer
    
getting ImportError: No module named json now...import json –  Jim Feb 8 '12 at 20:46
    
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. –  Matt Luongo Feb 8 '12 at 20:49
    
Added the egg with sys.path.append...saying simplejson is undefined...I have a import simplejson statement... –  Jim Feb 8 '12 at 20:59
    
Above works like a boss. Just double checked my quotes and it worked for me. –  Jim Feb 8 '12 at 21:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.