使用AJAX或PHP将数据写入文本文件中的特定位置

问题描述:

我正在创建一个网站,该网站利用非常复杂的表格结构和文本文件作为数据库。现在我有一个页面,将包含所有提供给用户的数据,它看起来像这样 - >使用AJAX或PHP将数据写入文本文件中的特定位置

all_data

<html> 
    <head> 
     <!-- custom css --> 
     <link rel="stylesheet" type="text/css" href="tables.css"> 
     <!-- bootstrap CDN --> 
     <!-- Latest compiled and minified CSS --> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
     <!-- Optional theme --> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> 
     <!-- Latest compiled and minified JavaScript --> 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 
      <!-- where all the data is stored --> 
      <script type="text/javascript" src="data.js"></script> 
      <!-- where the functions that fill the table are stored --> 
      <script type="text/javascript" src="index.js"></script> 

    </head> 
    <body> 
     <ul id="nav_bar"> 
      <li><a href="prepared_data.html">PREPARED</a></li> 
      <li><a href="known_data.html">KNOWN</a></li> 
      <li><a href="all_data.html">ALL</a></li> 
      <li><a href="guide.html">GUIDE</a></li> 
     </ul> 
     <div id="scroll_box"> 
      <table id="container"> 

       <!-- this will access index.js and utilize the functions in there --> 
       <script type="text/javascript"> 
        document.getElementById("container").innerHTML = getDataSections(all_data); 
       </script> 
      </table> 
     </div> 
    </body> 
</html> 

这是连接到两个JS文件,一个包含所有数据在这样的格式 - >

var all_data = [ 
    { 
    name: "data0", 
    items : [ 
     { 
     name: "bernard", 
     job: "accountant", 
     description: "a nice family man" 
     }, 
     { 
     name: "susan", 
     job: "developer", 
     description: "a genius" 
     } 
    ] 
    }, 
    { 
    name: "data1", 
    items : [ 
     { 
     name: "David", 
     job: "Boss", 
     description: "loves corn on the cob" 
     }, 
     { 
     name: "Erica", 
     job: "CEO", 
     description: "classified" 
     } 
    ] 
    } 
] 

,而另一种此数据并创建相应的HTML看起来像这样 - >

function getDataSections(data) { 
    // loop through data 
    var HTML = "" 

    for (var i = 0, i_end = data.length; i < i_end; i++) { 
    var category = data[i] 

    // only make a section if there are any data there 
    if (category.items.length > 0) { 

     // adds the section title 
     HTML += '<thead class="sticky" align="center"><tr><th>' + category.name + '</th></tr></thead>' 
     // add category label 
     HTML += "<tbody><tr><td><table class='data_container'>" 

     // loop through category items and build HTML 
     // go to getDataInfo, run that, then proceed 
     for (var j = 0, j_end = category.items.length; j < j_end; j++) { 
     HTML += getDataInfo(category.items[j]) 
     } 

     // close category table 
     HTML += "</table></td></tr></tbody>" 
    } 
    } 

    return HTML 
} 



function getDataInfo(item) { 
    // opening row tag 
    var HTML = "<tr>" 

    // add item information 
    HTML += "<td><table class='table-bordered Data_shorthand'>" 
    HTML += "<tr><td>Name</td><td>" + item.name + "</td></tr>" 
    HTML += "<tr><td>Job</td><td>" + item.job + "</td></tr>" 
    HTML += "</table></td>" 

    // add description 
    HTML += "<td class='description'>" + item.description + "</td>" 
    HTML += "<td><div class='btn-group'><button class='add'>Add</button></div></td>" 
    // closing row tag 
    HTML += "</tr>" 

    return HTML 
} 

与所有的说法。我想为每个这些“添加”按钮添加一个EventActionListener,这样当它们被点击时,它们将获取特定于其行的信息,并将其添加到与上面显示的文件相似的文件中,但是是为要添加到列表中的人员指定的。这样,我可以使用相同类型的方法,并相应地加载该页面。

我在想,我可以使用j计数器变量为一个唯一的ID添加到每个引用J索引文件的按钮,然后将数据写入到另一个文件的data0data1节集群。有没有办法让我使用AJAX做到这一点?或者也许一些PHP?任何意见将不胜感激,谢谢。

为什么要提前创建html页面?您可以创建每种类型的一个页面并使用动态给出的数据。这样,您无需搜索文件中的某个页面或数据,只需与数据库进行通信即可获取每个操作的数据。