简体   繁体   English

使用 Ajax 将 PHP 文件上传到 HTML 表中

[英]Upload PHP file into HTML table using Ajax

I am very new to HTML/javascript and I have seen a bunch of questions like this but I can't seem to make head of tail of the solution provided.我是 HTML/javascript 的新手,我见过很多这样的问题,但我似乎无法理解所提供的解决方案。 I have the following HTML code in the body section(including what is relevant):我在正文部分有以下 HTML 代码(包括相关内容):

<div>
    <ul id="playerNames">
        <li><b>Harden</b></li>
        <li><b>Giannis</b></li>
        <li><b>Lebron</b></li>
        <li><b>Booker</b></li>
        <li><b>Lavine</b></li>
        <li><b>Westbrook</b></li>
        <li><b>Jokic</b></li>
    </ul>
</div>

There is some javascript below (also in the body of the same file):下面有一些 javascript(也在同一文件的正文中):

<script type = "text/javascript">
    $(document).ready(function(){
        $( "playerNames" ).load( 'nba2019_names.php' );
    });
</script>

And I have the following php code in a separate file that it meant to load a csv file that I am downloaded.我在一个单独的文件中有以下 php 代码,它意味着加载我下载的 csv 文件。 All of these files are in the same folder.所有这些文件都在同一个文件夹中。

$h=fopen('2019_nba_player_names.csv','r');

$data = fgetcsv($h, 1000, ",");

while(($data = fgetcsv($h, 1000, ",")) !== FALSE){
    echo $row."\n";
}

What I am trying to do is get the information in the csv to become an html list, and then replace the html code/list that is already in the ul.我要做的是获取csv中的信息成为html列表,然后替换ul中已有的html代码/列表。 The names in the current ul are just placeholders.当前 ul 中的名称只是占位符。 However, all that loads into the page is the placeholder names, and the code to get the csv has no effect.但是,所有加载到页面中的都是占位符名称,获取 csv 的代码没有任何效果。 Can anybody help me out with this?有人可以帮我解决这个问题吗?

Try: ajax code尝试: ajax代码

$.ajax({
      method: GET
      url:"nba2019_names.php",
      success:function(res){
     $("#playerNames").html(res)
      }
    })

php code php 代码

$h=fopen('2019_nba_player_names.csv','r');

$data = fgetcsv($h, 1000, ",");

while(($data = fgetcsv($h, 1000, ",")) !== FALSE){
    echo "<li>$row</li>"."\n";
}

update : you can use this code to get data from csv file更新:您可以使用此代码从csv file中获取数据

$row = 1;
if (($handle = fopen("2019_nba_player_names", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo "<li>$data[$c]</li>";
        }
    }
    fclose($handle);
}

reference参考

Why have JS at all?为什么要有 JS? I'll assume for now that there is only a bunch of names in your.csv file.我现在假设您的 .csv 文件中只有一堆名字。

<div>
  <ul id="playerNames">
  <?
    $h=fopen('2019_nba_player_names.csv','r');
    while(($data = fgetcsv($h, 1000, ",")) !== FALSE){
      echo "<li>" . $data[0] . "</li>\n";
    }
  ?>
  </ul>
</div>

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

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