python list to javascript array
-
27-04-2021 - |
Question
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
Solution
Try this:
<%
import json
myList['item1', 'item2', 'item3', 'item3']
%>
<input type="button" value="Upload" onclick="postCert('/support/upload.psp', <%= json.dumps(myList) %>, 'post');" />