{"id":168,"date":"2019-06-10T18:13:45","date_gmt":"2019-06-10T17:13:45","guid":{"rendered":"https:\/\/blog.inplico.uk\/?p=168"},"modified":"2019-08-03T01:02:36","modified_gmt":"2019-08-03T00:02:36","slug":"javascript-navigation-and-form-data","status":"publish","type":"post","link":"https:\/\/blog.inplico.uk\/?p=168","title":{"rendered":"Javascript navigation and form data."},"content":{"rendered":"<p>One of the things that I found myself doing over and over again was creating a function to post data back to the server so I created a tiny class that makes use of my <strong>jsfunction<\/strong> class to simplify the creation of post functions.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include \"jsnav.h\"\r\n\r\nusing std::string;\r\nusing std::vector;\r\n\r\njsnav::jsnav(std::string fName)\r\n    :\r\n      jsfunction(build_function_name(fName))\r\n\r\n{\r\n    jsfunction::add_line(\"var form = document.createElement(\\\"form\\\");\");\r\n    jsfunction::add_line(\"form.setAttribute(\\\"method\\\", \\\"post\\\");\");\r\n    jsfunction::add_line(\"form.setAttribute(\\\"action\\\", doNext);\");\r\n    jsfunction::add_line_break();\r\n\r\n}\r\n\r\nstring jsnav::build_function_name(string fName) {\r\n\r\n    string fcn;\r\n    if(fName.find_first_of(\"(\") == std::string::npos)\r\n        fcn = fName+\"(doNext)\";\r\n    else {\r\n        fcn = fName;\r\n    }\r\n\r\n    return(fcn);\r\n\r\n}\r\n\r\nvoid jsnav::build_js_function() {\r\n\r\n    auto val = [](POST_DATA &amp;cur)-&gt;string{\r\n        return(\r\n                (cur.literal) ?\r\n                         \"\\\"\"+cur.value+\"\\\"\" : cur.value\r\n                );\r\n    };\r\n\r\n    auto nam = [&amp;](POST_DATA &amp;cur)-&gt;string {\r\n        string name = m_fcnDeclaration.substr(\r\n                    m_fcnDeclaration.find_first_of(\"(\"),\r\n                    m_fcnDeclaration.find_last_of(\")\")\r\n                    );\r\n        return(\r\n                    (name.find(cur.name) == std::string::npos) ?\r\n                        \"\\\"\"+cur.name+\"\\\"\" : cur.name\r\n                        );\r\n    };\r\n\r\n    for(POST_DATA &amp;it : m_postData) {\r\n        string elementId = \"FRM_\" + it.name;\r\n        jsfunction::add_line(\"var \" + elementId + \" = document.createElement(\\\"input\\\");\");\r\n        jsfunction::add_line(elementId+\".setAttribute(\\\"type\\\", \\\"hidden\\\");\");\r\n        jsfunction::add_line(elementId+\".setAttribute(\\\"name\\\", \" + nam(it) + \");\");\r\n        jsfunction::add_line(elementId+\".setAttribute(\\\"value\\\", \" + val(it) + \");\");\r\n        jsfunction::add_line(\"form.appendChild(\" + elementId + \");\");\r\n        jsfunction::add_line_break();\r\n    }\r\n\r\n    jsfunction::add_line(\"document.body.appendChild(form);\");\r\n    jsfunction::add_line(\"form.submit();\");\r\n}\r\n\r\nvoid jsnav::add_attribute(std::string name, std::string value, bool literal) {\r\n    m_postData.push_back( {name, value, literal} );\r\n}\r\n\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h5><strong>The constructor:<\/strong><\/h5>\n<p>jsnav without any arguments will create a function called <strong>nav(doNext)<\/strong>.<\/p>\n<p>jsnav with a given name will create a function with that name i.e.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">jsnav fcn(\"myfcn\");<\/pre>\n<p>&nbsp;<\/p>\n<p>will create a function called <strong>myfcn(doNext)<\/strong>.<\/p>\n<p>jsnav with with a series of arguments will declare the function literally<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">jsnav fcn(\"myfcn(arg1, arg2, doNext\");<\/pre>\n<p>&nbsp;<\/p>\n<p>Note that one of the arguments must always be doNext.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>add_attribute(std::string <\/strong><em>name<\/em><strong>, std::string <\/strong><em>value<\/em><strong>, bool <\/strong><em>literal<\/em><strong>)<\/strong><\/p>\n<p>This is the method you call to set a given value when the method is called.<\/p>\n<h6><strong>Name:<\/strong><\/h6>\n<p>If the name is found in the function declaration then it will be treated as a variable, if not then it will be treated as a literal<\/p>\n<h6>Value:<\/h6>\n<p>The value can either be a literal value or something else such as a function call.\u00a0 If it is a literal string then you do not need to pass a third argument as <strong>bool literal<\/strong> is automatically set to <strong>true<\/strong>. If it is a function call then you must set <strong>literal<\/strong> to <strong>false<\/strong>.<\/p>\n<p>Theoretically this method can be called as many times as necessary.<\/p>\n<p>When you are done adding attributes then you need to call <strong>build_js_function()<\/strong> in order to construct the function before calling <strong>js_function(int indent)<\/strong> to grab the string;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the things that I found myself doing over and over again was creating a function to post data back to the server so I created a tiny class that makes use of my jsfunction class to simplify the creation of post functions. #include &#8220;jsnav.h&#8221; using std::string; using std::vector; jsnav::jsnav(std::string fName) : jsfunction(build_function_name(fName)) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-168","post","type-post","status-publish","format-standard","hentry","category-cgi"],"_links":{"self":[{"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/posts\/168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=168"}],"version-history":[{"count":4,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions\/212"}],"wp:attachment":[{"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}