简体   繁体   English

如何将php变量传递给jquery

[英]how to pass php variable to jquery

i try to pass php variable to jquery ,i have try to use 我尝试将php变量传递给jquery,我尝试使用

But it's not working success, when i try to use 但是,当我尝试使用它时,它并不成功

dataSource: insted dataSource:已插入

dataSource: [
  { childName: "Child1", childId: 1, parentId: 1 },
  { childName: "Child2", childId: 2, parentId: 2 },
  { childName: "Child3", childId: 3, parentId: 1 },
  { childName: "Child4", childId: 4, parentId: 2 }
 ]

it's cannot show the second select ,but i find the $data is the same as the original data 它不能显示第二个选择,但我发现$ data与原始数据相同

my code , 我的代码

<head>
 <meta charset="utf-8"/>
 <title>Kendo UI Snippet</title>

 <link rel="stylesheet"     href="http://kendo.cdn.telerik.com/2016.2.607/styles/kendo.common.min.css"/>
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.2.607/styles/kendo.rtl.min.css"/>
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.2.607/styles/kendo.silver.min.css"/>
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.2.607/styles/kendo.mobile.all.min.css"/>

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.2.607/js/kendo.all.min.js">        
</script>
</head>
<body>

 <input id="parent" />
 <input id="child" />
 <?php 
 $data = '[
  { childName: "Child1", childId: 1, parentId: 1 },
  { childName: "Child2", childId: 2, parentId: 2 },
  { childName: "Child3", childId: 3, parentId: 1 },
  { childName: "Child4", childId: 4, parentId: 2 }
 ]';
?>
<script>
$("#parent").kendoDropDownList({
  dataTextField: "parentName",
  dataValueField: "parentId",
  dataSource: [
  { parentName: "Parent1", parentId: 1 },
 { parentName: "Parent2", parentId: 2 }
   ]
  });

 $("#child").kendoDropDownList({
cascadeFrom: "parent",
dataTextField: "childName",
dataValueField: "childId",

dataSource: <?php json_encode($data); ?>
});
</script>

 </body>
</html>

i don't know what's problem with my code,please help me to solve the problem , anyhelp will be appreciated ! 我不知道我的代码有什么问题,请帮助我解决问题,任何帮助将不胜感激! thanks! 谢谢!

You forget to echo your php variable 您忘记echo您的php变量

<?php echo json_encode($data); ?>

plus, since you want it in json format, you have to parse it too and because of that you have to enclose your property name in double quotes. 另外,由于您希望使用json格式,因此也必须对其进行解析,因此必须将属性名称括在双引号中。

your php variable will become: 您的php变量将变为:

$data = '[
    { "childName": "Child1", "childId": 1, "parentId": 1 },
    { "childName": "Child2", "childId": 2, "parentId": 2 },
    { "childName": "Child3", "childId": 3, "parentId": 1 },
    { "childName": "Child4", "childId": 4, "parentId": 2 }
]';

and dataSource will be: dataSource将是:

dataSource: JSON.parse(<?php echo json_encode($data); ?>)

Its working :) 它的工作:)

它的工作:)

The problem here is that you are trying to parse a string directly to JSON and by doing so you will get also the "new lines" and other characters escaped on the output string as you can see in this test: 这里的问题是,您试图将字符串直接解析为JSON,这样您还将在输出字符串上获得“换行”和其他转义的字符,如在此测试中所见:

http://sandbox.onlinephpfunctions.com/code/3c31cecddd99aee0562d09c84b9a8e5770c3444b http://sandbox.onlinephpfunctions.com/code/3c31cecddd99aee0562d09c84b9a8e5770c3444b

However you could achieve the output that you want passing a properly formatted array to the json_encode function instead of a string like so: 但是,您可以实现想要将正确格式的数组而不是像这样的字符串传递给json_encode函数的输出:

$data_array = array(
    array('childName' => "Child1",
          'childId' => "1",
          'parentId' => "1"),
    array('childName' => "Child2",
          'childId' => "2",
          'parentId' => "3"),
    array('childName' => "Child3",
          'childId' => "3",
          'parentId' => "3"),
    array('childName' => "Child4",
          'childId' => "4",
          'parentId' => "4"),          
);

echo json_encode($data_array);

/*
Output:
[{"childName":"Child1","childId":"1","parentId":"1"},
{"childName":"Child2","childId":"2","parentId":"3"},
{"childName":"Child3","childId":"3","parentId":"3"},
{"childName":"Child4","childId":"4","parentId":"4"}]
*/

Edit: as @mohammad-mudassir noted you also need to use JSON.parse to parse the string back to JSON. 编辑:正如@ mohammad-mudassir指出的那样,您还需要使用JSON.parse将字符串解析回JSON。

dataSource: JSON.parse(<?php echo json_encode($data_array); ?>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM