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);
?>
3

2 Answers 2

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);
});
Sign up to request clarification or add additional context in comments.

Comments

1

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

used above. you can also show in console window

1 Comment

Please add some explanation to your code such that others can learn from it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.