Using ajax to call php and return multiple variables?

后端 未结 5 847
既然无缘
既然无缘 2021-02-04 22:30

I am trying to use javascript to call a php script which then will return multiple variables back to my javascript so I can manipulate them.

This is my JS.



        
5条回答
  •  暖寄归人
    2021-02-04 22:50

    You can return as many variables as you want with json_encode().

    Try in your PHP:

    
    

    You can add to that array , $num3, $num4, ... and so on.

    In your JS, you can access each number as follows.

    First, you will need this line of code to parse the encoded JSON string, in your success function.

    var result = $.parseJSON(output);

    That sets result as a JSON object. Now you can access all fields within result:

    • result[0] -- $num1 in PHP
    • result[1] -- $num2 in PHP

提交回复
热议问题