简体   繁体   English

如何使用JSON转换和传输PHP变量数据到HTML页面的Javascript

[英]How to convert and transmit PHP Variable data Using JSON to HTML page's Javascript

I can't seem to figure it out how to convert the following PHP variable to HTML: 我似乎无法弄清楚如何将以下PHP变量转换为HTML:

$persondata = "<div id='teach'><h3>Name: " . $row["fname"]. "<br>User Name: " . $row["username"]. "</h3><p>Password: " . $row["upass"]. "</p></div><br>";

I wanted to pass that exact data to my HTML page, so that I can use JavaScript's getElementById function to insert it into a selected data field. 我想将确切的数据传递到HTML页面,以便可以使用JavaScript的getElementById函数将其插入到选定的数据字段中。

I asked a similar question here , which helped me work out some of logic I need, but I can't work out this part of the equation. 在这里提出了类似的问题,该问题帮助我确定了所需的一些逻辑,但我无法解决等式的这一部分。

If you could please let me know of a simple way of going about this, it would be very much appreciated, as I don't even know the keywords to search for. 如果可以的话,请让我知道一种简单的处理方法,将不胜感激,因为我什至不知道要搜索的关键字。

A javaScriptVar equals with an echo in php of your php variable. 一个javaScriptVar等于您的php变量在php中的回显。

That is to put php inside js 那就是将php放入js

<script type="text/javascript">
//Your JS

var jsvar = <?php echo $persondata; ?>

//Your JS
</script>

Put PHP var like above into js. 将上面的PHP var放入js。

Continuing from above answer: 从上述答案继续:

That is to put php inside js 那就是将php放入js

<script type="text/javascript">
//Your JS

var jsvar = <?php echo $persondata; ?>;

//Your JS
</script>

Put PHP var like above into js. 将上面的PHP var放入js。

If you want to transmit data from a PHP script in a server to a client side HTML page for using in js. 如果要将数据从服务器中的PHP脚本传输到客户端HTML页面以在js中使用。 Go like following. 像下面一样去。

  1. First get all your data in PHP in an array and use json_encode() and after that echo that variable. 首先在数组中使用PHP获取所有数据,然后使用json_encode() ,然后echo该变量。
  2. Now in the client side HTML use jQuery.ajax() and send request to that PHP file in server. 现在在客户端HTML中使用jQuery.ajax()并将请求发送到服务器中的该PHP文件。
  3. When you have the response in Json use js to fragment it and append wherever you want. 当您在Json中获得响应时,请使用js对其进行分段,然后将其附加到所需的任何位置。

Above is the basic procedure to send data from PHP to HTML page using JS. 上面是使用JS将数据从PHP发送到HTML页面的基本过程。

Hope below code help you: 希望下面的代码对您有所帮助:

<?php
$persondata = '';
foreach($rows as $row){
 $persondata .= "<div class='teach'><h3>Name: " . $row["fname"]. "<br>User Name: " . $row["username"]. "</h3><p>Password: " . $row["upass"]. "</p></div><br>";
}

?>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
      $(document).ready(function () {
         $('#data-row').html("<?php echo $persondata ?>");
      });
    </script>
</head>
<div id="data-row"></div>

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

相关问题 通过json数据结构将xlsx数据从php传输到javascript - transmit xlsx data from php to javascript through json data structure 如何读取JSON文件并传输到Z9784E91C7B26579789ZAF组织的HTML中的Javascript? - How to read JSON file and transmit to Javascript in HTML organized by Flask? 如何转换 json 并显示 html 中的所有数据? 使用 Javascript 和 Map - How to convert json and show all data in html ? using Javascript and Map 如何使用JavaScript / PHP将变量数据导出到服务器上的JSON文件? - How to export variable data to a JSON file on the server using JavaScript/PHP? 使用 JavaScript 将 JSON 数据显示到 HTML 页面 - Display JSON data to HTML page using JavaScript 如何使用PHP将HTML转换为JSON? - How to convert HTML to JSON using PHP? 如何使用 javascript 将 html 页面转换为 pdf - How to convert html page to a pdf using javascript 如何使用javascript将json数据从一个html页面传递到另一html页面? - How to pass json data from one html page to other html page using javascript? 如何更改数据图并将php变量从另一页转换为javascript - How to change data graph and convert a php variable to javascript from another page 如何使用JavaScript在数据表中显示JSON数据-PHP代码有效但不确定如何转换为JavaScript - How to display JSON data in a datatable using JavaScript - PHP code working but not sure how to convert to JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM