登录php,ajax和API时出现问题

时间:2018-10-25 05:28:16

标签: php ajax api

我在这里发布用户名和密码,应该对其进行验证。如果成功,它将返回用户名,否则将给出错误。所以现在我很困惑我是ajax的新手。请帮助我找到解决方案的人。

注意: 我尝试从 POSTMAN 获得答复,如果用户名和密码是正确这样

,则会给我答复
{
    "username": "Felix_Col"
}

如果用户名和密码错误,则会显示类似错误

{
    "error": {
        "message": "",
        "code": "401",
        "date": "Fri May 10 16:53:17 IST 2019"
    }
}

但是当我在浏览器中使用 HTML前端时,我被卡住了。请帮助我获得直到现在为止还被困住的解决方案

    <body>
        <!-- Begin Page Content -->
        <div id="container">
           <form id="loginform" method="post">
                <label for="username">Username:</label>
                <input type="text" id="username" name="username" required>
                <label for="password">Password:</label>
                <input type="password" id="password" name="password" required>
                <div id="lower">
                    <input id="checkbox" type="checkbox"><label class="check" for="checkbox">Save Login Credentials</label>
                    <input type="submit" name="submit" id="submit" value="Login">
                </div><!--/ lower-->
            </form>
        </div><!--/ container-->
        <!-- End Page Content -->
        <script>

    $(document).ready(function() {
  $('#loginform').submit(function(e) {
    e.preventDefault();
    $.ajax({
       type: "POST",
       url: 'http://',
       data: $(this).serialize(),
       success: function(res)
       {

            window.location = '/user-page.php';

       }
   });
 });
});
    </script>
    </body>

1 个答案:

答案 0 :(得分:1)

将players表的副本复制为players_backup,然后将数据首先插入备份表,一旦插入后获得的num行大于零,则表示已插入数据。之后,您可以从玩家表中删除数据。很简单。

我假设players和players_bkp具有相同的架构。

if (isset($_GET['id']) && is_numeric($_GET['id']))
{
    // get the 'id' variable from the URL
    $id = $_GET['id'];
    $sql  = "insert into players_bkp select * FROM players WHERE id = ? LIMIT 1";
    $stmt = $mysqli->prepare($sql);
    $rc = $stmt->bind_param('i',$id);
    $rc = $stmt->execute();
    $tid = $stmt->insert_id;
    $stmt->close();
    if($tid  > 0){  //that means record is inserted and you can actually delete the record from your players table
        // delete record from database
        if ($stmt = $mysqli->prepare("DELETE FROM players WHERE id = ? LIMIT 1"))
        {
            $stmt->bind_param("i",$id);
            $stmt->execute();
            $stmt->close();
        }
        else
        {
            echo "ERROR: could not prepare SQL statement.";
        }
        $mysqli->close();

        // redirect user after delete is successful
        header("Location: view.php");
    }
}
else
// if the 'id' variable isn't set, redirect the user
{
    header("Location: view.php");
}