简体   繁体   English

如何使用json读取html中的php数据

[英]How to read php data in html using json

I'm working on a project in which I want to read PHP data in HTML using JSON 我正在一个项目中,我想使用JSON读取HTML中的PHP数据

Here is my JavaScript code: 这是我的JavaScript代码:

var xmlhttp;
var jsonResponse = "";

window.onload = function() {
  alert('window.onload fired!');
  xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET", "../getproduct.php",true);
  alert('Constructed XMLHTTP request!');
  xmlhttp.send();
  xmlhttp.onreadystatechange = dataReturn;
  document.getElementById('getBtn').addEventListener('click', getData, true);
}

function dataReturn() {
  if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
     jsonResponse = xmlhttp.responseText;
     //jsonResponse = eval('(' + jsonResponse + ')');
     alert(jsonResponse);
  }
}

var div = document.getElementById('result');

function getData() {
  alert('getData()');
  // Parsing the txt JSON string
  obj = jsonResponse;
  document.getElementById('result').innerHTML = obj;
  txt1 = JSON.parse(obj);
  alert(txt1);
  employees = txt.products;

  // Looping through object employees
  for (i = 0; i < employees.length; i++) {
    var p = document.createElement('p');
    p.innerHTML = '<b>First name:</b>' + employees[i].menuid + ', <b>Last name: </b>' + employees[i].name;
    div.appendChild(p);
  }

  // Generate a new JSON string based on current JS object.
  jsonString = JSON.stringify(obj);
  alert(jsonString);
}

Using this code I am able to read PHP file as a json 使用此代码,我可以将PHP文件作为json读取

Here is the output of PHP file: 这是PHP文件的输出:

{
  "products": [
    {
      "menuid": "9",
      "name": "Cable Managementdsdsd",
      "description": "fgfgfg",
      "location":"CableTray.jpg"
    }
  ]
}

I am unable to read this JSON data in JavaScript. 我无法在JavaScript中读取此JSON数据。

You have a JSON response from a PHP page not reading PHP in JSON. 您有来自PHP页面的JSON响应,但未读取JSON中的PHP。 PHP gives a response and JSON is the format of that response. PHP提供了一个响应,而JSON是该响应的格式。 Similarly you now want to display the data in an HTML page you can't just read it in HTML. 同样,您现在要在HTML页面中显示数据,而不仅仅是在HTML中读取数据。 If you log your JSON response you will see it is a string which can be parsed into a javascript object containing an array of products and each product has menuid, name, description, location. 如果您记录JSON响应,则会看到它是一个字符串,可以将其解析为包含产品数组的javascript对象,并且每个产品都有menuid,名称,描述,位置。

An example of your json converted to a javascript object, this will read through all products that are returned: 将json转换为javascript对象的示例,该示例将读取返回的所有产品:

var json = '{"products":[{"menuid":"9","name":"Cable Managementdsdsd","description":"fgfgfg","location":"CableTray.jpg"}]}';

var jsobject= JSON.parse(json);

console.log(json);
console.log(jsobject);

for(var i = 0; i < jsobject.products.length; i++){
    console.log(jsobject.products[i].menuid);
    console.log(jsobject.products[i].name);
    console.log(jsobject.products[i].description);
    console.log(jsobject.products[i].location);
}

https://jsfiddle.net/o1ofnmrL/1/ https://jsfiddle.net/o1ofnmrL/1/

You can just replace the console.log lines with code to write onto the html page instead of console. 您只需将console.log行替换为要写入HTML页面(而不是console)的代码即可。

Here is an updated version that will print into a div (You will need to sort out formatting and things) https://jsfiddle.net/o1ofnmrL/3/ 这是一个更新的版本,它将打印到div中(您需要对格式和内容进行分类) https://jsfiddle.net/o1ofnmrL/3/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM