How to pick up value from a button without a form

Asked

Viewed 62 times

0

Hey, guys, I wanted to know how to pass a function to the button without a php form...

Ex: I click the Follow button, and start following the person.

And without going via get too, because it would look really bad if my site was full of '?key=3937''

I tried several ways but could not, and most of the topics I read needed ajax.

  • Exactly, Ajax!

  • ?key=3937 is a get method, you have to use an ajax method that is post.

  • 1

    https://api.jquery.com/jquery.ajax/ Here you can learn how to do it.

1 answer

0


Here is an example of an AJAX call, but your question is very incomplete, I suggest that in the next questions, study more on the subject try to develop alone and come up with more specific doubt that saves us from developing something for you.

Other than that the code is simple and in Javascript, it has some comments,.

In addition to HTML, CSS, and JS(Javascript), we used the bootstrap framework, and the JS library, Jquery.

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

</head>
<body>
  <div class="container">
    <div class="row">
      <div class="col-xs-12 col-sm-3">
        <button class="btn btn-info btn-seguir" meu-id="1" id-pessoa="2">Seguir</button>
      </div>
    </div>
  </div>
</body>
</html>
<script type="text/javascript">
  $(".btn-seguir").click(function(){
    //Ação de click no botão

    //pega os atributos dos botões e salva em variáveis
    let meu_id = $(this).attr("meu-id");
    let id_pessoa = $(this).attr("id-pessoa");

    //faz a requisição para o link atribuido em url,
    //com os dados atribuidos em data,
    //e com o método atribuido em type
    $.ajax({
      type: "POST",
      data:{
        "meu-id": meu_id,
        "id_pessoa": id_pessoa 
      },
      url: "link que faz seguir com PHP",
      success: function(){
        //Caso a requisição funcione(O ajax retorna o código 200);
        alert("Você está seguindo Fulano agora");
      }
    });

  });
</script>
  • This is the problem kkk... I came here to see if there was a way to do only with php because I haven’t learned ajax, js, jquery kkkk

  • Who takes care of making a request is the client (Browser), to have access to the server script (PHP), vc need to send a request, either by a form, or by an ajax

  • PHP(server). Jquery(client).

  • But thank you,

Browser other questions tagged

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