I've a problem with a returned json value, when the ldap result is valid the json return is OK but when the result is invalid the json is invalid too.
I'm using uwamp. I'm a newbie.

Thank you all.

**php code :**

```
<?php
if (is_ajax()) {
  if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
    $action = $_POST["action"];
    switch($action) { //Switch case for value of action
      case "test": test_function(); break;
    }
  }
}
//Function to check if the request is an AJAX request
function is_ajax() {
  return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}

function test_function(){
  $return = $_POST;
  $return["code"] = "NO PASSWORD";
  //$ldapconn = FALSE;

  $return["username"] = 'intra\\'.$return["username"];
  ($return["password"]) ? $ldapconn = ldap_connect("ldap://xxx:389") : $ldapconn = FALSE ;
  if ($ldapconn) {

      $ldapbind = ldap_bind($ldapconn, $return["username"], $return["password"]);

      $return["code"] = ($ldapbind) ? "VU" : "VI" ;
      $ldapunbind = ldap_unbind($ldapconn);

  }
  $return["json"] = json_encode($return);
  echo json_encode($return);

}
?>

```

**html code :**

```
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <script src="https://code.jquery.com/jquery-3.0.0.js"></script>
  <script type="text/javascript">
  $("document").ready(function(){
    $(".js-ajax-php-json").submit(function(event){
      var data = {
        "action": "test"
      };
      data = $(this).serialize() + "&" + $.param(data);
      $.ajax({
        type: "POST",
        dataType: "json",
        url: "ldap.php", //Relative or absolute path to response.php file
        data: data,
        success: function(data) {
          $(".the-return").html(
            "username: "         + data["username"] +
            "<br />password: "   + data["password"] +
            "<br />Code: "       + data["code"] +
            "<br />JSON: "       + data["json"]
          );
          alert("Form submitted successfully.\nReturned json: " + data["json"]);
        },
        error: function (request, status, error) {
          alert(request.responseText + "-" + error + "-" + request);
        }
      });
      //return false;
      event.preventDefault();
    });
  });
  </script>
  <title>Document</title>
</head>
<body>
  <form action="" class="js-ajax-php-json" method="post" accept-charset="utf-8">
    <input type="text"     name="username" value="" placeholder="username" />
    <input type="password" name="password" value="" placeholder="password" />

    <input type="submit" name="submit" value="Submit form"  />
  </form>

  <div class="the-return">
    [HTML is replaced when successful.]
  </div>
</body>
</html>

```

**error code :**

( ! ) Warning: ldap\_bind(): Unable to bind to server: Invalid credentials in E:\SOFT\WebServer\UwAmp\www\json\_validation\ldap.php on line *24*
Call Stack
#TimeMemoryFunctionLocation
10.0340138696{main}( )...\ldap.php**:**0
20.0340138800test\_function( )...\ldap.php**:**6
30.0340139424http://www.php.net/function.ldap-bind' target='\_new'>ldap\_bind
( )...\ldap.php**:**24

{"username":"intra\username","password":"qsdqsdqs","action":"test","code":"VI","json":"{\"username\":\"\\xxx\",\"password\":\"qsdqsdqs\",\"action\":\"test\",\"code\":\"VI\"}"}-SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data-[object Object]