-3

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <script src="jquery-3.1.0.min.js" type="text/javascript"></script>
        <script src="myValidations.js" type="text/javascript"></script>
    </head>
    <body>
        Username : <input type="text" id="txtusername"></>
        Password : <input type="text" id="txtpassword"></>
        <input type ="submit" value="Save" id="btnSave"></>
        <div id="disp"></div>
    </body>
</html>

myValidation.js

$(document).ready(function(){
    $("#btnSave").click(function() {
        var username = $("#txtusername").val();
        var password = $("#txtpassword").val();
        $.ajax({ 
                type: 'POST',
                url: 'savecontacts.php',
                data: {user:username, pass:password},
                success: function (data) {
                    alert(data);
                }
        });
    });
});

savecontacts.php

<?php
require_once './connectDB.php';   

$user = $_POST("user");
$pass = $_POST("pass");

mysqli_query($conn, "insert into tbluser (username, password) values ('$user','$pass')");

echo "Done!";
?>

My problem is - it's not working. It does not execute the php function. Does anybody have an idea about this? I'm new on this type of programming. I'm a desktop developer and now I'm practising web programming.

Thanks

Loi Condes
  • 95
  • 1
  • 2

1 Answers1

2

Try these:

$user = $_POST["user"];
$pass = $_POST["pass"];

In its current state, a 500 error should be thrown for your code; you will see this in your Chrome console when viewing the response (right click -> Inspect -> click Network tab -> look for savecontacts.php) and also your PHP error log (if error reporting is turned on)