Echo variable from JS to HTML / PHP

Echo variable from JS to HTML / PHP

hyklhykl Posts: 48Questions: 19Answers: 5

I want to ask how to take the value of the variable id from Ajax datatables and show value in PHP or HTML? For example: I datatables ajax and everything works without problem using JS file. But I just want to take the value of the JS file in HTML or PHP? It is possible and ask for help.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    You want to send a value (variable) to the server on Ajax data load requests? If so, use ajax.data. If I've misunderstood, could you clarify your question please.

    Allan

  • hyklhykl Posts: 48Questions: 19Answers: 5

    No, I want get value from ajax in file JS and in file PHP i want echo print value, you understand? Or I have to paint for idea?

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    I'm afraid I still don't understand. Could you please link to an example of what you want to do.

    Allan

  • hyklhykl Posts: 48Questions: 19Answers: 5
    edited March 2016

    OK, I write now for understand:

    <?php
        include( "../php/DataTables.php" );
        use
            DataTables\Editor,
            DataTables\Editor\Field,
            DataTables\Editor\Format,
            DataTables\Editor\Mjoin,
            DataTables\Editor\Upload,
            DataTables\Editor\Validate,
            DataTables\Database,
            DataTables\Database\Query,
            DataTables\Database\Result;
    
            $RAW_SQL_QUERY="SELECT COUNT(id) FROM tablename;
            $result_raw = $db ->sql($RAW_SQL_QUERY)->fetchAll();
    
            ->process($_POST)
            ->json();
    
    

    And how get value from $result_raw for echo print in file PHP?

    file index.php:

    <!DOCTYPE html>
    ...
    <script type="text/javascript" language="javascript" src="../editor/js/dataTables.editor.js"></script>
            <script type="text/javascript" language="javascript" class="init">           
                $(document).ready(function() {
                    editor = new $.fn.dataTable.Editor({
                        ajax: "../editor/examples/php/staff.php",
                        table: "#example",
                    })
                });
    </script>
    <body>
    <?php echo $result_raw; ?>
    </body>
    </html>
    

    Why variable $result_raw does not echo? Problem?

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin
    Answer ✓

    You have two different scripts there:

    1. The Editor PHP which is requested when Editor makes edits.
    2. The index.php file that currently has an undefined variable in it.

    The two scripts are entirely independent - you can't use a variable from one in the other, without linking them.

    I would suggest you simply do something like:

    ```html
    <!DOCTYPE html>
    ...

    <script type="text/javascript" language="javascript" class="init">
    $(document).ready(function() {
    editor = new $.fn.dataTable.Editor({
    ajax: "../editor/examples/php/staff.php",
    table: "#example",
    })
    });
    </script>
    <body>
    <?php
    $RAW_SQL_QUERY="SELECT COUNT(id) FROM tablename;
    $result_raw = $db ->sql($RAW_SQL_QUERY)->fetchAll();

         echo $result_raw;
    
    <?php > ``` ?>

    Note: THIS WILL NOT WORK WITHOUT EDITING! You are using the Editor $db library, which you can do, but you would need to include the DataTables.php file in your index.php file. Alternatively, make your own MySQL database connection. Any PHP tutorial will show you how to do that.

    Allan

  • hyklhykl Posts: 48Questions: 19Answers: 5

    This is resolved :-)

    Thank you

This discussion has been closed.