html - Send Password using Ajax to PHP



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.


 <form action="" method="post" id="formLogin">
    <div >
     <input type="password" name="password" required="" placeholder="Password">
   <button type="submit">Accedi</button>


        $('#formLogin').on('submit', function(event)

            var items = $('#formLogin').serialize();

                type: 'POST',
                url: '',
                data: items,
                contentType: "application/json; charset=utf-8",
                //dataType: 'json',
                success: function(data)

                    var result = JSON.stringify(data.flag);

                error: function()
                    alert('Connsessione non riuscita');


  $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' => '',
      'flag' => 'true',
      'password' =>'Password',
      'categoria' => $riga["CategoriaAppartenenza"],
      'comune' => $riga["ComuneAppartenenza"],
      'regione' => $riga["Regione"]
  echo json_encode($dataJSON);
     $dataJSON = array('flag' => 'false');

     echo json_encode($countRisultato);

People are also looking for solutions to the problem: PHP messing with HTML Charset Encoding


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.