Save form attribute without name defined

Asked

Viewed 60 times

1

I have a problem with a php script. The name attribute of a page receives a value that is generated by an array. What I need to do is basically generate a table in which each row has a button that serves to delete the same information received from the database, this button is the one that uses the attribute name to save the id of the received record. So I don’t know what I put on the other php page to save this value. I’ll put the code below for further analysis.

The bottom php page that shows the elements on the screen.

<?php
        $query = sprintf("SELECT conteudo, autor, titulo, pagina, id FROM atualizacoes ORDER BY id DESC");

        $dados = mysql_query($query, $con) or die(mysql_error());

        $linha = mysql_fetch_assoc($dados);

        $total = mysql_num_rows($dados);
        ?>
                <body>
            <div class="row ">
                <div class="col-md-8 col-md-offset-2">
                    <form name="login" method="post" action="../removendo.php">
                <h2 style="color: white">Atualizando blog</h2>

            <table class="tabelaconteudo">

                        <?php
                        if($total>0){
                            do{

                                ?>
                                <tr>
    `<td><?= $linha['titulo']?></td>
    <td><?= $linha['pagina'] ?></td>
    <td><?= $linha['autor'] ?></td>
    <td><input type="submit" class="btn-cad" style="color: white" value="Apagar"
name="ref_<?= $linha['id'] ?>"></td> //AQUI É O CAMPO O QUAL NÃO CONSIGO SALVAR A INFORMAÇÃO
    </tr>


                            <?php

                            }while ($linha = mysql_fetch_assoc($dados));

                        }
                        ?>

            </table>
                    </form>

            </div>
            </div>
            </body>

Below is the php page that is loaded when you press the Submit button (same as I cannot define a specific name).

<?php
error_reporting (E_ALL & ~ E_NOTICE & ~ E_DEPRECATED);
    $id=$_POST['NAME QUE NAO ESTA DEFINIDO'];
    $restid = substr($id, 1, 4);
    $sql = mysql_query("DELETE FROM atualizacoes WHERE id = $restid");
    echo"
<script>
document.getElementById('loader').onpageshow = alertfunc();
function alertfunc() {
    alert('Ocorreu algum erro.'$sql);
    }
</script>

";
?>

I started with web programming a short time ago, and all I’ve learned from php so far is this. I’ve swept the entire internet behind it and never found anything concrete until now, then in despair I come here for help. I would like to thank all those who have been willing to help.

2 answers

0


<td>
  <form name="login" method="post" action="../removendo.php">
    <input type="hidden" value="Apagar" value="ref_<?= $linha['id'] ?>" name="campo_para_apagar" />
    <input type="submit" class="btn-cad" value="Apagar" name="submit" />
  </form>
</td>

If you only send these values at a time, one solution is to open one <form> different for each table row, with a hidden input that has its value. So when you click Delete it will send only the value of that input and it will be available in the request as

$id = $_POST['campo_para_apagar'];
  • Thank you very much I tested your solution and I managed to solve my problem!! Thank you very much!!! :)

0

You can create an array in the name attribute like this:

<input type="submit" class="btn-cad" style="color: white" value="Apagar"
name="ref[<?= $linha['id'] ?>]">

and in PHP get the ID like this:

$id = key($_POST['ref']);

Browser other questions tagged

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