首页 >后端开发 >php教程 >js和php嵌套

js和php嵌套

jacklove
jacklove原创
2018-06-15 16:59:356407浏览

        JavaScript和PHP可以相互嵌套,前提是JavaScript写在以.php为后缀的文件里面

       (1) JavaScript嵌套PHP:

        直接在JavaScript里面写入PHP语句,如:

<script type="text/javascript">
  BUI.use(&#39;bui/grid&#39;,function (Grid) {
    var data = [//表单内容信息
            <?php
		foreach($slice_rows as $key=>$val){
		echo "{id:&#39;".$val[&#39;id&#39;]."&#39;,imgpath:&#39;".$val[&#39;imgpath&#39;]."&#39;,introduction:&#39;".$val[&#39;introduction&#39;].
               "&#39;,linkpath:&#39;".$val[&#39;linkpath&#39;]."&#39;,date:&#39;".$val[&#39;date&#39;]."&#39;},";
	    }
	
	       echo "{id:&#39;1112&#39;,imgpath:&#39;李四&#39;,introduction:&#39;this is a test&#39;,linkpath:&#39;#&#39;,date:&#39;date2&#39;}";
	   ?>
	],
 
        grid = new Grid.SimpleGrid({
          render : &#39;#grid&#39;, //显示Grid到此处
          width : 950,      //设置宽度
          columns : [//表单头部信息
            {title:&#39;编号&#39;,dataIndex:&#39;id&#39;,width:80},
            {title:&#39;图片路径&#39;,dataIndex:&#39;imgpath&#39;,width:100},
			{title:&#39;简介&#39;,dataIndex:&#39;introduction&#39;,width:200},
			{title:&#39;链接地址&#39;,dataIndex:&#39;linkpath&#39;,width:100},
            {title:&#39;上传日期&#39;,dataIndex:&#39;date&#39;,width:100}
          ]
        });
      grid.render();
      grid.showData(data);
  });
  
</script>

        (2)PHP嵌套JavaScript

<?php
		
	echo &#39;<script type="text/javascript">&#39;;
	echo " BUI.use(&#39;bui/grid&#39;,function (Grid) {";
	//表单的数据内容
	echo &#39;var data = [&#39;;
	foreach($slice_rows as $key=>$val){
		echo "{id:&#39;".$val[&#39;id&#39;]."&#39;,imgpath:&#39;".$val[&#39;imgpath&#39;]."&#39;,introduction:&#39;".$val[&#39;introduction&#39;].
                     "&#39;,linkpath:&#39;".$val[&#39;linkpath&#39;]."&#39;,date:&#39;".$val[&#39;date&#39;]."&#39;},";
	}
	
	echo "{id:&#39;1112&#39;,imgpath:&#39;李四&#39;,introduction:&#39;1349622209547&#39;,linkpath:&#39;#&#39;,date:&#39;date2&#39;}";
	
	echo &#39;],&#39;;
	//表单的头部信息
	echo "
	 grid = new Grid.SimpleGrid({
          render : &#39;#grid&#39;, //显示Grid到此处
          width : 950,      //设置宽度
          columns : [
            {title:&#39;编号&#39;,dataIndex:&#39;id&#39;,width:80},
            {title:&#39;图片路径&#39;,dataIndex:&#39;imgpath&#39;,width:100},
			{title:&#39;简介&#39;,dataIndex:&#39;introduction&#39;,width:200},
			{title:&#39;链接地址&#39;,dataIndex:&#39;linkpath&#39;,width:100},
            {title:&#39;上传日期&#39;,dataIndex:&#39;date&#39;,width:100}
          ]
        });
	
	";
	echo &#39;  grid.render();
			grid.showData(data);
			});
  &#39;;
	
	
	//echo &#39;alert("this is a test")&#39;;
	echo &#39;</script>&#39;;
	
?>

本文讲解了js和php嵌套,更多相关内容请关注php中文网。
相关推荐:

js阻止默认事件与js阻止事件冒泡示例分享 js阻止冒泡事件

js函数常见的写法以及调用方法

原生JS的AJAX读取json全过程

以上是js和php嵌套的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn