-3

I can't figure it out, i have a form submitted by ajax and the ajax code always return false even if the form values doesn't meet the requirement. Also the function that should happend in the form action php file when the form is correct doesn't happend so it doesnt matter if i fill in the required fields or not, or if i fill them right or wrong the ajax will call it success and the php action wont work{even if the form is correct!}

Form :

<form method="POST" onsubmit="return loginSubmit(this)">
<input type="text" name="username" placeholder="username"></br>
<input type="password" name="password" placeholder="password"></br>
<button type="submit" name="submit">login</button>
</form>

Function :

function loginSubmit(element){
  var values = $(element).serialize();
  $.ajax({
    type: 'post',
    url: 'assets/login.inc.php',
    data: values,
    success: function(data){
      alert('success');
    }
  });   
  return false;
}

Form Action{php} :

it's kind of long and i dont want this post to look like a mess so i'm just writing the way i pull the data from the form, the php code works fine when i submit the form without ajax.

$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
ginman
  • 1,315
  • 10
  • 20
Afik Habaz
  • 55
  • 12
  • 3
    Well, the function returns immediately, well *before* the ajax call gets a response (asynchronously). You cannot expect the function to return something that depends on a future event. So yes, it always returns false. – trincot Mar 13 '19 at 22:10
  • @trincot Sorry i couldn't understand what you wrote. The ajax always returns success not false. – Afik Habaz Mar 13 '19 at 22:12
  • That is not a *return*, that alert is what the asynchronous callback produces. It has nothing to do with the return value for `loginSubmit`. Sequence of execution is: (1) `values =`, (2) `$.ajax()`, (3) `return false`, (4) form is not submitted because of (3), (pause...now PHP script runs to completion...), (5) `alert('success')` – trincot Mar 13 '19 at 22:15
  • @trincot , Thanks for replying and thanks for helping me understand the execution proccess. I've tried to see what 'data' returns and it has returned the whole php form page. I've found more people who had the same thing{whole page return} but i still couldn't managed to fix my problem. Also i am sorry if my english is hard to under stand. – Afik Habaz Mar 13 '19 at 22:56
  • That is a completely different question. See also [this](https://stackoverflow.com/questions/12142172/apache-shows-php-code-instead-of-executing) for many different answers on that subject. – trincot Mar 14 '19 at 06:05

2 Answers2

-1

The jQuery success callback doesn't do what you think it does. It is triggered if the HTTP call is successful and received a normal 'OK' response.

You need to parse the data parameter within the callback to determine what your PHP code returned.

From the jQuery documentation for the 'Ajax' method call:

A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter or the dataFilter callback function, if specified; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. Ref: http://api.jquery.com/jquery.ajax/

cobberboy
  • 5,598
  • 2
  • 25
  • 22
  • Hay thanks for replying, also thanks for making it clear for me now i know why it returned succes everytime. i still wasn't able to fix my problem though i tried to check what 'data' will return and it retuns the whole form page which i saw on other questions that it's common but i still didn't got it to work... – Afik Habaz Mar 13 '19 at 23:00
  • not sure why anyone would downvote this? It's correct and is the source of confusion in the OP. – cobberboy Mar 14 '19 at 03:33
  • @AfikHabaz I'm not a PHP programmer but I believe you're posting to a URL that is intended to be included in other PHP files rather than invoked on its own (as designated by the `.inc.php` extension. – cobberboy Mar 14 '19 at 03:37
-2

Ok i got it, in the php file i checked if the form was submitted which seems not true if you submit it trough ajax. Thanks

Afik Habaz
  • 55
  • 12