如何将php变量传递给jquery

时间:2016-06-20 14:49:00

标签: javascript php jquery

我尝试将php变量传递给jquery,我尝试使用

 但是当我尝试使用

时,它并没有取得成功

的dataSource: insted的

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 }
 ]

它无法显示第二个选择,但我发现$ data与原始数据相同

我的代码,

<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>

我不知道我的代码有什么问题,请帮我解决问题,anyhelp将不胜感激!谢谢!

2 个答案:

答案 0 :(得分:1)

你忘了echo你的php变量

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

另外,既然你想要它以json格式,你也必须解析它,因此你必须用双引号括起你的属性名。

你的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 }
]';

dataSource将是:

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

它的工作:)

 Its working :)

答案 1 :(得分:0)

这里的问题是你试图直接将字符串解析为JSON,这样你就会得到“新行”以及输出字符串中转义的其他字符,你可以在这个测试中看到:

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

但是,你可以实现你想要将格式正确的数组传递给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"}]
*/

编辑:@ mohammad-mudassir指出你还需要使用JSON.parse将字符串解析回JSON。

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