Well, then the question we have is:
How to get select values from PHP server without refreshing the page?
The answer is:
Use AJAX, there are some ways to use Ajax and in my opinion the easiest and most compatible is with jQuery.
Imagine that we have a login / registration page and we want it to send the information to the server without leaving it at all, this would be an example similar to yours.
It's very simple, basically we need 2 files index.html and cadastro.php
full code: download full code
index.html is simply an HTML file with the common form that we already know the difference is in the ajax
var campos = {nome: "Joao", idade: 32};
$.ajax({
data: campos,// dados que serão enviados para o servidor
url: "cadastro.php", // url a buscar sem fazer refresh (ajax)
type: "POST", // método de envio dos dados (GET,POST)
dataType: "html", // como será recebida a resposta do servidor (html,json)
success: function(data){ // função que tras a resposta quando tudo der certo
alert(data);
},
error: function(){
alert("problema ao carregar a solicitação");
}
});
fields is the variable with the data to be sent, success is the function that is called when everything is finished, type is the way data is sent be GET
cadastro.php
You simply take the fields, do whatever you want with them, and print out an answer.
<?php
echo "você enviou os campos: <br/>";
print_r($_POST);
More details at:
jquery.com
ajax function