4

the scenario needs to run like this: client uploads CSV file which then dynamically populates a table with the data from the uploaded CSV file.

i can upload the CSV file with a simple form. no problem. i can parse the data with PHP on the server side. no problem. i can create an array with PHP server side. no problem.

how the hell do i get it back to JavaScript for processing? i thought i could AJAX it, but where would i point the request?

i thought about using PHP to recursively call the page, but then it loses it asynchronicity, which is not okay.

seems like i'm missing something simple here.

TIA for your help.

WR!

2 Answers 2

3

The answer is simple. Upload the document asynchronously, and have that upload call return some JSON.

Alternatively (and better for larger documents), upload the document and have the server return an ID, and then call a script with AJAX with that ID. That way, you can do background processing on a large file, and return the data when ready, or return some sort of waiting status, where the client waits for 5 seconds or so and tries again.

Sign up to request clarification or add additional context in comments.

2 Comments

oh look! i waded into deep water and fell off the submerged cliff! O_o i didn't know JS could upload files to a server, i thought that was PHP's domain. oops. are you saying i abandon the PHP handling of the file & data? i marginally understand the JSON part. the ID...? what's that part about? totally lost me there.
well. your answer got me oriented properly anyway. for others looking for an answer: go here and follow the tutorial. i suspect this is what @Brad was suggesting i do.
0

If you want to go down the Ajax route, you should create a page (or route) that is specifically designed for getting just the CSV data and returning it as a json object. Lets's say you just need to know the id to generate the data, you could create csvdata.php that looks for $_POST['id'], gets the data for that id, and returns the json_encode(csvArray) data. You probably want to set the mime type header('Content-type: application/json'); This is no different than a script that provides files for example. All you need to know is what to request and what data to send with the request. If you need the CSV data on a page, you need to render a script that knows what data to send along with the request. Your templating engine should be able to render small dynamic scripts for you. In principle you want to generate a script like

...
<body>
...
<script type="text/javascript">
    (function () {
        var csvid = 1;
        // Make ajax post to csvid.php with csvid as id
    }());         
</script>
</body>

You would generate a script like this, where the csvid value is set dynamically (using your method of choice for generating html/js). Always use POST to retrieve Ajax data, particularly if the data is sensitive and requires session authentication. GET in this case would be open to json array exploits.

Alternatively you could simply render the array inline without Ajax. So instead of creating the ajax request variables, you can just render a script with the array encoded in it.

Template:

<script type="text/javascript">
    (function () {
        var data = {jsondata};
        someNameSpace.someProcessingFunction(data);
    }());
</script>

Replaces {jsondata} with json_encode(csvarray);

1 Comment

i've not yet committed to using a templating system. the only one i'm aware of is Smarty and i've not dove into that pool just yet. and i hate to say it, but i read the words and they just don't gel in my head. as for using JSON, that's likely the route i'll take. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.