Instead of using id
s in form fields, you can use name
and use the method .serialize()
, which will return all fields in a string to be sent by Ajax to the PHP page.
Note that your form there are some typos:
</form> <- aqui o form foi fechado em vez de ser aberto
<input type="text" id="email">
<input type="password id="senha"> <- aqui faltou aspas para fechar o type
<button>Entrar</button>
</form>
Using name
and correcting the above errors, your form would look like this:
<form>
<input type="text" name="email">
<input type="password" name="senha">
<button>Entrar</button>
</form>
Using .serialize()
:
var dados = form.serialize();
The variable dados
becomes the values of the form fields, for example:
[email protected]&senha=senha_digitada
Applying in submit
and Ajax with the POST method:
var form = $('form');
form.submit(function(event){
event.preventDefault();
var dados = form.serialize();
$.ajax({
type: 'POST',
data: dados,
url: 'verificar.php',
success: function(data){
// faça alguma coisa se o Ajax foi bem sucedido;
}
});
});
In the PHP file verificar.php
, you capture the values with:
<?php
$email = $_POST['email'];
$senha = $_POST['senha'];
?>