Hi guys I need your help. I am writing the list of code in my javascript and now I want to read my php code from my javascript using with loadjscssfile("test1.php", "js") function. function loadjsfile(filename) { var fileref=document.createElement('script'); fileref.setAttribute("type","text/javascript") fileref.setAttribute("src", filename); document.body.appendChild(fileref); } document.onkeydown = function(ev) { var key; ev = ev || event; key = ev.keyCode; var image1 = document.getElementById("image1").getElementsByTagName("img")[0]; if(key == 13) { if (img1.indexOf('image1_yellow.jpg') != -1) { if (img5.indexOf('picture1_yellow.jpg') != -1) { loadjscssfile("test1.php", "js"); } } } Code (markup): Does anyone know how i can read my php code from javascript?
Not quite sure what you're trying to accomplish. Javascript is client side so there's no way to read a server side file / script as it will be executed prior to javascript or any brwser having access to it. If you're looking to return some value from a php script, json is typically the cleanest and most appropriate way of doing it. PHP can use json_encode to for data returned via an ajax request. Otherwise if you want to explain a little more of what you're needing to do, there may be a better answer.
you don't understand what I am trying to achieve. I want to achieve is to read php in javascript when the image of picture1 is change it to picture1_yellow. I am using javascript to change the image when i press on the keyboard. Now I want to read php from javascript. See this code: if (img1.indexOf('image1_yellow.jpg') != -1) { if (img5.indexOf('picture1_yellow.jpg') != -1) { loadjscssfile("test1.php", "js"); } Code (markup): I am not really sure if it possible for php to detect the keycode that I press on it and when the image is change?
Why would you be generating javascript with php?!? Either you're not grasping how php files are accessed from a server, or you're doing something in server side code that shouldn't even BE server side code. Really generating CSS from PHP is just bad practice and since it is then dynamic content, defeats one of the entire advantages of CSS - CACHING. We'd have to see the contents of test1.php to even START to weigh in on what you're doing wrong... but again this is why snippets are like trying to to brain surgery through a keyhole with an endoscope and two long-reach grippers." NOT that you should be loading CSS based on a keypress -- since that will take time and not really feel responsive. You load the CSS before you even use it, and then apply it to the relevant elements via something simple like a class swap.
I think the answer you are looking for is ajax (I could be completely wrong though?) . Try sending the data-string ('js') to your php script via ajax
AJAX is indeed the way to communicatie between your JS code and the server. You can use PHP to output data as Javascript (using EVAL), JSON or XML or ofcource as PLAIN HTML!. I recommend you use AJAX in combination with jQuery as its much easier to understand and implement!