Home > Backend Development > PHP Problem > How to use js with php

How to use js with php

Release: 2023-02-28 08:12:02
Original
5585 people have browsed it

How to use js with php

JavaScript and PHP can be nested in each other, provided that JavaScript is written in a file with the .php suffix.

(1) JavaScript nested PHP:

Write PHP statements directly in JavaScript, such as:

<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>
Copy after login

(2) PHP Nested 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;;
	
?>
Copy after login

Recommended: php server

The above is the detailed content of How to use js with php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template