Run PHP script with ajax

Run PHP script with ajax

xsanchezxsanchez Posts: 1Questions: 1Answers: 0

Hello, I would like to pass data to a php script with a button. I'm trying this code. I'm new to programming. Not working...

I click the button and nothing happens. The Alert (cellData) works.

 $(document).ready(function() {
    var events = $('#events');

    $('#Table1').on( 'click', 'td', function () {
    cellData = table.cell( this ).data();
     // ... do something with `cellData`
                                                 } );
    var table = $('#Table1').DataTable( {   
    "paging":   true,
    dom: 'Bfrtip',  
    "ordering": true,
    "AutoWidth": true,
     responsive: true,
     "processing": true,
     "serverSide": true,
     "ajax": "getData2_copy2.php",
      select: true,
    "language": {"url": "//cdn.datatables.net/plug-ins/1.10.20/i18n/French.json"},
      buttons:  [
      {
            text: 'Télécharger certificat',
            action: function (e, dt, node, config) {
                var listeId = table.rows( { selected: true } ).data().pluck(0).toArray();
                events.prepend( '<div>'+listeId+'</div>' ); 
                listeId='('+listeId+')';
                //alert(listeId);
                window.open('rapport_smtp.php?listeId='+listeId);           
            },

      },                    
        {
            text: 'Classer dans SyGED',
            action: function ( e, dt, node, config ) {
    //      alert (cellData); 
            $('#Table1').DataTable({
                retrieve: true,
                paging: false,
                searching: false,
            ajax: {
            url: 'rapport_smtp_copy.php',
            type: 'GET',
            dataType: "json",
            data: (cellData),

                  },
                                    });
                                                     },
        },
              ],
    "columnDefs": [
  { "width": "8px", "targets": 0 },
  { "width": "8px", "targets": 1 },
  { "width": "5px", "targets": 2 },
  { "width": "10px", "targets": 3 },
  { "width": "16px", "targets": 4 },
  { "width": "16px", "targets": 5 },
  { "width": "16px", "targets": 6 },
  { "width": "75px", "targets": 7 }
],
        });
   }); 
$(document).ready(function() { $("#display").click(function() { $.ajax({ //create an ajax request to display.php type: "GET", url: "getData2_copy2.php", dataType: "html", //expect html to be returned success: function(response){ $("#Table1").html(response); //alert(response); } }); }); });

HTML/PHP
<?php session_start(); if (!$_SESSION['loggedIn']){ header('Location: index.html'); } ?>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=n">
<meta http-equiv="Content-type" content="text/html">

<?php include 'JAVASCRIPT_CDN.html' ; ?>
<?php include 'CSS_CDN.html' ; ?>

  <style>
      <?php include 'template.css' ; ?>
      <?php include 'sidebar.css' ; ?>
      <?php include 'navbar.css' ; ?>
  </style>

</head>
<body class="holy-grail">

<div class="holy-grail-body">

    <section class="holy-grail-content">
        <!-- Main page content -->

<?php include 'buttonsupperright.html' ; ?>
<br><br>
<br>

Id Transmission Immeuble Unité Prenom Nom Date transmission Date ouverture Type de document
    </section>

    <div class="holy-grail-sidebar-1 hg-sidebar">
        <!-- sidebar 1 content -->             
       <?php include 'sidebar.html' ; ?>
    </div>

</div>

<footer>
    <!-- footer content -->
</footer>

</body>
</html>

Answers

  • allanallan Posts: 51,440Questions: 1Answers: 7,758 Site admin

    If I'm reading that code correctly (its late on Saturday night, my built in Javascript interpreter might not be fully working!) you are attempting to create a second DataTable inside another one. Which in turn is created inside a click event handler. And all three share the same ID (Table1) - which isn't going to work.

    If you are new to programming an jQuery, I'd suggest checking out a few tutorials such as this one. For general programming questions, you are best asking on StackOverflow.

    Allan

Sign In or Register to comment.