html - Send Password using Ajax to PHP
PROBLEM
I would like to send the form from Ajax to PHP. My Ajax reaches the PHP but the last one does not find any index corresponding to the value of the Input Password. I tried to do a lot of research to understand if and where the problem was but nothing. Also, I would like to know if Ajax is used correctly in this way, to send not only other forms but also JSON files in the future.
Note: PHP responds in JSON format.
HTML
<form action="https://civicsensethecitizen.altervista.org/php/formLogin.php" method="post" id="formLogin">
<div >
<input type="password" name="password" required="" placeholder="Password">
</div>
<button type="submit">Accedi</button>
</form>
JAVASCRIPT
$(function()
{
$('#formLogin').on('submit', function(event)
{
event.preventDefault();
var items = $('#formLogin').serialize();
$.ajax(
{
type: 'POST',
url: 'https://civicsensethecitizen.altervista.org/php/formLogin.php',
data: items,
contentType: "application/json; charset=utf-8",
//dataType: 'json',
success: function(data)
{
$('#formLogin').trigger("reset");
var result = JSON.stringify(data.flag);
alert(data);
},
error: function()
{
$('#formLogin').trigger("reset");
alert('Connsessione non riuscita');
}
});
});
});
PHP
<?php
$password = $_POST['password'];
$connessione = mysqli_connect('localhost','civicsensethecitizen','') or die (mysqli_errno ($connessione). mysqli_error ($connessione));
$db = mysqli_select_db($connessione, "my_civicsensethecitizen") or die ('Database non trovato!');
$query = "SELECT Password FROM Ente WHERE Password = '$password' ";
$risultato = mysqli_query($connessione,$query) or die ("Error in query: $query. " . mysqli_connect_error());
$countRisultato = mysqli_num_rows($risultato); //conta quante righe con quella password sono state trovate
if($countRisultato == 1)
{
$riga = mysqli_fetch_array($risultato, MYSQLI_ASSOC);
$dataJSON = array(
'url' => 'https://civicsensethecitizen.altervista.org/index.html',
'flag' => 'true',
'password' =>'Password',
'categoria' => $riga["CategoriaAppartenenza"],
'comune' => $riga["ComuneAppartenenza"],
'regione' => $riga["Regione"]
);
echo json_encode($dataJSON);
}
else
{
$dataJSON = array('flag' => 'false');
echo json_encode($countRisultato);
}
mysqli_close($connessione);
?>