Call Javascript function with parameters inside PHP

Malintha

I have a javascript function which has some parameters (an array). I want to call it inside php. Following is my code

 <script type="text/javascript">

<?php 

$task_list=array();
        foreach($tasks as $row2){


            $task_list=$row2;
        }

         echo "display_diagram(".$task_list.");";

        ?>

function display_diagram(var1){

     .........................
     .........................

 }

When I call it without any parameter it is working (I could displayed the content in javascript function. But when I give the parameter( it is an array it dose not working. Could you give this a solution?

quietmint

The usual practice here is to create a separate PHP file that outputs JSON data and fetch that data using an AJAX request. Below is an example of including JSON data inline, but I wouldn't typically recommend doing it like this, especially if the data is quite large.


Use json_encode() to convert a PHP variable into something that can be used in JavaScript. With an associative array, you'll get a JavaScript object like {"a":1,"b":2,"c":3,"d":4,"e":5}. With a non-associatve array, you'll get a JavaScript array like [1,2,3,4,5].

<script>
<?php
    $tasks = array(
        145 => array(
            'name' => 'Sorting Task',
            'owner' => 'user1'
        ),
        2343 => array(
            'name' => 'Processing Task',
            'owner' => 'user2'
        ),
        7266 => array(
            'name' => 'Another Task',
            'owner' => 'user1'
        ),
        8373 => array(
            'name' => 'Lorem Ipsum Task',
            'owner' => 'user3'
        )
    );
    echo 'display_diagram(' . json_encode($tasks) . ')';
?>

function display_diagram(tasks) {
    $.each(tasks, function (id, task) {
        console.log('Task #' + id + ': name=' + task.name + ', owner=' + task.owner);
    });
}
</script>

The JavaScript above uses jQuery to process the object. It should output the following in the JavaScript console:

Task #145: name=Sorting Task, owner=user1
Task #2343: name=Processing Task, owner=user2
Task #7266: name=Another Task, owner=user1
Task #8373: name=Lorem Ipsum Task, owner=user3

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

php function call parameters not passing

분류에서Dev

Use Angular in javascript call function parameters

분류에서Dev

Call function with parameters

분류에서Dev

javascript call inside php where loop not working and breaks query

분류에서Dev

Recursive call to a function inside a class

분류에서Dev

How to create a function with parameters and a loop inside in R?

분류에서Dev

C programming doubts- function call inside function call

분류에서Dev

Lua Gideros: Call function with multiple parameters

분류에서Dev

Template parameter deduction for parameters not used in function call

분류에서Dev

Allocating memory for a pointer inside a function call in C

분류에서Dev

Jquery ajax call inside success function

분류에서Dev

call isset from function inside class

분류에서Dev

Use php namespace inside function

분류에서Dev

How to call the Javascript file inside Body tag

분류에서Dev

Call Javascript function across pages

분류에서Dev

How to use php inside of javascript?

분류에서Dev

how to define a Function inside a Function in javascript?

분류에서Dev

Call a function that has parameter type inside another function c#

분류에서Dev

WordPress 및 PHP의 JavaScript Inside JavaScript

분류에서Dev

php in a javascript function not working

분류에서Dev

Invoke a Function with the Correct Parameters from an Object in JavaScript

분류에서Dev

Get the Variable name inside the function in javascript

분류에서Dev

Javascript If Statement inside while loop inside a function breaks page

분류에서Dev

call a function, inside a for loop, to remove elements in a vector (or list)

분류에서Dev

Is it bad practice to instantiate an object as an argument inside a method/function call?

분류에서Dev

How can I call a bash function in bash script inside awk?

분류에서Dev

Auto call a php function every 10 seconds

분류에서Dev

ajaxrequest call php function instead of get

분류에서Dev

ajaxrequest call php function instead of get

Related 관련 기사

  1. 1

    php function call parameters not passing

  2. 2

    Use Angular in javascript call function parameters

  3. 3

    Call function with parameters

  4. 4

    javascript call inside php where loop not working and breaks query

  5. 5

    Recursive call to a function inside a class

  6. 6

    How to create a function with parameters and a loop inside in R?

  7. 7

    C programming doubts- function call inside function call

  8. 8

    Lua Gideros: Call function with multiple parameters

  9. 9

    Template parameter deduction for parameters not used in function call

  10. 10

    Allocating memory for a pointer inside a function call in C

  11. 11

    Jquery ajax call inside success function

  12. 12

    call isset from function inside class

  13. 13

    Use php namespace inside function

  14. 14

    How to call the Javascript file inside Body tag

  15. 15

    Call Javascript function across pages

  16. 16

    How to use php inside of javascript?

  17. 17

    how to define a Function inside a Function in javascript?

  18. 18

    Call a function that has parameter type inside another function c#

  19. 19

    WordPress 및 PHP의 JavaScript Inside JavaScript

  20. 20

    php in a javascript function not working

  21. 21

    Invoke a Function with the Correct Parameters from an Object in JavaScript

  22. 22

    Get the Variable name inside the function in javascript

  23. 23

    Javascript If Statement inside while loop inside a function breaks page

  24. 24

    call a function, inside a for loop, to remove elements in a vector (or list)

  25. 25

    Is it bad practice to instantiate an object as an argument inside a method/function call?

  26. 26

    How can I call a bash function in bash script inside awk?

  27. 27

    Auto call a php function every 10 seconds

  28. 28

    ajaxrequest call php function instead of get

  29. 29

    ajaxrequest call php function instead of get

뜨겁다태그

보관