javascript - Choose which fields to submit in a form -
i using hidden form in python cgi code pass information on cgi file (let's call printer.cgi) print information in forms. have made clickable text links point same printer file tutorial:
http://www.thesitewizard.com/archive/textsubmit.shtml
the hidden form contains massive, nested dictionary has been encoded text json. printer file turns string dictionary object. based on link clicked, printer file decides information in dictionary print. however, decoding , encoding entire dictionary inefficient , slows down program. instead submit through hidden form smaller dictonary objects print rather entire dictionary. here example of doing:
javascript code:
function getdetails(selectedfield) { document.results.resultstoprint.value = selectedfield; document.results.submit(); }
python method:
def transfertestresults(testresults, printer_file_directory): form = "<form name = 'results' method = 'post' action = '%s'> \n" % (printer_file_directory) datastring = str(json.dumps(testresults)) #converting massive dictionary string datastring = datastring.replace("\"", """) form += "<input type = 'hidden' name = 'testresults' value = \"%s\">" % (datastring) form += "<input type = 'hidden' name = 'resultstoprint' > \n"#value determined javascript:getdetails() form += "</form> \n" return form
testresults here 1 big dictionary. instead rather make form each nested dictionary such as:
def transfertestresults(testresults, detail_generator_directory): form = "<form name = 'results' method = 'post' action = '%s'> \n" % (detail_generator_directory) datastring1 = str(json.dumps(testresults['testpasses'])) datastring1 = datastring.replace("\"", """) datastring2 = str(json.dumps(testresults['testfailures'])) datastring2 = datastring.replace("\"", """) datastring3 = str(json.dumps(testresults['commandpasses'])) datastring3 = datastring.replace("\"", """) datastring4 = str(json.dumps(testresults['commandfailures'])) datastring4 = datastring.replace("\"", """) form += "<input type = 'hidden' name = 'testresults1' value = \"%s\">" % (datastring1) form += "<input type = 'hidden' name = 'testresults2' value = \"%s\">" % (datastring2) form += "<input type = 'hidden' name = 'testresults3' value = \"%s\">" % (datastring3) form += "<input type = 'hidden' name = 'testresults4' value = \"%s\">" % (datastring4) form += "<input type = 'hidden' name = 'resultstoprint' > \n"#value determined javascript:getdetails() form += "</form> \n" return form
i submit specific field based on link clicked. example if click link 1, send field datastring1. question boils down this, can submit fields in form rather entire form based on user actions
the following trivial example of how might go this. can disable inputs default, enable ones want before submitting form.
i've submitted form using submit method, might want use ajax or whatever. also, might want attach listeners dynamically, or use event delegation (which preferred design).
<script> function dosend(el) { var form = document.forms['form0']; var controlname = el.getattribute('data-sendname'); form[controlname].disabled = false; form.submit(); } </script> <form id="form0"> <input name="foo" value="foo" disabled> <input name="bar" value="bar" disabled> </form> <p onclick="dosend(this)" data-sendname="foo">send foo</p> <p onclick="dosend(this)" data-sendname="bar">send bar</p>
Comments
Post a Comment