2017-08-14 18:16
浏览 121


Alright so I have a form where you select a CSV file and when you hit the sales_importer button I would like my java to call my php function and get the returned multidimensional array to then do later processing. When I run the following code I get the alert box with a multidimensional array of values but it seems like it is in string form. When I do alert(result[0][0]); I get [ in my alert box.

I have tried changing my dataType in my ajax call to json but then it just fails but I still get a 200 response from my browser. Any suggestions of what might be going on/how to go about fixing it?


$('body').on('click', '#sales_importer', function() {
        type: 'POST',
        data: new FormData($('form[id="import_form"]')[0]),
        cache: false,
        contentType: false,
        processData: false, 
        url: admin_url+'Clients/import',
        success: function(result){    


public function import()
    if ($this->input->is_ajax_request()) {
        if (isset($_FILES['client_file_csv']['name']) && $_FILES['client_file_csv']['name'] != '') {            
            // Get the temp file path
            $tmpFilePath = $_FILES['client_file_csv']['tmp_name'];
            // Make sure we have a 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);

                    echo json_encode($rows); 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyan4958 2017-08-14 19:02

    If you want to be able to use the data in javascript, then you're going to need to apply a key. So in your PHP:

    echo json_encode( array('rows' => $rows) );

    This now allows you to access the rows in your jQuery AJAX success function:

    success: function(result){    
        // rows available as result.rows
        if( result.rows ){
            // Do something with result.rows...
            console.log( result.rows );
            $.each( result.rows, function(i, arr){
                 console.log( arr );

    Remember that result.rows is going to be a javascript object, so you'll need to use jQuery's each to loop through the rows and do what you want to do. Use your console to view result.rows, and you'll see what I mean.

    打赏 评论

相关推荐 更多相似问题