Does anyone know how to do this with PHP without jquery?

0

I need to click the add and put values inside inputs in PHP .

function InserirTelefone(){
  $('#telefones').append('<div>Nome: '+$('#nome').val()+' - Telefone: '+$('#telefone').val()+' </div>')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><!DOCTYPEhtml><html><head><title>Test</title><scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    </head>
    <body> 
    <form>
        <h2>Inserir Novo Telefone</h2>
        Nome: <input id="nome" type="text"/><br>
        Telefone: <input id="telefone" type="text"/><br>
        <input type="button" onclick="InserirTelefone()" value="Enviar" />
    </form>
    
    <div id="telefones">
        <h2>Telefones</h2>
    </div>
    </body>
    </html>
    
asked by anonymous 18.09.2017 / 16:46

3 answers

0

Just that form also sends the previous information, so you add the new one and give echo . This will require a new field:

<form>
  //..
  <input type="hidden" name="historico" value="HTMLENTITES(JSON({Valores Anteriores}))">
  //...
</form>

In this way PHP will know what existed before, without any JQuery or Javascript. Also note the use of id that should be changed to name , in order for the information to be sent to PHP.

<?php
// Se o primeiro acesso, ele terá um histórico vazio:
$historico = [];

if(isset($_POST['nome'], $_POST['telefone'], $_POST['historico'])){

    // Define o historico (os dados que já foram mostrados anteriormente)
    $json = json_decode($_POST['historico'], true);
    if(json_last_error() === JSON_ERROR_NONE){
        $historico = $json;
    }

    // Acrescentas os dados atuais no historico
    $historico[] = ['nome' => $_POST['nome'], 'telefone' => $_POST['telefone']];
}
?>

<form action="#" method="post">
    <h2>Inserir Novo Telefone</h2>

    <label>Nome: <input name="nome" type="text"/></label><br>
    <label>Telefone: <input name="telefone" type="text"/></label><br>

    <!-- Define o "historico" com todos os valores já inseridos: -->
    <input type="hidden" name="historico" value="<?= htmlentities(json_encode($historico), ENT_QUOTES, 'utf-8', false) ?>">
    <input type="submit" value="Enviar" />
</form>

<?php
// Repete por cada dado do "historico", tanto os antigos quanto os novos estão nessa variável.
foreach ($historico as $dados){
    if(isset($dados['nome'], $dados['telefone'])) {
        ?>
        <div>Nome: <?= htmlentities($dados['nome'], ENT_QUOTES, 'utf-8', false) ?> -
            Telefone: <?= htmlentities($dados['telefone'], ENT_QUOTES, 'utf-8', false) ?> </div>
        <?php
    }
}
?>
    
18.09.2017 / 17:16
0

If I understand correctly, you want to put the values listed below right?

Well, if so, you have to create a database ( mysql ), submit the data and reload the page in one of the following ways: Or POST or GET . After creating the database, the code would look like this:

<?php
      $db_select=mysqli_connect('Localização do site ou "127.0.0.1" se for local','nome de utilizador ou "root" se for local','palavra passe ou " " se for local','nome da base de dados');
?>
<form method="POST">
        <h2>Inserir Novo Telefone</h2>
        Nome: <input name="nome" type="text"/><br>
        Telefone: <input name="telefone" type="text"/><br>
        <input type="submit" name="sub" value="Enviar" />
    </form>

<div id="telefones">
   <h2>Telefones</h2><br/>
   <?php
   $select = mysqli_query($db, "SELECT * FROM nome_tabela");
   while($mostra=mysqli_fetch_assoc($select)){
      echo $mostra['nome']." - ".$mostra['telefone']. "<br/>;
   } ?>
</div>

<?php
if(isset($_POST['sub'])){
  $nome=$_POST['nome'];
  $telefone = $_POST['telefone'];
  $insere=mysqli_query($db_select, "INSERT INTO nome_tabela(nome, telefone) VALUES('$nome', '$telefone')");
  echo "<meta http-equiv="refresh" content="0">";
}
?>
    
18.09.2017 / 17:27
0
 $ _POST ['item'], 'description' = > $ _POST ['description'], $ _POST ['quantity'], 'valueUni' = > ['valueUni'], 'totalvalue' = > $ _POST ['totalvalue']];   } ? >     Product Registration                                                                                                                                                                                                                                                            ">                        Add to Compare          Description :          Amount :          Uni Value:          Amount:               
18.09.2017 / 22:40