Rumah  >  Artikel  >  pembangunan bahagian belakang  >  js怎么跟php结合使用

js怎么跟php结合使用

尚
asal
2019-10-31 11:31:105495semak imbas

js怎么跟php结合使用

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

推荐:php服务器

Atas ialah kandungan terperinci js怎么跟php结合使用. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn