Can't send data to server via ajax

Can't send data to server via ajax

azonekz@gmail.comazonekz@gmail.com Posts: 32Questions: 9Answers: 1

mytable = $("#Programme1").DataTable
({
dom:"Blfrtip",
ajax: "php/demandes.php",
type: 'POST',
data: {
prog:1
},

On a server side:

$p=$_POST['prog'];

Error is:
Notice: Undefined index: prog in C:\xampp\htdocs\MyPHPproject\php\demandes.php on line 22

Can you help me please?
Thanks

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,277Questions: 26Answers: 4,765

    Are you trying to combine the url, type and data in your ajax config, similar to this example?
    https://datatables.net/reference/option/ajax.data#Examples

    If so then you need to change your syntax to something like this:

    dom:"Blfrtip",
    ajax: {
      url: "php/demandes.php",
      type: 'POST',
      data: {
        prog:1
      }
    }
    
    

    Kevin

  • azonekz@gmail.comazonekz@gmail.com Posts: 32Questions: 9Answers: 1

    Thanks for reponse,
    I've tryed it, but have the same error

  • kthorngrenkthorngren Posts: 20,277Questions: 26Answers: 4,765

    Error is:
    Notice: Undefined index: prog in C:\xampp\htdocs\MyPHPproject\php\demandes.php on line 22

    What is line 22 of demandes.php?

    Maybe you can provide more of your demandes.php code so we can see what is leading up to line 22.

    Kevin

  • azonekz@gmail.comazonekz@gmail.com Posts: 32Questions: 9Answers: 1
    <?php
    // DataTables PHP library
    include( "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;
    
    $p=$_GET['prog'];
    Editor::inst($db, 'demandes','id')
        ->debug(true)
        ->field(
            Field::inst('eleves.nom' ),
            Field::inst('eleves.pnom' ),
            Field::inst('eleves.date_naiss'),
            Field::inst('demandes.rang')
                ->validator( 'Validate::numeric' ),
            Field::inst('demandes.etat'),
            Field::inst('demandes.approb'),
            Field::inst('eleves.email'),
            Field::inst('demandes.annee')
                ->validator( 'Validate::dateFormat', array(
                    "format"  => "Y",
                    "message" => "Entrez une date dans le format yyyy"
                ) ),
            Field::inst('demandes.ele_id')
                ->options( Options::inst()
                    ->table( 'eleves' )
                    ->value( 'id' )
                    ->label( 'nom' )
                ),
            Field::inst('demandes.prog_id')
                ->options( Options::inst()
                    ->table( 'programmes' )
                    ->value( 'id' )
                    ->label( 'nom' )
                )
    
        )
        ->join(
            Mjoin::inst( 'files' )
                ->link( 'demandes.id', 'users_files.demandes_id' )
                ->link( 'files.id', 'users_files.file_id' )
                ->fields(
    
                    Field::inst( 'id' )
                        ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/upload/__ID__.__EXTN__' )
                            ->db( 'files', 'id', array(
                                'filename'    => Upload::DB_FILE_NAME,
                                'filesize'    => Upload::DB_FILE_SIZE,
                                'web_path'    => Upload::DB_WEB_PATH,
                                'system_path' => Upload::DB_SYSTEM_PATH
                            ) )
                            ->validator( function ( $file ) {
                                return$file['size'] >= 500000 ?
                                    "Files must be smaller than 500K" :
                                    null;
                            } )
                            ->allowedExtensions( array( 'pdf', '.pdf' ), "Veuillez télécharger un fichier PDF" )
                        )
                )
        )
        ->on( 'preRemove', function ( $editor, $id, $values ) {
            // On remove, the sequence needs to be updated to decrement all rows
            // beyond the deleted row. Get the current reading order by id (don't
            // use the submitted value in case of a multi-row delete).
            $order = $editor->db()
                ->select( 'demandes', 'rang', array('id' => $id) )
                ->fetch();
    
            $editor->db()
                ->query( 'update', 'demandes' )
                ->set( 'rang', 'rang-1', false )
                ->where( 'rang', $order['rang'], '>' )
                ->exec();
        } )
        ->leftJoin('eleves','eleves.id','=','demandes.ele_id')
        ->leftJoin('programmes','programmes.id','=','demandes.prog_id')
        ->where('demandes.prog_id',$p,'=')
        ->process($_POST)
        ->json();
    
  • azonekz@gmail.comazonekz@gmail.com Posts: 32Questions: 9Answers: 1

    now the error is:
    Notice: Undefined index: prog in C:\xampp\htdocs\MyPHPproject\php\demandes.php on line 14

  • azonekz@gmail.comazonekz@gmail.com Posts: 32Questions: 9Answers: 1
    <?php
    // DataTables PHP library
    include( "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;
    
    $p=$_POST['prog'];
    
  • allanallan Posts: 61,669Questions: 1Answers: 10,096 Site admin
    edited June 2017 Answer ✓

    Does error occur when you are submitting edited data, or when you are loading the DataTable? If the former (which I am guessing is the case) you need to use ajax.data in the Editor options to set the data being sent to the server as well as doing it for the DataTable.

    Allan

  • azonekz@gmail.comazonekz@gmail.com Posts: 32Questions: 9Answers: 1

    The error occur when I'm loading the DataTable

  • azonekz@gmail.comazonekz@gmail.com Posts: 32Questions: 9Answers: 1

    Yes, it'working great with ajax.data, thank you!!!

This discussion has been closed.