{"id":293,"date":"2013-02-26T10:15:43","date_gmt":"2013-02-25T23:15:43","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=293"},"modified":"2015-05-15T11:15:53","modified_gmt":"2015-05-15T01:15:53","slug":"phpjavascript-what-a-duo","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/phpjavascript-what-a-duo\/","title":{"rendered":"PHP\/Javascript what a duo!"},"content":{"rendered":"<div style=\"border: 15px solid pink;padding-left: 6px;\" >You have a PHP page that shows some information, but you don&#8217;t want to show all the information shown at once. When the user clicks on a link the content of the paragraph associated with the link should appear under the link.   How do you approach this?<\/p>\n<blockquote><p><strong><em>This is largely a job Javascript could do (if you know everything ahead of time) or PHP will be needed if you don&#8217;t know everything until you click. Am going to give you guidelines regarding the first scenario (the second scenario (sort of) only differs in the sense that PHP would do what is &#8220;hard-coded&#8221; in the discussion below, adding its intelligence along the way &#8230; ie. know that PHP can write its own Javascript &#8230; very powerful duo!).<\/strong><\/em><\/p><\/blockquote>\n<p><code><\/p>\n<p><strong> Example 1 of 2:<\/p>\n<p> You have some PHP which produces HTML like below:<\/p>\n<p> &lt;a href=\"#\" id='mya' onclick='populatep();'&gt;My Link Makes Things Appear Below ...&lt;\/a&gt;<br \/>\n &lt;p id='myp' style='display:none;'&gt;&lt;\/p&gt;<\/p>\n<p> ... the new Javascript you will need ( put within &lt;head&gt; and &lt;\/head&gt; ) would look like:<\/p>\n<p> &lt;script type=\"text\/javascript\"&gt;<br \/>\n function populatep() {<br \/>\n var myst = document.getElementById( \"myp\" );<br \/>\n myst.innerHTML = myst.innerHTML + \"My paragraph appears!\";<br \/>\n myst.style.display = \"block\";<br \/>\n }<\/p>\n<p> &lt;\/script&gt;<\/p>\n<p> Example 2 of 2:<\/p>\n<p> You have some PHP which produces HTML like below:<\/p>\n<p> &lt;a href=\"#\" id='mya' onclick='populateselect();'&gt;My Link Makes Things Appear Below ...&lt;\/a&gt;<br \/>\n &lt;select id='myselect' style='display:none;'&gt;&lt;\/select&gt;<\/p>\n<p> ... the new Javascript you will need ( put within &lt;head&gt; and &lt;\/head&gt; ) would look like:<\/p>\n<p> &lt;script type=\"text\/javascript\"&gt;<br \/>\n function populateselect() {<br \/>\n var myst = document.getElementById( \"myselect\");<br \/>\n myst.innerHTML = myst.innerHTML + \"&lt;option value='\" + \"val1\" + \"'&gt;Value 1&lt;\/option&gt;\";<br \/>\n myst.innerHTML = myst.innerHTML + \"&lt;option value='\" + \"val2\" + \"'&gt;Value 2&lt;\/option&gt;\";<br \/>\n myst.innerHTML = myst.innerHTML + \"&lt;option value='\" + \"val3\" + \"'&gt;Value 3&lt;\/option&gt;\";<br \/>\n myst.innerHTML = myst.innerHTML + \"&lt;option value='\" + \"val4\" + \"'&gt;Value 4&lt;\/option&gt;\";<br \/>\n myst.style.display = \"block\";<br \/>\n }<\/p>\n<p> &lt;\/script&gt;<\/p>\n<p> Appendix 1 of 2 (PHP writing Javascript) ... :<\/p>\n<p> &lt;?php<br \/>\n echo '&lt;html&gt;' . \"n\";<br \/>\n echo '&lt;head&gt;' . \"n\";<br \/>\n echo '&lt;script type=\"text\/javascript\"&gt;' . \"n\";<br \/>\n echo ' function populatep() {' . \"n\";<br \/>\n echo ' var myst = document.getElementById( \"myp\" );' . \"n\";<br \/>\n echo ' myst.innerHTML = myst.innerHTML + \"My paragraph appears!\";' . \"n\";<br \/>\n echo ' myst.style.display = \"block\";' . \"n\";<br \/>\n echo ' }' . \"n\";<br \/>\n echo '&lt;\/script&gt;' . \"n\";<br \/>\n echo '&lt;\/head&gt;' . \"n\";<br \/>\n echo '&lt;body&gt;' . \"n\";<br \/>\n echo '&lt;\/body&gt;' . \"n\";<br \/>\n echo '&lt;\/html&gt;' . \"n\";<br \/>\n ?&gt;<\/p>\n<p> Appendix 2 of 2 (PHP writing Javascript) ... differentiating:<\/p>\n<p> &lt;?php<\/p>\n<p> if (strpos($_SERVER['QUERY_STRING'], \"#\") !== false) {<\/p>\n<p> echo '&lt;html&gt;' . \"n\";<br \/>\n echo '&lt;head&gt;' . \"n\";<br \/>\n echo '&lt;script type=\"text\/javascript\"&gt;' . \"n\";<br \/>\n echo ' function populatep() {' . \"n\";<br \/>\n echo ' var myst = document.getElementById( \"myp\" );' . \"n\";<br \/>\n echo ' myst.innerHTML = myst.innerHTML + \"My paragraph appears on the second pass!\";' . \"n\";<br \/>\n echo ' myst.style.display = \"block\";' . \"n\";<br \/>\n echo ' }' . \"n\";<br \/>\n echo '&lt;\/script&gt;' . \"n\";<br \/>\n echo '&lt;\/head&gt;' . \"n\";<br \/>\n echo '&lt;body&gt;' . \"n\";<br \/>\n echo '&lt;\/body&gt;' . \"n\";<br \/>\n echo '&lt;\/html&gt;' . \"n\";<br \/>\n } else {<br \/>\n  echo '&lt;html&gt;' . \"n\";<br \/>\n echo '&lt;head&gt;' . \"n\";<br \/>\n echo '&lt;script type=\"text\/javascript\"&gt;' . \"n\";<br \/>\n echo ' function populatep() {' . \"n\";<br \/>\n echo ' var myst = document.getElementById( \"myp\" );' . \"n\";<br \/>\n echo ' myst.innerHTML = myst.innerHTML + \"My paragraph appears!\";' . \"n\";<br \/>\n echo ' myst.style.display = \"block\";' . \"n\";<br \/>\n echo ' }' . \"n\";<br \/>\n echo '&lt;\/script&gt;' . \"n\";<br \/>\n echo '&lt;\/head&gt;' . \"n\";<br \/>\n echo '&lt;body&gt;' . \"n\";<br \/>\n echo '&lt;\/body&gt;' . \"n\";<br \/>\n echo '&lt;\/html&gt;' . \"n\";<br \/>\n }<br \/>\n ?&gt;<\/p>\n<p><\/strong><br \/>\n<\/code>\n<\/div>\n<div style=\"width: 397px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" alt=\"Think you misunderstood me when I said &quot;Get to the root of the problem&quot;?!\" src=\"http:\/\/www.rjmprogramming.com.au\/wordpress\/NewtownTree.jpg\" title=\"Think you misunderstood me when I said &quot;Get to the root of the problem&quot;?!\" width=\"387\" height=\"259\" \/><p class=\"wp-caption-text\">Think you misunderstood me when I said &quot;Get to the root of the problem&quot;?!<\/p><\/div>\n<p><strong><em>Did you know &#8230;<\/em><\/strong><br \/>\nJavaScript makes a great easy-access Calculator?<\/p>\n<p>Try typing the lines below into the address bar of your favourite browser:<\/p>\n<p>Javascript: eval(512 \/ 380);<br \/>\nJavascript: eval(512 * 380);<br \/>\nJavascript: eval(512 &#8211; 380);<br \/>\nJavascript: eval(512 + 380);<br \/>\nJavascript: eval(512 % 380);<\/p>\n<p>These days we spend so much time on the Internet it is a much quicker way to get to a calculator!\n<\/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='#d293' onclick='var dv=document.getElementById(\"d293\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=PHP\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d293' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have a PHP page that shows some information, but you don&#8217;t want to show all the information shown at once. When the user clicks on a link the content of the paragraph associated with the link should appear under &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/phpjavascript-what-a-duo\/\">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":[12],"tags":[652,932],"class_list":["post-293","post","type-post","status-publish","format-standard","hentry","category-elearning","tag-javascript","tag-php"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/293"}],"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=293"}],"version-history":[{"count":1,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/293\/revisions"}],"predecessor-version":[{"id":14835,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/293\/revisions\/14835"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}