Database column value in a hidden HTML field

Asked

Viewed 436 times

0

I want to take the value of the column and store it in a variable.

This code below works perfectly, however I now need to fill the value of the hidden field with the ID that comes from the bank:

"columns": [
   { "data": "cpf" },
   { "data": "nome" , },
   { "data": "sobrenome" , },
   { "data": "celular" },
   { "data": "email" },
   {               
   "data": null,
   "defaultContent": "<form method=\"post\" action=\"\"><input type=\"hidden\" id=\"idcontrib\" name=\"idcontrib\" value=\"  => "DATA": "ID" <=    \"><button type=\"submit\" class=\"btn btn-warning btn-xs\" name=\"edit_contrib\">Editar</button>&nbsp;&nbsp;<button type=\"submit\" class=\"btn btn-danger btn-xs\" name=\"exc_contrib\">Excluir</button></form>"
},

I have to make that part of the code:

value=\"  => "DATA": "ID" <=    \"

Be something like:

value=\"<?=$id?>\"

For this I have to make the value that comes from the bank be stored in the variable:

$id = "data" : "id";

Something like this is possible ?

HTML

<table id="datatable_fixed_column" class="table table-striped table-bordered" width="100%">
<thead>
  <tr>                                            
    <th>CPF</th>
    <th>Nome</th>
    <th>Sobrenome</th>
    <th>Celular</th>
    <th>email</th>
    <th>Ação</th>
  </tr>
</thead>                                    
</table>

javascript

<script type="text/javascript">

$(document).ready(function() {

    var otable = $('#datatable_fixed_column').DataTable({

    "processing": true,
    "serverSide": true,
    "ajax": "server_processing/contribuintes.php",

    "order": [[ 1, "asc" ]],


    "columns": [

        { "data": "cpf" },
        { "data": "nome" , },
        { "data": "sobrenome" , },
        { "data": "celular" },
        { "data": "email" },
        {                
        "data": null,

        "defaultContent": "<form method=\"post\" action=\"\"><input type=\"hidden\" id=\"idcontrib\" name=\"idcontrib\" value=\"<id>\"><button type=\"submit\" class=\"btn btn-warning btn-xs\" name=\"edit_contrib\">Editar</button>&nbsp;&nbsp;<button type=\"submit\" class=\"btn btn-danger btn-xs\" name=\"exc_contrib\">Excluir</button></form>"
        },

    ],


    "columnDefs": [

        { "width": "10%", "targets": 0 },
        { "width": "15%", "targets": 1 },
        { "width": "25%", "targets": 2 },
        { "width": "10%", "targets": 3 },
        { "width": "25%", "targets": 4 },
        { "width": "15%", "targets": 5 },


    ],

    "sDom": "<'dt-toolbar'<'col-xs-6'f><'col-xs-6'<'toolbar'>>r>"+
            "t"+
            "<'dt-toolbar-footer'<'col-xs-6'i><'col-xs-6'p>>"
    });

     $("div.toolbar").html('<div class="text-right"><form method="post" action=""><button type="submit" class="btn btn-success" name="add">ADICIONAR NOVO CONTRIBUINTE</button></form></div>');

})

Server-side script

$table = 'tbl_contribuintes';
$primaryKey = 'id_contrib';

$columns = array(

  array( 'db' => 'cpf', 'dt' => 'cpf' ),
  array( 'db' => 'fnome',  'dt' => 'nome' ),
  array( 'db' => 'lnome',  'dt' => 'sobrenome' ),   
  array( 'db' => 'celular1',  'dt' => 'celular' ),
  array( 'db' => 'email',  'dt' => 'email' ),
  array( 'db' => 'id_contrib', 'dt' => 'id' )

);

$sql_details = array(
  'user' => 'root',
  'pass' => 'XXXXX',
  'db'   => 'XXXXX',
  'host' => 'localhost'
);

require( 'ssp.class.php' );

echo json_encode(
  SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

Remembering that the data are being listed perfectly, just missing even this detail to take the value of the column and put in the variable.

Thank you very much.

  • Is this a JSON you generate in PHP? How are you doing to generate? You would need to include the ID before converting to JSON, but not knowing how/when you do it makes it difficult to answer.

  • My friend, I’m sorry, I’ve posted so much on this q ended up summarizing too much. I’ll edit the post for you to take a look

  • bjavaretto, aguaardando a help your or some illuminated

  • I don’t know this SSP class of yours (I don’t know what it does), nor did I understand when the data of the database comes in. I wouldn’t know how to help...

  • I’m actually using the DATATABLE component

1 answer

1


For someone with the same question, go the answer:

SERVER-SIDE

$table = 'tbl_contribuintes';
$primaryKey = 'id_contrib';

$columns = array(

    array( 'db' => 'id_contrib', 'dt' => 0 ),
    array( 'db' => 'cpf', 'dt' => 1 ),
    array( 'db' => 'fnome',  'dt' => 2 ),
    array( 'db' => 'lnome',  'dt' => 3 ),   
    array( 'db' => 'celular1',  'dt' => 4 ),
    array( 'db' => 'email',  'dt' => 5 ),
    array( 'db' => 'id_status',  'dt' => 6 ),

);

$sql_details = array(
  'user' => 'root',
  'pass' => 'XXX',
  'db'   => 'XXX',
  'host' => 'localhost'
);

require( 'ssp.class.php' );

echo json_encode(
  SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

TABLE

<table id="datatable_fixed_column" class="table table-striped table-bordered" width="100%">
      <thead>
        <tr>
          <th>ID</th>
          <th>CPF</th>
          <th>Nome</th>
          <th>Sobrenome</th>
          <th>Celular</th>
          <th>email</th>
          <th>status</th>
          <th>Ação</th>                                                                                     
       </tr>
    </thead>                                            
</table>

SCRIPT

$(document).ready(function() {

    var otable = $('#datatable_fixed_column').DataTable({

    "processing": true,
    "serverSide": true,
    "ajax": "server_processing/contribuintes.php",

    "order": [[ 2, "asc" ]],

    "columnDefs": [

        { "width": "5%", "targets": 0 },
        { "width": "10%", "targets": 1 },
        { "width": "15%", "targets": 2 },
        { "width": "20%", "targets": 3 },
        { "width": "10%", "targets": 4 },
        { "width": "20%", "targets": 5 },
        { "width": "5%", "targets": 6 },
        { "render": actionlinks,
          "data": null,         
          "targets": [7], "width": "15%", "targets": 7 },

    ],

    });

    function actionlinks(data, type, full) {

        return '<form method="post" action=""><input type="hidden" id="idcontrib" name="idcontrib" value="' + full[0] + '"><button type="submit" class="btn btn-warning btn-xs" name="edit_contrib">Editar</button>&nbsp;&nbsp;<button type="submit" class="btn btn-danger btn-xs" name="exc_contrib">Excluir</button>&nbsp;&nbsp;<button type="submit" class="btn btn-info btn-xs" name="ativa_contrib">Reativar</button></form> ';

    }

Acknowledgments: Anderson Torres

Browser other questions tagged

You are not signed in. Login or sign up in order to post.