user4298098
user4298098

Reputation: 1

EOF / Failed to load error when calling PHP file with AJAX

Apparently my POST requests are being cancelled? http://puu.sh/d73LC/c6062c8c07.png

and also, mysqli_result object has all null values when i query the database with a select query:

object(mysqli_result)[2]
  public 'current_field' => null
  public 'field_count' => null
  public 'lengths' => null
  public 'num_rows' => null
  public 'type' => null

here is my php file:

<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "uoitlol";
$name = "test1"; //this should be $_POST['name']; test1 is just to test if it works.
$err = false;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_errno > 0) {
    echo 'connerr';
    die();
}
$sql = "INSERT INTO summoners (name) VALUES (?)";

$getname = "SELECT name FROM summoners";

$result = $conn->query($getname);
while ($row = $result->fetch_assoc()) {
    echo 'name : ' . $row['name'];
    if ($row['name'] === $name) {
        echo 'error, name exists';
        $err = true;
    }
}

$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $name);

if ($err === false) {
    if (!$stmt->execute()) {
        echo 'sqlerr';
    } else {
        echo 'success';
    }
}

$stmt->close();
mysqli_close($conn);

here is my javascript file, which calls the php file with ajax whenever i click submit on my form (in a different html file)

$(document).ready(function () {
    $("#modalClose").click(function () {
        document.getElementById("signupInfo").className = "";
        document.getElementById("signupInfo").innerHTML = "";
    });
    $("#formSubmit").click(function () {
        var name = $("#name").val();
// Returns successful data submission message when the entered information is stored in database.
        var dataString = {'name' :name};
        if (name === '')
        {
            document.getElementById("signupInfo").className = "alert alert-danger";
            document.getElementById("signupInfo").innerHTML = "<b>Please enter a summoner name!</b>";
        }
        else
        {
// AJAX Code To Submit Form.
            $.ajax({
                type: "POST",
                url: "submitName.php",
                data: dataString,
                cache: false,
                success: function (msg) {
                    if (msg === 'error'){
                        document.getElementById("signupInfo").className = "alert alert-danger";
                        document.getElementById("signupInfo").innerHTML = "<b>That summoner name is already in the database!</b>";
                    } else if (msg === 'sqlerror'){
                        document.getElementById("signupInfo").className = "alert alert-danger";
                        document.getElementById("signupInfo").innerHTML = "<b>SQL error, contact the administrator.</b>";
                    } else if (msg === 'success'){
                        document.getElementById("signupInfo").className = "alert alert-success";
                        document.getElementById("signupInfo").innerHTML = "<b>Summoner successfully added!</b>";
                    }
                }
            });
        }
        return false;
    });
});

I'm getting these errors everytime I click my button that submits my form:

Failed to load resource: Unexpected end of file from server (19:41:35:538 | error, network) at public_html/submitName.php

Failed to load resource: Unexpected end of file from server (19:41:35:723 | error, network) at public_html/submitName.php

Failed to load resource: Unexpected end of file from server (19:41:36:062 | error, network) at public_html/submitName.php

I'm using Netbeans IDE, if that matters.

puu.sh/d6YXP/05b5f3dc06.png - screenshot of the IDE, with the output log errors.

Upvotes: 0

Views: 1214

Answers (2)

EternalHour
EternalHour

Reputation: 8631

Remove this from your submitName.php, unless there really is HTML in it.

<!DOCTYPE html>

If there is HTML in it, do this instead.

<?php
//your PHP code//
?>
<!DOCTYPE html>
//your HTML here//
</html>

Also, if submitName.php contains no HTML, make sure there is no blank line after ?> at the bottom.

EDIT: In regards to your query failing, try this code.

if (!empty($name) { //verify the form value was received before running query//
    $getname = "SELECT name FROM summoners WHERE name = $name";

    $result = $conn->query($getname);
    $count = $getname->num_rows; //verify a record was selected//
    if ($count != 0) {
        while ($row = $result->fetch_assoc()) {
            echo 'name : ' . $row['name'];
            if ($row['name'] === $name) {
                echo 'error, name exists';
                $err = true;
            }
        }
    } else {
        echo "no record found for name";
        exit;
    }
}

Upvotes: 1

Philll_t
Philll_t

Reputation: 4437

Drop the ?> at the end of the php file and instead of using var dataString = 'name=' + name; use this instead:

var data = { "name" : name};

jQuery will automagically do the dirty stuff for you so that you don't have to special text-escape it and stuff.

That's as far as I can help without any log files and just a quick skim of your code.

Upvotes: 0

Related Questions