SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member lionking2's Avatar
    Join Date
    May 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Call and implement PHP+javascript code by using ajax

    Hi

    I have Mixed Code php + javascript Inside php file . I called the code inside this file by ajax .

    Example of the php code

    PHP Code:
    echo "<script type='text/javascript'>
      alert('Hello')
    </script>"

    The problem is that PHP code is executed, but the JavaScript code is not executed .

    Is there a solution for the implementation the two codes PHP + JS using ajax request

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by lionking2 View Post
    Is there a solution for the implementation the two codes PHP + JS using ajax request
    You could output from the PHP code some data, which the javascript code then processes in certain ways. Commonly JSON is the data format that is used, so that multiple bits of data can be easily transferred as need be.

    An alternative solution might be for the PHP code to send back the name of a script file which you can then add to your page.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess an example could be, if you're using ajax with the GET method,

    Code PHP:
    <?php
     
    $x = $_GET['x']
     
    echo "<script type=\"text/javascript\">
             document.write(\"" . $x . "\");
             </script>";
     
    ?>

    Something along those lines. You can evaluate with javascript all you want.

    Now the problem comes when you want to evaluate something in javascript, and then use it by the php code, like adding in javascript and assigning to a php variable, instead of the way I can do it, which is the other way around. Not to say it's impossible, but I've never tried it.

    The result of the above code will write that snippet of javascript to the page that is using the ajax to call this file. So on that page, the javascript can do whatever it wants. On the page with the php, however, it is really just treated as html, not as a scripting language.


Bookmarks

Posting Permissions

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