get the elements of a row and show on a separate page in javascript, php, html

0

function detalle(item_id){

}
<thead>
            <tr>
                <th>Código</th>
                <th>Nombre</th>
                <th>Fabricante</th>
                <th>Tipo</th>
                <th><center>Gramos</center></th>
                <th><center>Precio</center></th>
                <th>
                    <center>Acciones</center>
                </th>
            </tr>
        </thead>

<?php foreach($items as $it): ?>
                    <tr>
                        <td><?= $it['item_code']; ?></td>
                        <td><?= $it['item_name']; ?></td>
                        <td><?= $it['item_brand']; ?></td>
                        <td><?= $it['item_type_desc']; ?></td>
                        <td><?= $it['item_grams']; ?></td>
                        <td><?= $it['item_price']; ?></td>
                        <td>
                           <center>
                               <button onclick="detalle('<?= $it['item_id']; ?>');" type="button" class="btn btn-warning btn-xs">Editar</button></center>
                       </td>
                   </tr>
<?php endforeach; ?>

I have that, a foreach that shows the items, stored in a database.

What I do not know, is how I do when I click on the detail button I send a new page showing the details of that row, clicked. I'm trying to do something inside the function detail in javascript but they have not worked, since I do not handle much javascript. I'm waiting for your help

    
asked by Hernelio Long 27.02.2018 в 20:47
source

1 answer

0

Since you have the information in PHP and you are going to show it in a Javascript function, you must first code it in JSON (PHP) and then decode it (Javascript).

The resulting code would be:

PHP

<thead>
            <tr>
                <th>Código</th>
                <th>Nombre</th>
                <th>Fabricante</th>
                <th>Tipo</th>
                <th><center>Gramos</center></th>
                <th><center>Precio</center></th>
                <th>
                    <center>Acciones</center>
                </th>
            </tr>
        </thead>

<?php foreach($items as $it): ?>
                    <tr>
                        <td><?= $it['item_code']; ?></td>
                        <td><?= $it['item_name']; ?></td>
                        <td><?= $it['item_brand']; ?></td>
                        <td><?= $it['item_type_desc']; ?></td>
                        <td><?= $it['item_grams']; ?></td>
                        <td><?= $it['item_price']; ?></td>
                        <td>
                           <center>
                               <button onclick="detalle('<?= json_encode($it); ?>');" type="button" class="btn btn-warning btn-xs">Editar</button></center>
                       </td>
                   </tr>
<?php endforeach; ?>

Javascript

function detalle(item){
    var obj = JSON.parse(item);
    var myWindow = window.open("", "Detalles");
    myWindow.document.write("<p>Nombre: " + obj.item_name + "</p><p>Precio: " + obj.item_price+ "</p>");
}
    
answered by 28.02.2018 / 12:34
source