read php from javascript

Discussion in 'PHP' started by mark103, Mar 25, 2013.

  1. #1
    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?
     
    mark103, Mar 25, 2013 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    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.
     
    jestep, Mar 25, 2013 IP
  3. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #3
    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?
     
    mark103, Mar 25, 2013 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    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.
     
    deathshadow, Mar 26, 2013 IP
  5. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #5
    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
     
    kutchbhi, Mar 29, 2013 IP
  6. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #6
    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!
     
    EricBruggema, Mar 29, 2013 IP