简体   繁体   中英

Having issue while login in php ,ajax and API

Here i am posting username and password and it should verify it. if success it will return the username else it will give error. so now i am confused how to proceed as i am new to ajax . Please help me to get solution guys.

NOTE: I tried to get my response from POSTMAN it gives me a response if username and password is correct like this

{
    "username": "Felix_Col"
}

if username and password is wrong it gives error like this

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

But i am stuck when i am using HTML front-end in the browser.Please help me to get solution i am stuck till now

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

make a replica of players table as players_backup and insert the data first to backup table and once you get the num rows after inserting is greater than zero that means the data has been inserted. after this you can delete the data from the players tables. Simple.

i am assuming that players and players_bkp have same schemas.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM