【问题标题】:how to move the table row data (HTML) from one php page to another如何将表格行数据(HTML)从一个 php 页面移动到另一个
【发布时间】:2018-01-04 12:57:20
【问题描述】:
 // searching the country to select(js file)
function search_country(){
  d3.csv("data/totalpopulation.csv", function(data) {
  var v=f1.search.value; 
    for(var i=0;i<data.length;i++){
      if(data[i].Name==v){
        document.getElementById('tablediv').style.visibility="visible";
        // retrun value
        document.getElementById('poptable').innerHTML+="<tr id='"+data[i].Name+"' class='ComparisonTable'  onclick='current_country(this.id)' style='cursor:pointer'><td>" + data[i].Name + "</td><td>" + (data[i].p1961/1000000).toFixed(2) + "</td><td>" + (data[i].p2016/1000000).toFixed(2) + "</td></tr>";
    }
  }
});
}

// Comparing the Selected Countries
function compare(){
window.location.href='country.php';
}

在这里,我从搜索栏中选择国家并放入 CompareTable。 此外,单击比较按钮时,我应该移动到下一个 php 文件(country.php)并携带相同的表格数据来比较它们。

 document.getElementById('poptable').innerHTML+="<tr id='"+data[i].Name+"' class='ComparisonTable'  onclick='current_country(this.id)' style='cursor:pointer'><td>" + data[i].Name + "</td><td>" + (data[i].p1961/1000000).toFixed(2) + "</td><td>" + (data[i].p2016/1000000).toFixed(2) + "</td></tr>";

【问题讨论】:

  • 请注意,如果您只想显示项目,您可能会这样做,否则最好通过服务器端页面获取数据的新副本,而不是将它们作为 可编辑 GET参数。

标签: javascript html


【解决方案1】:

使用 arrya 存储完整的 tr 值

var data_arr[];   
function search_country(){
d3.csv("data/totalpopulation.csv", function(data) {
var v=f1.search.value; 
for(var i=0;i<data.length;i++){
  if(data[i].Name==v){
    document.getElementById('tablediv').style.visibility="visible";
    // push value to an object.
    data_arr.push(document.getElementById('poptable').innerHTML+="<tr id='"+data[i].Name+"' class='ComparisonTable'  onclick='current_country(this.id)' style='cursor:pointer'><td>" + data[i].Name + "</td><td>" + (data[i].p1961/1000000).toFixed(2) + "</td><td>" + (data[i].p2016/1000000).toFixed(2) + "</td></tr>");
   }
  }
});
}

// Comparing the Selected Countries
function compare(){
data_arr_json = JSON.stringify(data_arr);
window.location.href='country.php?data='+data_arr_json;
}

当你想获取country.php中数组的数据时:

$data_from_url = json_decode($_GET['data']);

【讨论】:

  • 感谢您的解决,我明白了
  • 接受答案会很有帮助。
猜你喜欢
  • 2019-12-03
  • 2017-01-23
  • 1970-01-01
  • 1970-01-01
  • 2018-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-24
相关资源
最近更新 更多