0

I have a javascript function in a file comment_insert.js:

  function comment_insert(data,ul_id) {

        var t = '';
        t += '<li class="comment-holder" id="_' + data.comment_id + '">';
        t += '<div class="user-img">';
        t += '<img src="' + data.profile_img + '" class="user-img-pic" />';
        t += '</div>';
        t += '<div class="comment-body">';
        t += '<h3 class="username-field" >' + data.Username + '</h3>';
        t += '<div class="comment-text">' + data.comment + '</div>';
        t += '</div>';
        t += '<div class="comment-buttons-holder">';
        t += '<ul>';
        t += '<li class="delete-btn">[x]</li>';
        t += '</ul>';
        t += '</div>';
        t += '</li>';

        $('#' + ul_id).prepend(t);
    }

I want to call this function from a php file display.php as:

$smthng = new stdClass();
        $smthng->comment_id = 24;
        $smthng->Userid = 1;
        $smthng->comment = "Hard coded comments";
        $smthng->Username = "Sagar_username"; 
        //$smthng->profile_img = "images/Setting-icon.png";


        $data =  json_encode($smthng);
                $ul_id = "ul218";

 comment_insert(jQuery.parseJSON($data),$ul_id);

The function being called is the same as in comment_insert.js which accepts 2 inputs (data and ul_id). These are being created in display.php and then I want to execute the function.

4
  • PHP runs on the server, Javascript runs on the client. You can NOT call one from the other. At best you can do an AJAX call from client->server to invoke php code and get its output. Commented Apr 4, 2014 at 14:21
  • Could you provide me the ajax call that would solve this problem ? Commented Apr 4, 2014 at 14:22
  • 2
    I am so tired of the questions starting with the phrase Calling a Javascript function from PHP Commented Apr 4, 2014 at 14:23
  • possible duplicate of calling javascript function from php Commented Apr 4, 2014 at 14:24

2 Answers 2

1

Assuming that the PHP code is in the same file as Javascript and is being used for front end out put to the browser, you can do this:

<?php
$smthng = new stdClass();
        $smthng->comment_id = 24;
        $smthng->Userid = 1;
        $smthng->comment = "Hard coded comments";
        $smthng->Username = "Sagar_username"; 
        //$smthng->profile_img = "images/Setting-icon.png";


        $data =  json_encode($smthng);
                $ul_id = "ul218";
?>
<script type="text/javascript">
    comment_insert(jQuery.parseJSON(<?php echo $data ?>), <?php echo $ul_id ?>);
</script>

However, if this is not in a file that is producing output to the browser, you cannot execute Javascript server-side

Sign up to request clarification or add additional context in comments.

3 Comments

Variable creation and function call are to be executed in the same for{} loop which is in php code. Hence, I could not end my php code and start a <script> tag to call the function.
Then I refer you to the comments on your original question, Javascript cannot be executed server-side unless you do something akin to what madhippie is suggesting (using the V8js extension)
You can add script blocks between php blocks, even in a for loop. just end the php ?> write the script block, and open the again <?php
0

In order to execute Javascript in PHP you'll need to install the V8js Extension.

After installing you should parse your comment_insert.js to a PHP variable via file_get_contents and execute it somehow like this:

<?php

$v8 = new V8Js();

/* Get you js file's content */
$JS = file_get_contents("PATH/TO/JS-FILE/comment_insert.js");

try {
  $v8->executeString($JS, 'basic.js');
} catch (V8JsException $e) {
  var_dump($e);
}

?>

Note: In order to make it work, I think you'll replace the jQuery call with native JS.

So modify you comment_insert.js to something like this:

  function comment_insert(data,ul_id) {

    var t = '';
    t += '<li class="comment-holder" id="_' + data.comment_id + '">';
    t += '<div class="user-img">';
    t += '<img src="' + data.profile_img + '" class="user-img-pic" />';
    t += '</div>';
    t += '<div class="comment-body">';
    t += '<h3 class="username-field" >' + data.Username + '</h3>';
    t += '<div class="comment-text">' + data.comment + '</div>';
    t += '</div>';
    t += '<div class="comment-buttons-holder">';
    t += '<ul>';
    t += '<li class="delete-btn">[x]</li>';
    t += '</ul>';
    t += '</div>';
    t += '</li>';
  
    //$('#' + ul_id).prepend(t);
    elem = document.getElementById(ul_id);
    elem.insertBefore(t);  
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.