0

For some reason this code:

$.ajax({
    type: "POST",
    url: "assets/PHP/registreer.php",
    data: $('form').serialize(),
    complete: function (data) {
        console.log(data.responseText);
    }
})

Returns undefined, i'm think this is beacause responseText is not yet returned when complete is fired. But why does it not wait? And how would i make it wait.

I've tryed alot of diffrent things including:

jQuery.ajax response data is undefined

AJAX responseText undefined

Jquery Ajax responseText returns undefined but when i log it as an object it returns the ajax text

EDIT: Include PHP file

<?php
session_start();
$emailPost = $_REQUEST["email"];
$passwordPost = $_REQUEST["password"];
$passwordRetypePost = $_REQUEST["retypepassword"];

$servername = "XXXXXX";
$username = "XXXXX";
$password = "XXXXX";
$dbname = "XXXXXX";

// Create connection

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

if ($passwordPost != $passwordRetypePost) {
    echo "Paswoorden zijn niet hetzelfde!";
} else {
    if (!($stmt = $conn->prepare("SELECT * FROM `Gebruikers` WHERE `Email` = 
?"))) {
        echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
    } else {

$stmt->bind_param("s", $emailPost);
$stmt->execute();
$stmt->bind_result($id, $email, $paswoord, $rol);
$stmt->store_result();

if ($stmt->num_rows > 0) {
    echo "Email bestaat al!";
} else {
    $stmt->close();
    $hash = password_hash($passwordPost, PASSWORD_DEFAULT);
    if (!($stmt = $conn->prepare("INSERT INTO `Gebruikers`(`Email`, `Paswoord`) VALUES (?,'$hash')"))) {
        echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
    }

    $stmt->bind_param("s", $emailPost);
    $stmt->execute();
    echo "Gelukt!";
}
}
}

$mysqli->close();
?>
DjKillerMemeStar
  • 425
  • 5
  • 18
  • 1
    `complete: function (data, textStatus) { console.log("Status:", textStatus); ...` - what is in the console? – Igor Nov 20 '17 at 19:09
  • There is probably an issue with the url, doing as @Igor suggests should help confirm this. – philreed Nov 20 '17 at 19:11
  • Either there is something wrong in your php file, or you have multiple forms on the page. Have you used `json_encode()` in your php file? – SierraOscar Nov 20 '17 at 19:12
  • @Macro Man. I do indeed have multiple forms on my page. But they are called seperatly `$('#registreer').click(function () {` Is what triggers them. Not form onSubmit – DjKillerMemeStar Nov 20 '17 at 19:14
  • @JariFlederick Yes but how does `$('form')` know _which_ form you actually mean? Have you checked in the php file to see if anything is actually being passed? We need to see the HTML and the PHP to be able to assist really – SierraOscar Nov 20 '17 at 19:15
  • @Macro Man That is changed now. But apart from that the PHP file is working 100% because without ajax everything is fine. – DjKillerMemeStar Nov 20 '17 at 19:18
  • @Igor This returns: "Status: error" – DjKillerMemeStar Nov 20 '17 at 19:19
  • Well the PHP obviously isn’t working 100% because it’s not sending the response you’re expecting to the Ajax method. You have to change the PHP to work with the Ajax call - you can’t just “plug and play” straight into an existing PHP file. Again, without the PHP there’s really nothing more to add here. – SierraOscar Nov 20 '17 at 19:20
  • “Without Ajax everything is fine” <~ definitely makes me thing your PHP is wrong. – SierraOscar Nov 20 '17 at 19:21
  • @JariFlederick now, add `console.log(data);` and examine the fields of the request object for more information – Igor Nov 20 '17 at 19:22
  • @igor Already done and the responseText is there. I'll edit my question with a picture – DjKillerMemeStar Nov 20 '17 at 19:24
  • @MacroMan I'll add the PHP to my question! – DjKillerMemeStar Nov 20 '17 at 19:24
  • @JariFlederick you know `echo` will output to the Ajax response? As I suspected - you need to change your PHP file to work with your Ajax call. – SierraOscar Nov 20 '17 at 19:28
  • @MacroMan I've used ajax before on other files and used echo aswell there it it works flawless. So why won't it work now? – DjKillerMemeStar Nov 20 '17 at 19:30
  • Your Ajax is expecting a JavaScript object as the response, not a string. So you need `json_encode` whatever is being refined back to the Ajax. A quick Google will show you what to do. – SierraOscar Nov 20 '17 at 19:31
  • 1
    *"Returns undefined, **i'm think this is beacause responseText is not yet returned when complete is fired**"* that is impossible. – Kevin B Nov 20 '17 at 19:31
  • 1
    @JariFlederick how can - "responseText is there" and "responseText is not yet returned" - exist at the same time? – Igor Nov 20 '17 at 19:33
  • responseText being undefined suggests there may be something wrong with your request. Add an error handler and log all three parameters. – Kevin B Nov 20 '17 at 19:33
  • @Igor Is it posible you have an answer to this problem? Or do you need more info? – DjKillerMemeStar Nov 20 '17 at 19:59

1 Answers1

-1

Found the issue! The problem was that the form's default was not being prevented. So it called a refresh before responseText was present.

Code has been edited to:

$("#login").submit(function (event) {
    event.preventDefault();
        $.ajax({
            type: "POST",
            url: "assets/PHP/registreer.php",
            data: $('#login').serialize(),
            complete: function (data) {
                alert(data.responseText);
            }
        })
});
DjKillerMemeStar
  • 425
  • 5
  • 18