Categories: javascript, php, jquery, html, ajax

Upload PHP file into HTML table using Ajax

2 answers

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. I have the following HTML code in the body section(including what is relevant):

<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):

<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. 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. The names in the current ul are just placeholders. However, all that loads into the page is the placeholder names, and the code to get the csv has no effect. Can anybody help me out with this?

All answers to this question, which has the identifier 61177816

The best answer:

Why have JS at all? I'll assume for now that there is only a bunch of names in your .csv file.

<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> 

Try: ajax code

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

php code

$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

$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

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data