Pass variable from .js to .php not working

Pass variable from .js to .php not working

RpgccvRpgccv Posts: 10Questions: 3Answers: 0

Here is my "table.something.js'

                    var test = datatest;
                    $.ajax({
                        url: 'php/table.something.php',
                        type: 'POST',
                        data: ({
                            test: test
                        }),
                        success: function (data) {

                        }
                    });

And here is my "table.something.php'

$idNumber = $_POST['test'];

// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'table' )
    ->fields(
        Field::inst( 'column_id' ),
        Field::inst( 'text' ),
        Field::inst( 'registertime' ),
        Field::inst( 'changedtime' )
    )
    ->where('table.column_id', $idNumber)
    ->process( $_POST )
    ->json();

Why is not passing the variable!?
If i change to $idNumber = 1 or any other number, it work great.
Any suggestion!?
Thanks ;-)

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Assuming that datatest is set to be 1, then it looks like $idNumber should also be 1.

    If you have a link to the page showing the issue I'd be happy to take a look.

    Allan

  • RpgccvRpgccv Posts: 10Questions: 3Answers: 0
    edited June 2018

    That is correct, datatest is set to be 1 or any number, but nothing works.
    This is the error:
    "DataTables warning: table id=table_id - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1"

    Am i putting the variable $idNumber = $_POST['test']; in the right place!?

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    What is the server returning if it isn't valid JSON? Is there an error message in there saying what is going wrong? If the response is empty, you'll need to check your server's error logs.

    Allan

  • RpgccvRpgccv Posts: 10Questions: 3Answers: 0

    on the error's log the error is "...Undefined index: test..."
    I can pass that variable to any other .php file, just not this one or any other alike from the "Datatables" so there is a problem there.

    I see on this forum more people with the same problem and they didn´t solve it...

    Can you, please, show me something like this working!?

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    This shows your Ajax request works fine, and I don't see anything wrong with your PHP code. As such, can you link to the page you are having a problem with, and also show me your full PHP code so I can offer some help?

    Allan

  • RpgccvRpgccv Posts: 10Questions: 3Answers: 0

    Thanks for your help, but your example is not helping...
    i´m saying that i thing the problem is on datatables - table.something.php, since i can send any variable with ajax anywhere else, but not to that unique file.

    This is all php code:

    <?php
    
    // DataTables PHP library and database connection
    include( "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;
    
        $idNumber = $_POST['test'];
     
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'table' )
        ->fields(
            Field::inst( 'column_id' ),
            Field::inst( 'text' ),
            Field::inst( 'registertime' ),
            Field::inst( 'changedtime' )
        )
        ->where('table.column_id', $idNumber)
        ->process( $_POST )
        ->json();
    
    
  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    I wouldn't bother assigning the $idNumber variable - just use:

    ->where('table.column_id', $_POST['test'])
    

    I can't see any reason why that wouldn't work with the Ajax request from my post above - I can see in the Chrome inspector that the test parameter is being sent.

    Try doing print_r( $POST ); in your PHP. It will cause a JSON parsing error on the client-side, but that doesn't really matter for a quick check - use the Network inspector in your browser to check what the response from the server is.

    Allan

  • RpgccvRpgccv Posts: 10Questions: 3Answers: 0
    edited June 2018

    The same...That is correct; I'm sure the test is being sent... the problem is that it's not received from that file.
    With you change,... still the same.
    I'll post something online so you can access and see!! Can i contact you directly!?

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Yes - you can drop me a PM by clicking my name above and then the "Send message" button.

    Allan

  • silvior92silvior92 Posts: 3Questions: 1Answers: 0

    Try to use use
    $_REQUEST instead of $_POST

    in the php file. If thats not working try this

    var test = datatest;
    url: 'php/table.something.php?test=' + test,
    

    instead of data parameter

This discussion has been closed.