Receive text typed in input text, put in variable and apply in link

Asked

Viewed 1,571 times

0

I want to receive the text in a field typed by the user and, when clicking on Ubmit, it will be generated a link with the text and sent to Whatsapp as in the code below:

<form action="https://api.whatsapp.com/send?phone='.$telefone.'&text='.$texto.'" method="POST" target="_blank">
    <input type="text" name="text" placeholder="Digite sua mensagem..." autocomplete="off">
    <button class="trwpwhatsappsendbutton" name="enviar" type="submit">
        <i class="fa fa-paper-plane-o"></i>
    </button>
</form>

How can I do that?

2 answers

0

In this code nothing will occur, since this action is totally wrong. action should call a POST API, but without passing parameters with PHP name on it.

What you want to do would be more or less:

<?php    
if(isset($_POST['telefone'])){
   $telefone = $_POST['telefone'];
   $texto    = $_POST['texto'];

   $resposta = file_get_contents("https://api.whatsapp.com/send?phone=" . $telefone . "&text=" . $texto);

   echo $resposta;
}    
?>

<html>
   <body>    
      <form action="" method="post">
          <?php echo $resposta; ?>
          <input type="text" name="telefone"/>
          <input type="text" name="texto"/>
          <input type="submit" name="SubmitButton"/>
      </form>    
   </body>
</html>

Even so, it is not recommended. It is recommended to make use of jQuery, with each code in separate files, in the form of:

HTML ---> FORM ---> jQuery ---> PHP ---> WhatsApp API

You can even directly call the Whatsapp API from the form, assuming:

<form action="https://api.whatsapp.com/send/" method="POST" target="_blank">
    <input type="text" name="text" placeholder="Digite sua mensagem..." autocomplete="off">
    <input type="text" name="phone" placeholder="Insira o telefone (com o +55)..." autocomplete="off">
    <button class="trwpwhatsappsendbutton" name="enviar" type="submit">
        <i class="fa fa-paper-plane-o"></i>
    </button>
</form>

In this way, text and phone would be sent to the API.

  • 1

    I tested it the way you did, but you don’t pull the information. The Whatsapp number should be hidden because it will not be changed by the user and I am using api.Whatsapp and web.Whatsapp for the link to work on both mobile and desktop.

0

Just manipulate the event submit of the form and make the necessary amendments.

$(function() {
  $('#send-message-whatsapp').on('click', 'button', function(e) {
    e.preventDefault();

    const $form = $("#send-message-whatsapp");
    const phone = '5512999999999';
    const text = $form.children('input[name="text"]').val();
    const action = "https://api.whatsapp.com/send?phone=" + phone + "&text=" + text;

    $form.attr('action', action);
    $form.attr('target', '_blank');
    $form.submit();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form id="send-message-whatsapp">

  <input type="text" name="text" placeholder="Digite sua mensagem..." autocomplete="off">

  <button class="trwpwhatsappsendbutton" name="enviar" type="submit">Enviar</button>

</form>

  • Victo, how do I get the link to change depending on the user’s device? I have a php code that the link changes depending on the device, whether web or mobile. How do I apply this in JS? Web link would be: web.Whatsapp Mobile link would be: api.Whatsapp

  • @Eduardo, you render HTML into a file .php?

  • Yes, everything is in index.php, only css and js are in a separate folder

  • Is the link stored in a variable or is it return of some function? You can put this code in the question?

  • He stays in JS: //ENVIA AS MENSAGENS&#xA; $send.on('click', () => {&#xA; if ($txtArea.val() !== '') {&#xA; let msg = $txtArea.val();&#xA; let fone = 999999999999; //DEFINE PARA QUAL NÚMERO VAI AS MENSAGENS&#xA; let url = https://web.whatsapp.com/send?phone=${fone}&text=${msg};&#xA;&#xA; window.open(url);&#xA; $txtArea.val('');&#xA; }&#xA; });

  • 1

    Check out this solution: https://stackoverflow.com/questions/2928827/access-php-var-from-external-javascript-file

  • Interesting, I’m trying to use this code, but when I rename input[name="text"] to another name it doesn’t work...

Show 2 more comments

Browser other questions tagged

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