{"id":42041,"date":"2018-11-27T03:01:06","date_gmt":"2018-11-26T17:01:06","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=42041"},"modified":"2018-11-26T12:48:20","modified_gmt":"2018-11-26T02:48:20","slug":"ajax-formdata-javascript-json-parse-and-stringify-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/ajax-formdata-javascript-json-parse-and-stringify-primer-tutorial\/","title":{"rendered":"Ajax FormData Javascript JSON Parse and Stringify Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/json_code.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Ajax FormData Javascript JSON Parse and Stringify Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/json_code_ajax.jpg\" title=\"Ajax FormData Javascript JSON Parse and Stringify Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Ajax FormData Javascript JSON Parse and Stringify Primer Tutorial<\/p><\/div>\n<p>For the most part, adding onto yesterday&#8217;s <a title='PHP JSON Decode and Encode Primer Tutorial' href='#phpjsondept'>PHP JSON Decode and Encode Primer Tutorial<\/a> with today&#8217;s clientside &#8220;Ajax FormData Javascript JSON Parse and Stringify&#8221; thinking we could have started afresh with an HTML codebase, except for one thing in the code that is &#8230; <a target=_blank title='?' href='https:\/\/www.youtube.com\/watch?v=uhiCFdWeQfA'>anyone, anyone<\/a>? &#8230; yes, Edgar J Hoover, that would be the &#8220;<b><i>post<\/i><\/b>&#8220;, rather than &#8220;get&#8221; in the <i>codeline<\/i> &#8230;<\/p>\n<p><code><br \/>\nfunction clickit(bois) {<br \/>\n if (bois.id == 'ajaxit') {<br \/>\n  form = new FormData();<br \/>\n  xhr = new XMLHttpRequest();<br \/>\n  xhr.onreadystatechange = function() {<br \/>\n    if (this.readyState == 4 && this.status == 200) {<br \/>\n      var myobj = JSON.parse(this.responseText);<br \/>\n      var str=syntaxHighlight(myobj); \/\/JSON.stringify(myobj, null, 2);<br \/>\n      document.getElementById('result').innerHTML=\\\"&lt;br&gt;&lt;br&gt;&lt;a target=_blank title=JSON.Stringify href='\/\/www.w3schools.com\/js\/js_json_stringify.asp'&gt;JSON.stringify&lt;\/a&gt;(&lt;a target=_blank title=JSON.Parse href='\/\/www.w3schools.com\/js\/js_json_parse.asp'&gt;JSON.parse&lt;\/a&gt;('\\\" + this.responseText + \\\"')) = &lt;br&gt;&lt;br&gt;\\\" + str;<br \/>\n      console.log(myobj);<br \/>\n    }<br \/>\n  };<br \/>\n    form.append('ajax', 'yes');<br \/>\n    form.append('injson', document.getElementById('trcontent').innerText);<br \/>\n    <i>xhr.open('<b>post<\/b>', 'json_code.php', true);<\/i><br \/>\n    xhr.setRequestHeader('Cache-Control', 'no-cache');<br \/>\n    xhr.send(form);<br \/>\n } else {<br \/>\n  document.getElementById('mysubmit').click();<br \/>\n }<br \/>\n}<br \/>\n<\/code><\/p>\n<p> &#8230; the reason for keeping &#8220;post&#8221; being, purely and simply, and not for functional reasons (though it could be of interest to you that the user cannot know what data was involved by looking at the web browser address bar URL used), that &#8220;post&#8221; can handle more data than &#8220;get&#8221; (affecting the web browser address bar URL via those ? and &amp; arguments) can.  The upshot, though, is that we stay with a PHP codebase.  However, we want you to know, for small amounts of JSON data, you can use &#8230;<\/p>\n<ul>\n<li>HTML<\/li>\n<li>Javascript<\/li>\n<li><a target=_blank title='XMLHttpRequest object information' href='https:\/\/www.w3schools.com\/js\/js_ajax_http.asp'>Ajax<\/a><\/li>\n<li><a target=_blank title='FormData object information' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/FormData'>FormData<\/a> (optionally, or else a real HTML form)<\/li>\n<\/ul>\n<p> &#8230; to send and receive JSON data from a webpage.<\/p>\n<p>And so again, please feel free to try out the <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/PHP\/json_code.php-GETME\" title=\"json_code.php\">changed PHP<\/a> <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/json_code.php-GETME\" title=\"json_code.php\">json_code.php<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/json_code.php\" title=\"Click picture\">live run<\/a> way for you to try out some PHP and\/or Ajax FormData Javascript interplay via JSON data.<\/p>\n<hr>\n<p id='phpjsondept'>Previous relevant <a target=_blank title='PHP JSON Decode and Encode Primer Tutorial' href='\/\/www.rjmprogramming.com.au\/ITblog\/php-json-decode-and-encode-primer-tutorial\/'>PHP JSON Decode and Encode Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/json_code.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"PHP JSON Decode and Encode Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/json_code.jpg\" title=\"PHP JSON Decode and Encode Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">PHP JSON Decode and Encode Primer Tutorial<\/p><\/div>\n<p>PHP and Javascript can form a potent mix of serverside and clientside interplay for web applications.  As a conduit between the two, datawise, and an industry standard, is the <a target=_blank title='JSON information from w3schools' href='\/\/www.w3schools.com\/js\/js_json_intro.asp'>JSON<\/a> protocol &#8230;<\/p>\n<blockquote cite='\/\/www.w3schools.com\/js\/js_json_intro.asp'><p>\nJSON: JavaScript Object Notation.<\/p>\n<p>JSON is a syntax for storing and exchanging data.<\/p>\n<p>JSON is text, written with JavaScript object notation.<\/p>\n<p>When exchanging data between a browser and a server, the data can only be text.<\/p>\n<p>JSON is text, and we can convert any JavaScript object into JSON, and send JSON to the server.<\/p>\n<p>We can also convert any JSON received from the server into JavaScript objects.<\/p>\n<p>This way we can work with the data as JavaScript objects, with no complicated parsing and translations.\n<\/p><\/blockquote>\n<p>Looking at this JSON conduit from the PHP serverside, we have two really useful PHP functions, namely &#8230;<\/p>\n<ul>\n<li><a target=_blank title='PHP json_decode information, thanks' href='\/\/php.net\/manual\/en\/function.json-decode.php'>json_decode<\/a> \u2014 Decodes a JSON string<\/li>\n<li><a target=_blank title='PHP json_encode information, thanks' href='\/\/php.net\/manual\/en\/function.json-encode.php'>json_encode<\/a> \u2014 Returns the JSON representation of a value<\/li>\n<\/ul>\n<p> &#8230; that would be good to learn for any programmer interested in data, in all its forms, and possibilities.  And particularly for those programmers who love working with <a target=_blank title='Objects information from w3schools' href='https:\/\/www.w3schools.com\/js\/js_objects.asp'>objects<\/a> and <a target=_blank title='JSON syntax' href='https:\/\/www.w3schools.com\/js\/js_json_syntax.asp'>object notation<\/a>.<\/p>\n<p>What we&#8217;ve done today, in an introductory sense, is to write a PHP web application allowing the user to define up to 5 pieces of data, be that a scalar one, or an array one or a structure one, that collects JSON syntax into a JSON string.  That approach of doing this is new to us, and goes like this, in broad brush terms &#8230;<\/p>\n<ul>\n<li>we start with an HTML table element with columns enough for up to 5 delimited columns of data (in those forms talked about above) &#8230; and it is the case that &#8230;<\/li>\n<li>the web application is responsible for maintaining a valid JSON string, made up of several HTML td cell (innerHTML) parts (within a single HTML tr row &#8230; hint, hint) &#8230; so that being the case &#8230;<\/li>\n<li>what is posted back to the same PHP code via an HTML form method=POST action=[here&#8217;s looking at you, kid] becomes, simply, the one (relevant) HTML tr (row) element&#8217;s <a target=_blank title='HTML and Javascript innerText property information from w3schools' href='https:\/\/www.w3schools.com\/jsref\/prop_node_innertext.asp'>innerText<\/a> property value &#8230; cute, huh?!<\/li>\n<\/ul>\n<p> &#8230; so, in &#8220;response mode&#8221; the web application first calls &#8230;<\/p>\n<ul>\n<li><a target=_blank title='PHP json_decode information, thanks' href='\/\/php.net\/manual\/en\/function.json-decode.php'>json_decode<\/a> \u2014 to decode a JSON string (displayed via <a target=_blank title='PHP var_dump information' href='http:\/\/php.net\/manual\/en\/function.var-dump.php'>var_dump<\/a> PHP function), then &#8230; for the heck of it &#8230; calls &#8230;<\/li>\n<li><a target=_blank title='PHP json_encode information, thanks' href='\/\/php.net\/manual\/en\/function.json-encode.php'>json_encode<\/a> \u2014 to sanity check the JSON representation of that value (<font size=1>as much as anything because the dollar sign in front of a PHP variable name can cause some unusual behaviour, on occasions<\/font>)<\/li>\n<\/ul>\n<p>Please feel free to try out the PHP <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/json_code.php_GETME\" title=\"json_code.php\">json_code.php<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/json_code.php\" title=\"Click picture\">live run<\/a> way for you to try out some PHP and Javascript interplay via JSON data.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d42027' onclick='var dv=document.getElementById(\"d42027\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/json\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d42027' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d42041' onclick='var dv=document.getElementById(\"d42041\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/formdata\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d42041' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>For the most part, adding onto yesterday&#8217;s PHP JSON Decode and Encode Primer Tutorial with today&#8217;s clientside &#8220;Ajax FormData Javascript JSON Parse and Stringify&#8221; thinking we could have started afresh with an HTML codebase, except for one thing in the &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/ajax-formdata-javascript-json-parse-and-stringify-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,12,14,37],"tags":[69,1580,1607,2749,452,2730,2469,652,664,666,2748,849,2750,932,997,1075,1238,1319],"class_list":["post-42041","post","type-post","status-publish","format-standard","hentry","category-ajax","category-elearning","category-event-driven-programming","category-tutorials","tag-ajax","tag-cell","tag-decode","tag-encode","tag-form","tag-formdata","tag-innertext","tag-javascript","tag-json","tag-json_decode","tag-json_encode","tag-object","tag-object-notation","tag-php","tag-programming","tag-row","tag-table","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/42041"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=42041"}],"version-history":[{"count":2,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/42041\/revisions"}],"predecessor-version":[{"id":42043,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/42041\/revisions\/42043"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=42041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=42041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=42041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}