How to include a html button from a php result

Asked

Viewed 180 times

-2

REPHRASED THE QUESTION!

I would like the help of you, how to create a button, to copy the result of the function echo from PHP. For example:

Code in PHP

echo "Nome: " . $row_usuario['nome'] . "<br>";

Show this to the user:

Nome: siclano da silva

How to create this button in HTML (it can be otherwise) to copy the output result of the function echo from the browser screen, where this copy will be to clipboard, as if it were CTRL + C / CTRL + V?

improving my question, this one is code in question.

            <label>Nome: </label>
            <input type="text" name="nome" placeholder="Digite aqui"><br><br>
            
            <input name="SendPesqUser" type="submit" value="Pesquisar">
        </center>
        </form><br><br>
        
        <form>
        
        <center>
        <div>
        <input type="button" value="cadastra novo solicitante de crédito consignado" onClick="Nova()">
        </div>
        <div>
        <input type="button" value="Consultar crédito" onClick="Novo2()">
        </div>
        <div>
        <input type="button" value="Página Inicial" onClick="NovoInicial()">
        </div>
        </center>
        </form> 
        
        
        <?php
        $SendPesqUser = filter_input(INPUT_POST, 'SendPesqUser', FILTER_SANITIZE_STRING);
        if($SendPesqUser){
            $nome = filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_STRING);
            $result_usuario = "SELECT * FROM fornecedores WHERE nome LIKE '%$nome%'";
            $resultado_usuario = mysqli_query($con, $result_usuario);
            while ($row_usuario = $resultado_usuario->fetch_assoc()) {
                
                
                echo "Nome: " . $row_usuario['nome'] . "<br>";
                echo "Endereço: " . $row_usuario['endereco'] . "<br>";
                echo "Cep: " . $row_usuario['cep'] . "<br>";
                echo "E-mail: " . $row_usuario['email'] . "<br>";
                echo "Celular: " . $row_usuario['celular'] . "<br>";
                echo "Valor do crédito: " . $row_usuario['valor'] . "<br>";
                echo "Id: " . $row_usuario['id'] . "<br>";
                
                
                echo "<a href='edit_usuario.php?id=" . $row_usuario['id'] . "'>Editar</a><br>";
                echo "<a href='proc_apagar_usuario.php?id=" . $row_usuario['id'] . "'>Apagar</a><br><hr>";
            }
        }
        ?>
    </body>
</html>

https://ibb.co/h7b7TRb

The result would be that as per the image of the link above, the person would do a search by database, then the result that appeared on the screen, would like it to be copied via Clipboard by a button, because the intention is for the person to copy this data and then to post in Whatsapp.

BS: I am a beginner in the field of programming, I apologize for not knowing yet how to express myself properly with my doubts, I deeply thank you for your help.

my level of knowledge is a lot of effort in wanting to learn and many things I may not yet understand.

  • In short, do you want when you click a button, run a PHP code and the return of that PHP code go to the user’s clipboard? Your question got a little confused

  • From what I understand he wants to click on a button to execute a javascript code that copies the content of some html element to clipboard.

2 answers

0

Good night Claudio, In this case you need to put inside a while and inside put some conditional, so whenever the conditional is met the button will appear. Ex:

$query = "SELECT * FROM TABLE_1 WHERE USER =". $user."" ; $Row =($query,$db)//query and database connection while($Row['user'] == ".$id."){ echo" View user//View a modal bootstap //Put a modal and do the select inside. "; }

}

  • i re-asked the question. including more elements, thank you for your comment.

0

From what I understand, you want to copy an html element to clipboard when performing the click action. If your application handles modern browsers, these browsers have a new api that supports the api clipboard.

To perform this action in a way that it is compatible with most browsers the ideal would be to make an implementation that is compatible with new and old browsers.

Assuming we have the html tag label with id='meuelemento' and the content saída and we want to copy the text saída to clipboard.

<label id="meuelemento">saida</label>
<button type="button" onclick="copyToClipboardAction()">Area transferência</button>


<script>
  function copyToClipboardAction() {
    const textoCopiar = document.getElementById("meuelemento")
    const txtToCopy = navigator.clipboard.writeText(textoCopiar.textContent)
}
</script>

In this case the value saida, will be copied to the clipboard.

Note that using this stream if your browser has no implementation of the Clipboard api (old browsers) an error will occur.

So the ideal way to treat this is to make a try...catch and if an error occurs we must make a special treatment to manipulate this case.

This stream is represented in the catch:

  1. Inserts a new input element into html
  2. We set an id to this element so that it can be found in html.
  3. we took this element by the id added in the previous step.
  4. add this element to the current html
  5. insert the value to be copied to the inserted html element
  6. insert the focus into the html input element to be inserted
  7. we select the text() of input.
  8. run the execCommand('copy') function, which is the command that copies values to clipboard.
  9. remove the element we entered, to make the html according to when we started modifying the html.

Follows the code:

<script>
function copyToClipboardAction() {
  const textoCopiar = document.getElementById("meuelemento")
  try {
    const txtToCopy = navigator.clipboard.writeText(textoCopiar.textContent)
  } catch (error) {
     const input = document.createElement('entradainput')
     input.setAttribute('id', 'elementoclipboard')
     const fakeReadOnlyElement = document.getElementById("meuelemento")
     const divpai = fakeReadOnlyElement.parentNode
     divpai.insertBefore(input, fakeReadOnlyElement)
     input.value = textToCopy
     input.focus()
     input.select()
     document.execCommand('copy')
     const elementoRemover = document.getElementById('entradainput')
     elementoRemover.remove()
  }
}
</script>

You can validate that this flow was successful if after implementing these changes you tighten CTRL+V the content of the variable meuelemento must be in the clipboard.

  • i repeated the question. including more elements, I would like to see the possibility of being reviewed again.

  • actually worked the script, but now my challenge is, copy, the information that is shown in ECHO. I sent a link with the image, where when I search a name X in the database, below returns what has, ex: in the search appeared the id 61 (besides the name, etc, and 69), I would like to make this script now copy this data.

  • Sorry, I don’t understand php. I can’t say.

Browser other questions tagged

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