Can anyone do this with PHP without jquery?

Asked

Viewed 62 times

0

I need to click the add button and put values inside inputs in the 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>
<!DOCTYPE html>
    <html>
    <head>
        <title>Test</title>
        <script src="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>

  • Please edit the code to get inside a code block. Anything, just select all and click ctrl+k

  • Thanks I need to click the add button and put values inside inputs in php

  • But you’re still using Jquery... I need to use pure php

  • you know that PHP is a purely server language, right? So it is not possible to write a front-end event without using Javascript

3 answers

0


Just submit the form also the previous information, so you add the new and gives a echo. This will require a new field:

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

This way PHP will know what existed before, without any Jquery or Javascript. In addition it is necessary to note the use of id which must 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
    }
}
?>
  • Exactly that... Thank you

  • But look at the error...

  • @fabiobarros, what mistake?

  • He’s not writing on the screen

  • Which version of PHP are you using? If it is an unsupported version (< 5.6) it might not work. I could not play any error.

  • is mine is 5.2 I will update

  • updated pro 7.1.9 your example will but what I created did not

  • This is because you are forgetting to specify one of the elements, vise 'descricao' => $_POST['quantidade'] (in $historico[]). The $dados['quantidade'] will never exist, because he’s named after descrição, as well as the descricao will never be used.

  • I changed and nothing he no longer error appears what was written

  • now it worked ...

Show 5 more comments

0

If I understand correctly, you want to put the values listing them down right?

Well, if so, you will have to create a database (mysql), submit data and reload the page in one of the following ways: POST or by 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">";
}
?>

-2

$_POST['item'], 'description' => $_POST['description'], $_POST['quantity'], 'valorUni' => ['valorUni'], 'total value' => $_POST['total value']]; } >? Cadastro Produtos "> Item : Description : Quantity : Value Uni : Total Value:
  • If you format your code, it may have a better acceptance of the community

Browser other questions tagged

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