EOF /使用AJAX调用PHP文件时无法加载错误

时间:2014-11-27 00:57:04

标签: javascript php jquery ajax eof

显然我的POST请求被取消了? http://puu.sh/d73LC/c6062c8c07.png

并且,当我使用select查询查询数据库时,mysqli_result对象具有所有空值:

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

这是我的php文件:

<?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);

这是我的javascript文件,只要我在表单上单击提交(在不同的html文件中),就会用ajax调用php文件

$(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;
    });
});

每次点击提交表单的按钮时,我都会收到这些错误:

无法加载资源:来自服务器的文件意外结束(19:41:35:538 |错误,网络)   在public_html / submitName.php

无法加载资源:来自服务器的文件意外结束(19:41:35:723 |错误,网络)   在public_html / submitName.php

无法加载资源:来自服务器的文件意外结束(19:41:36:062 |错误,网络)   在public_html / submitName.php

如果重要的话,我正在使用Netbeans IDE。

puu.sh/d6YXP/05b5f3dc06.png - IDE的屏幕截图,输出日志错误。

2 个答案:

答案 0 :(得分:1)

submitName.php中移除此内容,除非其中确实包含HTML。

<!DOCTYPE html>

如果其中有HTML,请改为执行此操作。

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

另外,如果submitName.php不包含HTML,请确保底部?>后面没有空行。

编辑:关于您的查询失败,请尝试使用此代码。

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;
    }
}

答案 1 :(得分:0)

?>放在php文件的末尾,而不是使用var dataString = 'name=' + name;代替:

var data = { "name" : name};

jQuery会自动为你做脏东西,这样你就不需要特殊的文本 - 逃避它了。

只要我没有任何日志文件就可以提供帮助,只需快速浏览一下代码。