PHP发布请求未识别的索引错误

时间:2016-08-15 08:14:17

标签: javascript php sql

我正在尝试编写一个页面来对PHP脚本发出POST请求,我觉得我已经做得对,它在其他地方工作所以看起来似乎但我一直得到一个“未识别的错误”并且它赢了工作,我怎么能让这个工作?

使用Javascript:

$(document).ready(function() {
    $("#x").click(function() {
        var email = $("email").val();
        var pass = $("password").val();
        var confirmPass = $("confirmPassword").val();
        var name = $("name").val();
        var question = $("question").val();
        var answer = $("answer").val();

        if(pass != confirmPass) {
            alert("Passwords do not match!");
            return;
        }

        var stuff = {email: email, pass: pass, name: name, question: question, answer: answer};

        $.ajax({method: "POST", url: "addAccount.php", data: stuff, success: function(result) {
            alert(result);
            window.location.href = "../Dashboard";
        }});
    });
});

PHP:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "*********";
    $dbname = "myDB";

    $conn = new mysqli($servername, $username, $password, $dbname);

    $email = $_POST["email"];
    $pass = $_POST["pass"];
    $name = $_POST["name"];
    $question = $_POST["question"];
    $answer = $_POST["answer"];

    $sql = "INSERT INTO accounts (accountEmail, accountPassword, accountName, accountQuestion, accountRecover) VALUES ('$email', '$pass', '$name', '$question', '$answer')";
    $conn->close();

    if(mysql_affected_rows() > 0) {
        $response = "Account added successfully!";
    }
    else {
        $response = "Couldn't add account!";
    }

    $pre = array("Response" => $response);
    echo json_encode($pre);
?>

3 个答案:

答案 0 :(得分:0)

你需要正确使用jquery 例如 var email = $("email").val(); //IS WRONG 应该是(如果您输入id =&#34; email&#34;) var email = $("#email").val(); 如果您只有名字,则可以使用 var email = $("[name='email']").val();

有点偏离主题: 如果你正在使用表单ajax submit,请考虑使用jquery方法serialize https://api.jquery.com/serialize/来获取所有表单值(或者一些jquery ajaxform插件)。

请!不要制作不安全的mysql语句。为了上帝的缘故,请使用预备陈述。 如果您需要非常基本的东西,只需使用预备语句或考虑https://phpdelusions.net/pdo/pdo_wrapper

还有一个小提示:在echo json之前制作json标题 <?php header('Content-type:application/json;charset=utf-8');

答案 1 :(得分:0)

您的代码无法正常工作的原因很多。 @AucT和@gentle解决了你的Javascript问题,所以我将专注于PHP。您的查询代码是:

$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "...";
$conn->close();

请注意:

  • 你从不执行查询。 $sql只是一个内存中的字符串。
  • 您将mysqli函数与mysql_函数(mysql_affected_rows)混合;那将无法运作
  • 您正在将POST数据直接插入查询中,因此您很容易受到SQL injection
  • 的攻击
  • 最后,您回显JSON,但您没有告诉浏览器期望这种格式

请改为:

$conn = new mysqli(...);
//SQL with ? in place of values is safe against SQL injection attacks
$sql = "INSERT INTO accounts (accountEmail, accountPassword,
          accountName, accountQuestion, accountRecover) VALUES (?, ?, ?, ?, ?)";

$error = null;
//prepare query and bind params. save any error
$stmt = $conn->prepare($sql);
$stmt->bind_param('sssss',$email,$pass,$name,$question,$answer)
        or $error = $stmt->error;

//run query. save any error
if(!$error) $stmt->execute() or $error = $stmt->error;

//error details are in $error
if($error) $response = "Error creating new account";
else $response = "Successfully created new account";

//set content-type header to tell the browser to expect JSON
header('Content-type: application/json');
$pre = ['Response' => $response];
echo json_encode($pre);

答案 2 :(得分:0)

我认为你的jquery数据错了,它们应该有像id所标识的标识符#&#39;#&#39;和&#39;表示的类。&#39;,这样做就是你有id =&#34;字段名称&#34;在输入参数中:

$(document).ready(function() {
$("#x").click(function() {
    var email = $("#email").val();
    var pass = $("#password").val();
    var confirmPass = $("#confirmPassword").val();
    var name = $("#name").val();
    var question = $("#question").val();
    var answer = $("#answer").val();

    if(pass != confirmPass) {
        alert("Passwords do not match!");
        return;
    }

    var stuff = {email: email, pass: pass, name: name, question: question, answer: answer};

    $.ajax({method: "POST", url: "addAccount.php", data: stuff, success: function(result) {
        alert(result);
        window.location.href = "../Dashboard";
    }});
});
});

或者像这样你有class =&#34;字段名称&#34;在输入参数中:

$(document).ready(function() {
$("#x").click(function() {
    var email = $(".email").val();
    var pass = $(".password").val();
    var confirmPass = $(".confirmPassword").val();
    var name = $(".name").val();
    var question = $(".question").val();
    var answer = $(".answer").val();

    if(pass != confirmPass) {
        alert("Passwords do not match!");
        return;
    }

    var stuff = {email: email, pass: pass, name: name, question: question, answer: answer};

    $.ajax({method: "POST", url: "addAccount.php", data: stuff, success: function(result) {
        alert(result);
        window.location.href = "../Dashboard";
    }});
});
});

如果您想直接使用该名称,请按照以下步骤操作:

$(document).ready(function() {
$("#x").click(function() {
    var email = $("input[name='email']").val();
    var pass = $("input[name='pasword']").val();
    var confirmPass = $("input[name='confirmPassword']").val();
    var name = $("input[name='name']").val();
    var question = $("input[name='question']").val();
    var answer = $("input[name='answer']").val();

    if(pass != confirmPass) {
        alert("Passwords do not match!");
        return;
    }

    var stuff = {email: email, pass: pass, name: name, question: question, answer: answer};

    $.ajax({method: "POST", url: "addAccount.php", data: stuff, success: function(result) {
        alert(result);
        window.location.href = "../Dashboard";
    }});
});
});

我希望这有助于你