dongwen2162 2014-03-18 13:12
浏览 35
已采纳

通过AJAX调用将PHP变量转换为jquery脚本文件

So basically i have two files. 1 is my php file and it creates tables with some variables when it's called, and second file is jquery script file that makes that call. My script file:

$.ajax({
                type: 'POST',
                data: ({p:2,ank : ankieta,wybrane:wybrane}),
                url: 'zestawienia_db.php',
                success: function(data) {

                     $('#results').html(data);
  }
        });

and it works fine by printing my results. My php file is echoing data that should be printed in my results div. Question is how to get some PHP data variables and be able to use them in my jquery file without actually echoing them ??

  • 写回答

4条回答 默认 最新

  • dongmu4591 2014-03-18 13:20
    关注

    Like i said in my comment to your question, a way to do that is by echoing the variables on a script tag, so you can access in javascript.

    <script>
    var PHPVariables;
    PHPVariables.VariableName1 = '<?=$phpVariableName1?>';
    PHPVariables.VariableName2 = '<?=$phpVariableName2?>';
    PHPVariables.VariableName3 = '<?=$phpVariableName2?>';
    </script>
    

    And you could use those values accessing PHPVariables.VariableName1 on the javascript.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?