File upload: what am I doing wrong?

File upload: what am I doing wrong?

ostmalostmal Posts: 102Questions: 33Answers: 0

File upload: what am I doing wrong?
Hello!
I would like to make a photo upload and try to do as it is written here:
https://editor.datatables.net/examples/advanced/upload.html
https://editor.datatables.net/manual/php/upload#Overview
But I can't do it!

This is a link to the test page.

There are 2 tables: the main table "xxx_aaa_opponent "has the field"image_id". The "xxx_aaa_image" table has the following fields:
- id
- filename
- filesize
- web_path
- system_path
HTML:

<!doctype html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />

        <title>Оппоненты</title>

        <script type="text/javascript" charset="utf-8" src="/abc/dt/my/opponent/opponent.js"></script>
    </head>
    <body class="dataTables">
        <div class="container">


            <!-- Кнопка раскрытия/закрытия дочерних полей -->
            <hr>

            <table cellpadding="0" cellspacing="0" border="0" class="display table-bordered" id="id_opponent" width="100%">
            <!-- <table id="id_opponent" width="100%" class='table table-striped table-bordered'> -->
                <thead>
                    <tr>
                        <th>image</th>
                        <th>name</th>
                    </tr>
                </thead>
                <tfoot hidden >
                    <tr>
                        <th>image_id</th>
                        <th>name</th>
                    </tr>
                </tfoot>
            </table>

        </div>
    </body>
</html>

JS:

(function($){


$(document).ready(function() {
    var editor = new $.fn.dataTable.Editor( {
        ajax: {
            url: '/abc/dt/my/opponent/opponent.php',
            type: 'POST'
        },
        table: '#id_opponent',
        fields: [
            {
                label: "Name:",
                name: "name"
            },
            {
                label: "Image:",
                name: "image_id",
                type: "upload",
                display: function ( file_id ) {
                    return '<img src="'+editor.file( 'xxx_aaa_image', file_id ).web_path+'"/>';
                },
                clearText: "Clear",
                noImageText: 'No image'
            }
        ]
    } );


    var table = $('#id_opponent').DataTable( {
        dom: 'fBrltip',
        ajax: {
            url: '/abc/dt/my/opponent/opponent.php',
            type: 'POST'
        },
        columns: [
            {
                data: "image_id"
            },
            {
                data: "name"
            }
        ],
        select: true,   // Выделять строки по одиночному клику
        lengthChange: false,
        buttons: [
            { extend: 'create', editor: editor, text: 'Create' },
            { extend: 'edit',   editor: editor, text: 'Edit' },
            { extend: 'remove', editor: editor, text: 'Delete' }
        ]
    } );
} );
}(jQuery));

PHP:

<?php

// DataTables PHP library and database connection
include( "php/lib/DataTables.php" );

// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'xxx_aaa_opponent', 'id' )
    ->fields(
        Field::inst( 'name' ),
        Field::inst( 'image_id' )
            ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/upload/__ID__.__EXTN__' )
                ->db( 'xxx_aaa_image', 'xxx_aaa_image.id', array(
                    'filename'    => Upload::DB_FILE_NAME,
                    // 'filesize'    => Upload::DB_FILE_SIZE,
                    'web_path'    => Upload::DB_WEB_PATH,
                    'system_path' => Upload::DB_SYSTEM_PATH
                ) )
            )
    )
    ->process( $_POST )
    ->json();

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    Answer ✓

    I just took a look at your test page. "opponent.php" produces an error. "No such file or directory". You probably forgot to create the directory "upload" in "public_html". Can you try that first please. (Haven't looked at your code yet.)

  • ostmalostmal Posts: 102Questions: 33Answers: 0

    Thanks. I created a directory, but in the wrong place. Again, sorry to bother you. It's okay!!!

  • ostmalostmal Posts: 102Questions: 33Answers: 0

    Another question: I can't pass the parameter to the server: Test example.
    Everything worked out with uploading images, thank you.
    But I need to pass a parameter to the server.
    I had this:

    ajax: {
                url: '/abc/dt/my/test/test.php',
                type: 'POST'
                    },
    
    

    I did this (added parameter transmission):

    ajax: {
                url: '/abc/dt/my/test/test.php',
                type: 'POST',
                data: {
                    param : '1'
                }
            },
    

    The image stopped loading!!!

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited May 2020

    I am just using your example with Chrome:

    Editor crashes here:

    This is the error.

    But you can see the error message that would have been displayed if "Exception" had been defined:

    Upload feature cannot use ajax.data with an object. Please use it as a function instead.

    Don't get me wrong: We're here to help! But you could help yourself better and faster if you just checked the console of your browser for errors ...

    This is what you would need to use to follow the advice of the error message:

    ajax: {
        url: '/abc/dt/my/test/test.php',
        type: 'POST',
        data: function ( d ) {
            d.param = '1';
        }
    },
    

    In PHP you will then have the super global $_POST['param'] available.

    My advice would be to ALWAYS use ajax.data with a function. There is no disadvantage of doing this, it provides more flexibility and you don't run into trouble with UPLOAD. (Just checked my own code: I never use it without a function ... now I know why :smile: ).

  • ostmalostmal Posts: 102Questions: 33Answers: 0

    thank you very much for your help!!!

This discussion has been closed.