在javascript文件中使用php var错误[重复]

This question already has an answer here:

I have a question about using a php variable in my javascript file. This is my index.php file:

<body>
    <div class="container">
        <div class="row">
            <div class="span12">
                <?php
                    if(isset($_GET['appid'])) {
                        // GET appid
                        $appid = $_GET['appid'];

                        $json_url  ='http://api.url.com/api/gateway/call/1.4/getApp?appid=' . $appid;

                        $ch = curl_init($json_url);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                        $str = curl_exec($ch);
                        curl_close($ch);

                        $data = json_decode($str);
                        $array = json_encode($data);
                    }  

                    else{

                    }    
                ?>
                <p id="errors" class="text-error"></p>
            </div>
        </div>

        <hr>

    </div> <!-- /container -->

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>')</script>

    <script src="js/vendor/bootstrap.min.js"></script>

    <script src="js/plugins.js"></script>
    <script src="js/main.js.php"></script>
</body>

As you can see I check if an appid is sent. When I received it I load data from an api. In the bottom of the page I have a link to my javascript file. (.php because I want to use the php var $ array in my js file)

My main.js.php:

$(document).ready(function() {
    var data = <?php echo $array;?>;
    alert(data);
});

But I got always error in console:

Uncaught SyntaxError: Unexpected token <

Does anyone know what I do wrong?

</div>
dqgg25493
dqgg25493 我可以从.js.php文件中的api加载我的数据吗?
7 年多之前 回复
donglao7947
donglao7947 索引文件中的$array变量在main.js.php文件中不可用。
7 年多之前 回复
duanpo1498
duanpo1498 它是main.js.php的完整来源吗?您在此处粘贴的代码在index.php中设置$array并尝试在main.js.php中使用它?
7 年多之前 回复

4个回答



您正在一个完全不同的文件中创建数组!</ strong>这两个变量不在同一范围内。 更重要的是,Javascript文件显然不被解释为PHP(也不应该)。 所以:</ p>


  1. Javascript抱怨&lt;?</ code>标记,它永远不会看到它。</ li>
  2. Even 如果你解决了这个问题,它将无法工作,因为 main.js.php </ code>中没有PHP $ array </ code>变量。</ li>
    </ ol>

    首先要了解如何解释Javascript和PHP,请参阅参考:为什么我的Javascript中的PHP代码不起作用?。</ p>
    </ div>

展开原文

原文

You are creating the array in a completely different file! The two variables are not in the same scope. What's more, the Javascript file is apparently not interpreted as PHP (and neither should it). So:

  1. Javascript complains about the <? tag, which it should never see.
  2. Even if you solved that, it won't work since there's no PHP $array variable in main.js.php.

Start by understanding how Javascript and PHP are interpreted, see Reference: Why does the PHP code in my Javascript not work?.



由于您没有使用PHP include </ code>函数,因此您的变量位于不同的范围内。 这是我知道实现您想要的最简单的方法:</ p>


  • 将您的JavaScript文件重命名为 main.js </ code> </ li>
    < li>

    由于您无法在 .js </ code>文件中使用PHP,因此在调用脚本之前声明JavaScript变量,如下所示:</ p>

       &lt; script type =“text / javascript”&gt; 
    var array ='&lt;?php echo $ array?&gt;';
    &lt; / script&gt;
    &lt; script src =“js / main.js”&gt ;&lt; / script&gt;
    </ code> </ pre> </ li>
  • 然后,在 main.js </ code>文件中,只需替换您发布的代码 通过这个:</ p>

      $(document).ready(function(){
    var data = array;
    alert(data);
    });
    < / code> </ pre> </ li>
    </ ul>
    </ div>

展开原文

原文

Your variable is in a different scope since you're not using the PHP include function. Here's the easiest way I know to achieve what you want:

  • Rename your JavaScript file to main.js
  • Since you cannot use PHP in a .js file, declare your JavaScript variable before you call your script, like this:

    <script type="text/javascript">
        var array = '<?php echo $array ?>';
    </script>
    <script src="js/main.js"></script>
    
  • Then, in your main.js file, just replace the code you posted by this:

    $(document).ready(function() {
        var data = array;
        alert(data);
    });
    



试试这个,</ p>

  $(document).ready(function(){

var data ='&lt;?php echo isset($ array)?$ array:
json_encode(array(“array data”中没有“));?&gt;';
//如果$ array未设置则 它应该返回{“0”:“数组数据中没有”}
alert(数据);
});
</ code> </ pre>
</ div>

展开原文

原文

Try this,

$(document).ready(function() {
    var data = '<?php echo isset($array) ? $array : 
                    json_encode(array("nothing in array data"));?>';
    // if $array not set then it should return {"0":"nothing in array data"}
    alert(data);
});

duanlaican1849
duanlaican1849 从页面中删除卷曲代码,然后检查是否出现错误,然后javascript中没有错误。
7 年多之前 回复
dongya6395
dongya6395 我仍然得到错误:Uncaught SyntaxError:意外的令牌<
7 年多之前 回复



您忘记引用var数据的值使用此</ p>

  var data ='  &lt;?php echo $ array;?&gt;'; 
</ code> </ pre>
</ div>

展开原文

原文

you forgot to Quote Value of var data use this

var data = '<?php echo $array;?>';

立即提问