www.webdeveloper.com
Results 1 to 9 of 9

Thread: writing php through javascript with a variable in the middle

  1. #1
    Join Date
    Apr 2006
    Posts
    58

    writing php through javascript with a variable in the middle

    I am attempting to write a piece of php code into a div tag via JS while having input JS variables, eg.
    Code:
    no = "123";
    target = document.getElementById("target");
    target.innerHTML += "...php code "+no+" here...";
    When i try:
    Code:
    no = "123";
    target.innerHTML += "<?php openfile('file_"+no+".txt') ?>";
    It doesn't recognise the "+no+" ,
    Then when I try
    Code:
    target.innerHTML += "\<?php openfile('file_"+no+".txt') ?\>";
    It comes up with a php error.

    To see what i mean in more context Click Here.


    I really need to find a way to do this or similar!

  2. #2
    Join Date
    Jul 2005
    Location
    USA
    Posts
    3,910
    JavaScript can't write PHP code. It's a client-side language. Since PHP is a server-side language, it can write (client-side) JavaScript code.

  3. #3
    Join Date
    Dec 2005
    Location
    Queen Creek, AZ
    Posts
    1,564
    php can run javascript directly, you just need to know how to do it.

    as long as your file is a php file, it can be done.

    you can either call the javascript from an external source and insert php variables into it, or you can embed the javascript directly into your php page.

    I don't fully understand what it is you want, so explain a little more, please.

    thx

  4. #4
    Join Date
    Aug 2006
    Posts
    31
    jclarke, what exactly are you trying to do there may be a way to achieve it using both php and javascript in a different way than you showed here.

  5. #5
    Join Date
    Apr 2006
    Posts
    58
    I'm making a sort of chat thing,
    where two members can chat to each other if they have each others consent.

    I've worked my way around it using an iframe with a php file that gets a variable that sends it back to the root frame, long and tricky but it works and that's all that matters.

    Thanks for your advice though I didn't think that I could write php through javascrpt anyway I just needed to confirm that.

    Thanks all,
    cya

  6. #6
    Join Date
    Aug 2006
    Posts
    31
    you can speed it up by using ajax and getting rid of that iframe.

  7. #7
    Join Date
    Apr 2006
    Posts
    58
    what's ajax?

  8. #8
    Join Date
    Dec 2005
    Location
    India
    Posts
    490
    PHP - a server side language. Before the page loads, all the content is parsed and downloaded on the client's machine using which the browser displays the page. Javascripts is also generated at that time. You can generate Javascript from PHP but not PHP from Javascript.

    no = "123";
    target.innerHTML += "<?php openfile('file_"+no+".txt') ?>";

    Doing this will load the javascript with the intial value of "no" that is defined in your function.
    "no" is a variable on client side.. not server side. One way of doing this is to have a loop (if you know what all values the "no" is supposed to have).

    <?
    $no = array("123","124","121","122");
    foreach $no as $key and $val{?>
    if(no=="<?echo $val?>"){
    target.innerHTML += "<?php file_get_contents('file_'.$val.".txt') ?>";
    }
    <?}?>
    [this may contain some errors.. not tested]
    doing like this, the user falls in the appropraite "if" condition.
    but this will increase the size of the downloaded file tremendously. Ajax is the best way to do this.
    http://www.xml.com/pub/a/2005/02/09/...p-request.html

  9. #9
    Join Date
    Apr 2006
    Posts
    58
    ok cool,
    thanks shridhar_423

    I will look into ajax a bit more and see if i can find a solution

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles