1

I used similar code like below to show database tables. It was working then but now it's not working. I have tried many other codes but my post file is not called no matter what I do.

<div class="loginform-in">
  <h1>User Login</h1>
  <div class="err" id="add_err"></div>
  <fieldset>
    <form action="./" method="post">
      <ul>
        <li>
          <label for="name">Username </label>
          <input type="text" size="30"  name="name" id="name" />
        </li>
        <li> 
          <label for="name">Password</label>
          <input type="password" size="30"  name="word" id="word" />
        </li>
        <li> 
          <label></label>
          <button id="login" type="button" class="btn btn-default" onclick="saveBasics();">Login</button>
      </ul>
    </form> 
  </fieldset>
</div>

<script src="jquery-2.2.3.min.js"></script>
<script src="login.js"></script>

login.js

alert("hi");

$("#login").click(function(){   
  username = $("#name").val();
  password = $("#word").val();

  $.ajax({
    url: "login.php",
    type: "POST",
    data: "name=" + username + "&pwd=" + password,
    success: function(html) { 
      aler(html);
      if (html == 'true') {
        window.location = "login.php";
      } else {
        $("#add_err").css('display', 'inline', 'important');
        $("#add_err").html("<img src='images/alert.png' />Wrong username or 
password");
      }
    },
    beforeSend: function() {
      $("#add_err").css('display', 'inline', 'important');
      $("#add_err").html("<img src='images/ajax-loader.gif' /> Loading...")
    }
  });
  return false;
});

login.php

<?php
  echo '<script language="javascript">';
  echo 'alert("message successfully sent")';
  echo '</script>';
?>

My code is not running login.php file and I don't know why. It should show alert when button is clicked but it doesn't.

Note: I just added alert(html); in my java script file and the alert shows

    <script language="javascript">alert("message successfully sent")
    </script>true

this

Edited: Every body The javascript is working fine (I think) its just that the response I am getting is the literal php page code.

8
  • 3
    You check if html is equal to 'true'. That will never happen. Commented Aug 2, 2017 at 8:48
  • but any ways it should show the alert that is written in login.php file. It is not even doing that. Commented Aug 2, 2017 at 8:49
  • hey echo "true" in login.php and remove all other code Commented Aug 2, 2017 at 8:50
  • Check the console to see the response from the AJAX request. At the very least there should be an HTTP response code which you can use to debug Commented Aug 2, 2017 at 8:50
  • No, it should not show the alert. You never tell the browser to treat the string that is returned from the request as HTML. It's just a string. Commented Aug 2, 2017 at 8:52

4 Answers 4

1

1st : your not sending response string like 'html' so that your if statement will be never true

For testing purpose echo the true in login.php like below

<?php
    echo 'true';
?>

2nd : In success function alert the response like this

success: function(response){ 

         alert(response);

          },
Sign up to request clarification or add additional context in comments.

Comments

0

Open browsers console (F12) and see what errors are you getting?

Also, there is a Network tab.

If you will filter the XHR in the Network tab - then you will see, if your request is actually addressing the login.php page and you can see what the page has answered (the Response sub-tab).

Usually, I use more convenient way to fire ajax requests:

$.post('login.php', {
    name: $('#name').val(),
    pwd: $('#word').val(),
}, function(response) {
    console.log(response);
    alert(response); 
});

1 Comment

This technically isn't an answer; it's showing the OP how to debug his code and as such should be a comment.
0

No you can't alert message from php through ajax. Instead of it you can do following

login.js

alert("hi");

$("#login").click(function(){   
  username = $("#name").val();
  password = $("#word").val();

  $.ajax({
    url: "login.php",
    type: "POST",
    data: "name=" + username + "&pwd=" + password,
    success: function(html) { 
      if (html == 'true') {
        alert("message successfully sent"); //<-----------alert here
        window.location = "login.php";
      } else {
        $("#add_err").css('display', 'inline', 'important');
        $("#add_err").html("<img src='images/alert.png' />Wrong username or 
password");
      }
    },
    beforeSend: function() {
      $("#add_err").css('display', 'inline', 'important');
      $("#add_err").html("<img src='images/ajax-loader.gif' /> Loading...")
    }
  });
  return false;
});

login.php

<?php
echo "true"; //<---return only true
?>

Comments

0

My problem was solved as I just need to understand how response works.

But question is the alert that I tried to show in login.php works in other forms I have implemented but not here. Why??

Comments

Your Answer

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