javascript - Ajax send FormData and retrieve multidimensional array -
alright have form select csv file , when hit sales_importer button java call php function , returned multidimensional array later processing. when run following code alert box multidimensional array of values seems in string form. when alert(result[0][0]);
[
in alert box.
i have tried changing datatype in ajax call json fails still 200 response browser. suggestions of might going on/how go fixing it?
js
$('body').on('click', '#sales_importer', function() { $.ajax({ type: 'post', data: new formdata($('form[id="import_form"]')[0]), cache: false, contenttype: false, processdata: false, url: admin_url+'clients/import', success: function(result){ alert(result); } }); });
php
public function import() { if ($this->input->is_ajax_request()) { if (isset($_files['client_file_csv']['name']) && $_files['client_file_csv']['name'] != '') { // temp file path $tmpfilepath = $_files['client_file_csv']['tmp_name']; // make sure have filepath if (!empty($tmpfilepath) && $tmpfilepath != '') { // setup our new file path $newfilepath = temp_folder . $_files['client_file_csv']['name']; if (!file_exists(temp_folder)) { mkdir(temp_folder, 777); } if (move_uploaded_file($tmpfilepath, $newfilepath)) { $import_result = true; $fd = fopen($newfilepath, 'r'); $rows = array(); while ($row = fgetcsv($fd)) { $rows[] = $row; } $data['total_rows_post'] = count($rows); fclose($fd); echo json_encode($rows); } unlink($newfilepath); } } } }
if want able use data in javascript, you're going need apply key. in php:
echo json_encode( array('rows' => $rows) );
this allows access rows in jquery ajax success function:
success: function(result){ // rows available result.rows if( result.rows ){ // result.rows... console.log( result.rows ); $.each( result.rows, function(i, arr){ console.log( arr ); }); } }
remember result.rows going javascript object, you'll need use jquery's each loop through rows , want do. use console view result.rows, , you'll see mean.
Comments
Post a Comment