Alexander Gelbukh
Alexander Gelbukh

Reputation: 2230

Edit file on server from a webpage

What I want to do is to have a set of editable Excel files on my webpage:

What I have considered:

Or what is the best / simplest way to achieve this?

(I know how to generate Excel files and how to open them from the webpage; my problem is how to get the user's edits back to the server transparently for the user.)

Upvotes: 2

Views: 5455

Answers (1)

Jonathan Lam
Jonathan Lam

Reputation: 17351

I think the easiest way to do this ("get the user's edits back to the server transparently for the user") is to use AJAX (JS) requests to PHP scripts.

AJAX is great for doing things in the background (asynchronously), but it can't edit the server. Just add an event listener in JS (an onchange or onblur, perhaps) and send an AJAX request every time the user edits the file.

PHP is a great server-side scripting language, and you can edit files with it.


EDIT: Example (on request)

Assuming that the Excel file is stored in a string in a <textarea> for simplicity (for now), you can set a listener to get the data from it (in jQuery), and send an AJAX request:

HTML:

<textarea id="excel"></textarea>

JS:

$("#excel").change(function() {
    var excelFile = $(this).val();
    $.ajax({
        url: "updateFile.php",
        method: "post",
        data: { data: excelFile }
    });
});

PHP (updateFile.php):

<?php
    $data = $_POST["data"];
    $file = fopen("FILENAME.xlsx", "w+");
    fwrite($file, $data);
    fclose($file);
?>

Upvotes: 1

Related Questions