pass a select value to a php page without refreshing the page!

Asked

Viewed 192 times

1

Good morning, guys I have an onchange event in a select, I wanted to take the value of select and send to a php page.

    $(document).on("change", "#doc_sel_clinic", function(){

  var id = $(this).val();
  $("#id_clinic_hidden").val(id);
    console.log(id);


});

i need to send this id and pick up the php page to do a search for the data only by this id, use the codeigniter with friendly url.

I tried to pass via session to pick up with php and could not, what alternatives to do this?

thank you

1 answer

2


You need to make an asynchronous request (more commonly known as AJAX) to achieve what you want.

Assuming you have a PHP page that handles the data you send, you can use jQuery and do something like this:

$('#doc_sel_clinic').on('change', function() {
  var id = $(this).val()

  // Se fosse uma requisição POST, você pode trocar `$.get` por `$.post`:
  $.get('/tratar-dados.php', {
    // Você poderá acessar essa propriedade pelo PHP com `$_GET['id']`:
    id: id
  })
    .done(function(resposta) {
      alert('Dados salvos com sucesso!')

      // Resposta é a mensagem que o PHP "mandou" de volta ao cliente.
      // Supondo que na sua página PHP você tenha um `echo $_GET['id']`, a
      // resposta irá ser uma `string` com o ID que você passou no objeto
      // como segundo argumento na função `$.get`.
      alert('Resposta do servidor: ' + resposta)
    })
    .fail(function() {
      alert('Whoops! Parece que houve um erro ao tentar tratar os dados.')
    })
})

Reference:

  • "the purpose of the site is not to [...] give the code ready as it asks in this context" - You ;)

  • Thank you, I did it this way. I received the success message but when I search in php it just show that it is empty. Thus array(0) { }. Someone knows what can be?

  • What is empty? What are you trying to access?

Browser other questions tagged

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