Having AJAX change result based on PHP If statement outcome?

Having AJAX change result based on PHP If statement outcome?

Hi guys,

I have had my fair share of experience with jQuery but have never dared go near the AJAX uses just because of the effect it has on my brain and causes me to become obsessive with fixing errors.

So, here I am with an error...

I have my usersystem.js and here is the login section which deals with the result of the user attempting to login.
  1. });
  2. $("#logincontent").on('click', '#login', function(){
  3. var loginusername = $("#loginusername").val();
  4. var loginpassword = $("#loginpassword").val();
  5. var dataString = 'loginusername='+ loginusername + '&loginpassword='+ loginpassword;
  6. if(loginusername==''||loginpassword=='')
  7. {
  8. alert("You left the username or password field blank, oops.");
  9. }
  10. else
  11. {
  12. $.ajax({
  13. type: "POST",
  14. url: "files/php/login.php",
  15. data: dataString,
  16. cache: false,
  17. success: function(result){
  18. $("#content").html('<div class="exclusivetransparentboxleft"></div><div class="exclusivetransparentboxright"></div>');
  19. $('.exclusivetransparentboxleft').load('files/php/usercp.php');
  20. $("#navigation").html('<div id="navigationitem">Home</div><div id="navigationitem">About Us</div><div id="navigationitem">Support</div><div id="navigationitem">Add Review</div><div id="navigationitem">View Venues</div><div id="navigationitem">Book Venues</div>');
  21. }
  22. });
  23. }
  24. return false;
  25. });
Now, as you can see, regardless of whether the user has entered the correct username or password it allows them to login with any name or password, due to the 'success' state running the result function once the script understands the data has been sent.

So, how would I go about changing the result based on the outcome of a PHP If statement, like in the login.php file I have below...
  1. <?php
  2. session_start();
  3. include("connect.php");
  4. $loginusername = $_POST["loginusername"];
  5. $loginpassword = $_POST["loginpassword"];
  6. $stmt = $conn->prepare("SELECT * FROM users WHERE username=:theusername");
  7. $stmt->bind_param(":theusername", $loginusername);
  8. $row = $stmt->fetch();
  9.     if (password_verify($loginpassword, $row['password']))
  10. {
  11.     $_SESSION["logged"] = $loginusername;
  12. }else{
  13. ?>
  14. <p style="font-size: 16px;"><b>Incorrect</b></p>
  15. <p>We're sorry to report that the username and password combination you entered does not exist, if you would like to try to login again then please click <a href="javascript:loadContent('#logincontent','login.html');">here.</a></b></p>
  16. <?php
  17. }
  18. ?>
As you can see, if they login, it sets the session to store the username to keep them logged in, if it's not successful you can see the error result.

I just don't know how to do what I'm doing in the PHP file in the JS file, I've been trying for two days straight now and still nothing.

Any help is extremely appreciated.
Thanks for any suggestions or opinions put forward.