2

I'm trying to import CSV data to MySQL over Ajax for an email program. Everything works good for 2000 Ajax requests but then the browser/server gives failures.

I tried several approaches, but I can't find a solution.

My code:

<?php
$zeilen = file('uploads/'.$dateipfad);

$gesamtzeilen = count($zeilen);

?>
<script type="text/javascript">
datenzeilen = []; <? php foreach($zeilen as $zeile) { ?>
        datenzeilen.push( <? php echo json_encode($zeile); ?> ); <? php
} ?>
</script>
<script type="text/javascript">
for (var i = 0; i < datenzeilen.length; i++) {
    var aktuelledaten = datenzeilen[i];
    $.ajax({
        type: "POST",
        url: "import.php",
        data: {
            trennzeichen: <? php echo '"'.$trennzeichen.
            '"'; ?> ,
            empfaengerliste: <? php echo '"'.$empfaengerliste.
            '"'; ?> ,
            aktuelle_daten: aktuelledaten,
            csvfelder: <? php echo '"'.implode(",", $csv_names).
            '"'; ?>
        },
        success: function (strResponse) {
            var gesamt = <? php echo $gesamtzeilen; ?> ;
            document.getElementById("gesamtanzahl").innerHTML = gesamt;
        }
    });
}
</script>
random
  • 9,774
  • 10
  • 66
  • 83
Mann87
  • 314
  • 1
  • 4
  • 14
  • In other words, your script contains over 2000 lines such as `datenzeilen.push(...)`? – punund Dec 25 '13 at 12:45
  • yes in my testfile there are 20.000 lines. – Mann87 Dec 25 '13 at 12:50
  • 1
    I suggest that you redesign your application altogether. What you're doing is fundamentally wrong. – punund Dec 25 '13 at 12:54
  • Ok why this? I know my programming knowledge is not perfect. What is a better way? – Mann87 Dec 25 '13 at 12:56
  • 5
    You application generates 20000 POST requests in very quick succession, stressing the webserver to its limit, this is probably why it stops responding. Try importing all the data in one call, not line by line. – punund Dec 25 '13 at 13:01
  • Yes but with the one by one line solution i get the import status. So I can see that for example 1000 from 20000 lines are imported. – Mann87 Dec 25 '13 at 14:57
  • Is there a way to start a new ajax request after one is completed? For example put the "i++" into the completed area? – Mann87 Dec 25 '13 at 19:12
  • 1
    As punund said, it's much, much better to upload the CSV file itself and parse it server-side. Searching for "ajax file upload" and "php csv" should give you all the info you need. This may also help: http://stackoverflow.com/questions/3901495/what-is-the-best-way-of-showing-progress-on-an-ajax-call – Curtis Mattoon Dec 25 '13 at 20:06
  • I agree with other comments, you must redesign your apps. Or else you could use "async:false" in your $ajax request so the post go one by one to your server. But it's not the best way to do this. – Sayris Dec 26 '13 at 10:39

0 Answers0