How to add different actions to the buttons generated in php

0

Good, You are trying to add an action to each button that is generated with a query, as you can see I do 3 actions for each items : modify | See | Approvals .

See PDF files in

How can I add one action to each button for each record ...

while ($extraido = mysqli_fetch_array($result))
{

if($extraido['status_final']==0)
{
  $vistos = '<img src="img/novisto.png" height="35" width="35">';
}
if($extraido['status_final']==1 || $extraido['status_final']==2)
{
  $vistos = '<img src="img/visto.png" height="35" width="35">';
}
if($extraido['status_final']==3)
{
  $vistos = '<img src="img/aprob.png" height="35" width="35">';
}
?>  
<tr>
<td><?php echo $extraido['id_prod'] ?></td>
<td><?php echo $extraido['nom_empre'] ?></td>
<td><?php echo $extraido['nom_prod'] ?></td>
<td><?php echo $extraido['f_creacion'] ?></td>
<td><?php echo $extraido['f_modificacion'] ?></td>
<td><?php echo $vistos ?></td>

<td><button class="btn btn-primary btn-sm" name="" id="">Modificar</button></td>
<td><button class="btn btn-default btn-sm" name="ver" id="">ver</button></td>
<td><button class="btn btn-default btn-sm" name="" id="">Aprobaciones</button></td>
</tr>

<?php
}
?>
    
asked by Eduardo Ramirez 18.01.2018 в 21:00
source

2 answers

2

Instead of a button, you could use a link shaped like a button. If you are using Bootstrap, it would be a tag like this:

<a class="btn btn-primary" href="#" role="button">Modificar</a>

And in the href attribute you put the page with the id in the query string :

    <a class="btn btn-primary" href="/modificar.php?id=<?=$extraido['id_prod']?>" role="button">Modificar</a>

And so you do not have to use javascript, just change the buttons for these tags:

<td> <a class="btn btn-primary" href="/modificar.php?id=<?=$extraido['id_prod']?>" role="button">Modificar</a></td>
//Produce un enlace estilo /modificar.php?id=1 por cada registro que muestres en la tabla
<td> <a class="btn btn-primary" href="/ver.php?id=<?=$extraido['id_prod']?>" role="button">Ver</a></td>
<td> <a class="btn btn-primary" href="/apro.php?id=<?=$extraido['id_prod']?>" role="button">Aprobaciones</a></td>

It's especially useful, if you just have to open another page. If you plan to perform more complex actions, you will have to use Javascript.

And then, in your file accion.php you can get the id through the variable $ _GET and show what you have to show.

<?php
   $prod_id = $_GET['id']; 
   //Ahora puedes usar $prod_id para buscar la información en la base de 
   //datos e imprimirlo en la página. 
?>
    
answered by 18.01.2018 / 23:09
source
0

From what I understand, you will have a list of pdf files, and you want to see, modify and approve and when you give one of those buttons perform that action for that pdf , in that case we would change a couple of things to speed up, it would look like this:

<tr id="documentos"> //le agrego un id a los tr para poder acceder al evento click dentro de el
<td><?php echo $extraido['id_prod'] ?></td>
<td><?php echo $extraido['nom_empre'] ?></td>
<td><?php echo $extraido['nom_prod'] ?></td>
<td><?php echo $extraido['f_creacion'] ?></td>
<td><?php echo $extraido['f_modificacion'] ?></td>
<td><?php echo $vistos ?></td>

//a cada boton le agrego como id el id_prod mas -mod si es modificar, -ver si es ver , y -apro si es aprobaciones
<td><button class="btn btn-primary btn-sm" name="" id="<?php $extraido['id_prod']."-mod" ?>">Modificar</button></td>
<td><button class="btn btn-default btn-sm" name="ver" id="<?php $extraido['id_prod']."-ver" ?>">ver</button></td>
<td><button class="btn btn-default btn-sm" name="" id="<?php $extraido['id_prod']."-apro" ?>">Aprobaciones</button></td>
</tr>

then with javascript, I manage the button presses, and I send the request to the server with the id of the product and the action, it would be like this:

//importando jquery
$('#documentos button').click(function(){
    var id_doc = $(this).prop('id').split('-')[0] // aqui capturo el id_prod que se selecciono la accion
    var ruta = '',accion='';

    if($(this).prop('id').split('-')[1] == 'ver'){ 
       ruta = '/ver',accion='ver'
    }else if($(this).prop('id').split('-')[1] == 'apro'){
       ruta = '/aproba',accion='aprobaciones'
    } else if($(this).prop('id').split('-')[1] == 'mod'){
       ruta = '/mod',accion='modificar'
    }
   if(accion == 'ver'){ //si la acción es ver redirijo a esa ruta sino hago la petición.
      location.href = '/visor.php?id_p' + id_doc // aqui cuando es accion ver envio a visor.php pasandole en la ruta en la variable id_p el id del documento    
   }else{

       $.ajax({
         url:ruta,
         type:'POST', // puede ser POST o GET , eso depende de lo que esperes en el servidor
         data:{ id_producto:id_doc, accion:accion}, // enviando al server por ejemplo, a la ruta /ver un json { id_producto:5698, accion: 'modificar' }
        }).done(function(data){ //el .done es por si envías datos del servidor luego de cumplir la petición , y si mandas información viene en data.
         console.log('Datos:',data)

       })
   }
})
    
answered by 18.01.2018 в 21:32