I'm trying to write a page to make a POST request to a php script and I feel like I've done it right, it's worked everywhere else so it seems but I keep getting a "unidentified error" and it won't work, how can I get this to work?

我正在尝试编写一个页面来对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 个解决方案

#1


0

You need to properly use jquery.
For example var email = $("email").val(); //IS WRONG Should be (if you have input id="email") var email = $("#email").val(); If you have only name you can use var email = $("[name='email']").val();

你需要正确使用jquery。例如var email = $(“email”)。val(); //错误应该是(如果你有输入id =“email”)var email = $(“#email”)。val();如果您只有名字,可以使用var email = $(“[name ='email']”)。val();

A bit offtopic: If you are using form ajax submit consider jquery method serialize https://api.jquery.com/serialize/ for getting all form values (or some jquery ajaxform plugin).

有点offtopic:如果你正在使用表单ajax提交考虑jquery方法序列化https://api.jquery.com/serialize/获取所有表单值(或一些jquery ajaxform插件)。

And please! don't make insecure mysql statements. For gods sake use prepared statements. If you need very basic stuff just use prepared statements or consider https://phpdelusions.net/pdo/pdo_wrapper

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

Also a small tip: before echo json make json header <?php header('Content-type:application/json;charset=utf-8');

还有一个小提示:在echo json之前使json标头

更多相关文章

  1. 在提交表单之前确定重复值
  2. 选择无线电输入时,无法提交带有JavaScript功能的表单
  3. 从列表单击功能获取列表项的值
  4. if-else语句太多了。他们可以成为全球性的吗?
  5. 在单页中使用ajax和php上传多个表单数据
  6. JavaScript表单验证和正则表达式
  7. 阻止用户在表单字段中输入
  8. chai-as-promised:单个测试中的多个期望语句
  9. 放在和中的javascript语句,但是语句不在函数中,这些语句何在被执行

随机推荐

  1. 查询自定义VO对象的sql
  2. 如果没有明确使用ISNULL,则左连接失败
  3. qt sql多重条件查询简便方法
  4. SQLServer行转列的问题
  5. 数据库操作类实现(C#,SqlClient)
  6. TTable怎样实现类似SQL Select..form...w
  7. 重复的数据只取一条,请问SQL语句怎么写
  8. Mysql设置自增长主键的初始值
  9. mysql通过复制data文件夹进行数据迁移
  10. 为什么明明登陆了plsql执行还报错呢