body { background-color: yellow; } #trhead { border: 2px solid black; } #trcontent { background-color: pink; } "; $jscr = " var biggestsofar=0; var lastone=0; var form=null; var xhr=null; function seldo(sois) { if (sois.value != '') { if (sois.value.trim() == '') { if (sois.value == ' ') { clickit(document.getElementById('phpit')); } else { clickit(document.getElementById('ajaxit')); } } else { var jj=0, cval=''; var colis=sois.id.replace('sel',''); var earlierthere=false; var yo=''; for (var ii=1; ii biggestsofar) { biggestsofar=eval('' + colis); } for (jj=2; jj<=4; jj++) { if (document.getElementById('dtd' + jj).innerHTML == ',' && document.getElementById('td' + jj).innerHTML == '') { if (document.getElementById('dtd' + eval(1 + eval('' + jj))).innerHTML == ',') { document.getElementById('dtd' + jj).innerHTML=''; } } } lastone=eval('' + colis); if (sois.value == 'days_of_the_week') { cval='\"' + sois.value + '_' + colis + '\"' + ':[' + '\"Sunday\",' + '\"Monday\",' + '\"Tuesday\",' + '\"Wednesday\",' + '\"Thursday\",' + '\"Friday\",' + '\"Saturday\"' + ']'; } else if (sois.value == 'top_ten_longest_river_lengths') { cval='\"' + sois.value + '_' + colis + '\"' + ':[' + '{\"river\":\"Nile\",\"miles\":4180},{\"river\":\"Amazon\",\"miles\":3912},{\"river\":\"Mississippi\",\"miles\":3710},{\"river\":\"Yangtze\",\"miles\":3602},{\"river\":\"Ob\",\"miles\":3459},{\"river\":\"Yellow\",\"miles\":2900},{\"river\":\"Yenisei\",\"miles\":2800},{\"river\":\"Parana\",\"miles\":2795},{\"river\":\"Irtish\",\"miles\":2758},{\"river\":\"Congo\",\"miles\":2716}' + ']'; } else if (sois.value == 'continents_of_the_earth') { cval='\"' + sois.value + '_' + colis + '\"' + ':[' + '\"Africa\",' + '\"Antarctica\",' + '\"Asia\",' + '\"Australia\",' + '\"Europe\",' + '\"North America\",' + '\"South America\"' + ']'; } else if (sois.value == 'months_of_the_year') { cval='\"' + sois.value + '_' + colis + '\"' + ':[' + '\"January\",' + '\"February\",' + '\"March\",' + '\"April\",' + '\"May\",' + '\"June\",' + '\"July\",' + '\"August\",' + '\"September\",' + '\"October\",' + '\"November\",' + '\"December\"' + ']'; } else if (sois.value == 'your_own_array') { yo=prompt('Please enter array members as comma separated list and double quote is good character to delimit strings',''); if (yo == null) { yo=''; } if (yo != '') { cval='\"' + sois.value + '_' + colis + '\"' + ':[' + yo.replace(/\[/g,'').replace(/\]/g,'') + ']'; } } else if (sois.value == 'your_own_entry') { yo=prompt('Please enter your own entry and double quote is good character to delimit strings',''); if (yo == null) { yo=''; } if (yo != '' && yo.indexOf(',') != -1) { cval='\"' + sois.value + '_' + colis + '\"' + ':[' + yo.replace(/\[/g,'').replace(/\]/g,'') + ']'; } else if (yo != '') { if (isNaN(yo)) { cval='\"' + sois.value + '_' + colis + '\"' + ':\"' + yo.replace(/\[/g,'').replace(/\]/g,'').replace(/\\\"/g,'') + '\"'; } else { cval='\"' + sois.value + '_' + colis + '\"' + ':' + yo.replace(/\[/g,'').replace(/\]/g,'') + ''; } } } if (cval != '') { document.getElementById('td' + colis).innerHTML=cval; if (earlierthere) { document.getElementById('dtd' + colis).innerHTML=','; } if (document.getElementById('td' + colis).innerHTML != '') { if (document.getElementById('tdstart').innerHTML == '') { document.getElementById('thstart').innerHTML=\" \"; } document.getElementById('tdstart').innerHTML='{'; document.getElementById('tdend').innerHTML='}'; } } //alert(document.getElementById('trcontent').innerText.replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(/\,\,\,\,/g,',').replace(/\,\,\,/g,',').replace(/\,\,/g,',').replace(/\,\ \,/g,',').replace(/\ /g,'^')); document.getElementById('injson').value=document.getElementById('trcontent').innerText; //.replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(String.fromCharCode(8),'').replace(/\,\,\,\,/g,',').replace(/\,\,\,/g,',').replace(/\,\,/g,',').replace(/\,\ \,/g,','); //alert(document.getElementById('trcontent').innerText); } } } function clickit(bois) { if (bois.id == 'ajaxit') { form = new FormData(); xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var myobj = JSON.parse(this.responseText); var str=syntaxHighlight(myobj); //JSON.stringify(myobj, null, 2); document.getElementById('result').innerHTML=\"

JSON.stringify(JSON.parse('\" + this.responseText + \"')) =

\" + str; console.log(myobj); } }; form.append('ajax', 'yes'); form.append('injson', document.getElementById('trcontent').innerText); xhr.open('post', 'json_code.php', true); xhr.setRequestHeader('Cache-Control', 'no-cache'); xhr.send(form); } else { document.getElementById('mysubmit').click(); } } function syntaxHighlight(json) { // thanks to https://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript if (typeof json != 'string') { json = JSON.stringify(json, undefined, 2); } json = json.replace(/&/g, '&').replace(//g, '>'); return json.replace(/(\"(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\\"])*\"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { var cls = 'number'; if (/^\"/.test(match)) { if (/:$/.test(match)) { cls = 'key'; } else { cls = 'string'; } } else if (/true|false/.test(match)) { cls = 'boolean'; } else if (/null/.test(match)) { cls = 'null'; } return '' + match + ''; }); }
"; $selstuff = " "; $tablestuff = "
Column 1/5" . str_replace('selx','sel1',$selstuff) . "Column 2/5" . str_replace('selx','sel2',$selstuff) . "Column 3/5" . str_replace('selx','sel3',$selstuff) . "Column 4/5" . str_replace('selx','sel4',$selstuff) . "Column 5/5" . str_replace('selx','sel5',$selstuff) . "
"; if (isset($_GET['injson']) || isset($_POST['injson'])) { if (isset($_GET['ajax']) || isset($_POST['ajax'])) { if (isset($_GET['injson'])) { echo str_replace('+',' ',urldecode($_GET['injson']) ); } else { echo str_replace('+',' ',urldecode($_POST['injson']) ); } } else { if (isset($_GET['injson'])) { echo "" . $cstyle . $jscr . "

Using PHP json_encode and json_decode or Ajax FormData and Javascript JSON.parse and JSON.stringify

RJM Programming

November, 2018

JSON in is " . str_replace('+',' ',urldecode($_GET['injson']) ) . "
"; echo "
\$jd=json_decode('" . str_replace('+',' ',urldecode($_GET['injson']) ) . "'); // results in ...
"; $jd=json_decode(str_replace('+',' ',urldecode($_GET['injson']) )); var_dump($jd); echo "
json_encode(\$jd); // results in ...

"; echo json_encode($jd) . "
"; echo "



" . $tablestuff . "
"; exit; } else { echo "" . $cstyle . $jscr . "

Using PHP json_encode and json_decode or Ajax FormData and Javascript JSON.parse and JSON.stringify

RJM Programming

November, 2018

JSON in is " . str_replace('+',' ',urldecode($_POST['injson']) ) . "
"; echo "
\$jd=json_decode('" . str_replace('+',' ',urldecode($_POST['injson']) ) . "'); // results in ...
"; $jd=json_decode(str_replace('+',' ',urldecode($_POST['injson']) )); var_dump($jd); echo "
json_encode(\$jd); // results in ...

"; echo json_encode($jd) . "
"; echo "



" . $tablestuff . "
"; exit; } } } else { echo "" . $cstyle . $jscr . "

Using PHP json_encode and json_decode or Ajax FormData and Javascript JSON.parse and JSON.stringify

RJM Programming

November, 2018

" . $tablestuff . "
"; } ?>