1

Below is my code for the problem. I am trying to call php file via AJAX. I have an HTML file where after form submission, the AJAX calls the PHP file. Unfortunately, I cannot see any output.

HTML File(Body):

<form>
  <label for="">Username</label>
  <input type="text" name="username" value="">
  <br><br>
  <label for="">Password</label>
  <input type="text" name="password" value="">
  <br>
  <button name="button" id="button" value="Submit">Submit</button>
</form>
<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script src="login.js"></script>

My login.js file(for AJAX):

$("#button").click(function(e){
e.preventDefault();

$.ajax({
 type: 'POST',
 url: 'login.php'
 });
});

My PHP file:

<?php
 // Server credentials
 $serverName = "localhost";
 $username = "root";
 $password = "root";
 $dbName = "Blog";

 //Creating connection
 $db = mysqli_connect($serverName,$username,$password,$dbName);
 if(!$db){
   echo "error";
   die($db);
 }

 else{
   echo "er22ror";
 }

 mysqli_close($db);
?>

2 Answers2

2

Your AJAX call doesn't do anything with the result:

$.ajax({
    type: 'POST',
    url: 'login.php'
});

In order to see output, you have to output something. Use a callback for that. For example, you can log the result to the console:

$.ajax({
    type: 'POST',
    url: 'login.php'
}).done(function (result) {
    console.log(result);
});
David
  • 208,112
  • 36
  • 198
  • 279
1

$.ajax({
    type: 'POST',
    url: 'login.php',
    success: function (res) {
        alert('hi'+res);
    }
});

used above. you can also show in console window

Sachin Sarola
  • 993
  • 1
  • 9
  • 26