I have this JQuery POST code to post data to a .php page for updating DB. Which works fine on a different website (which I wrote), and I just simply copy-pasted the same code and changed some values.
Now the problem occurs at posting the data fetched from inputs to the .php file.
I console logged in every possible place, Didn't understand why is the data not posted to the .php file.
Code goes as:
<form action="backend_functions/logincheck.php" method="post" id="logincheck" ></form>
<div class="form-group mt-4">
<label>Email Address</label>
<input type="text" id="login_form_email" form="logincheck" class="form-control" placeholder="Enter your email address" />
</div>
<div class="form-group">
<label>Password</label>
<input type="password" id="login_form_password" form="logincheck" class="form-control" placeholder="Enter your password" />
</div>
<div class="btn-group mt-3 w-100">
<button type="button" id="login_submit_btn" class="">Log In</button>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$("#login_submit_btn").on('click',function() {
console.log("button pressed");
$(this).attr("disabled", true);
let login_form_email = $("#login_form_email").val();
let login_form_password = $("#login_form_password").val();
console.log("value got at beggining - "+login_form_email);
if(!login_form_email || !login_form_password) {
$(".login_form_error_message_2").css("display","block");
setTimeout(function(){
$(".login_form_error_message_2").css("display","none");
}, 2000);
$(this).attr("disabled", false);
} else {
console.log("onelse");
$.post(
'backend_functions/logincheck.php',
{
login_form_email: login_form_email,
login_form_password: login_form_password,
},
function(result){
console.log(result);
if(result == 'error') {
$(".login_form_error_message").css("display","block");
setTimeout(function(){
$(".login_form_error_message").css("display","none");
}, 2000);
console.log("error returned");
} else {
console.log("success");
}
}
);
$(this).attr("disabled", false);
}
});
</script>
.php file: (query is just for testing.)
<?php include("../db.php");
$login_form_email = $_POST['login_form_email'];
$login_form_password = md5($_POST['login_form_password']);
$date = date_default_timezone_set('Asia/Kolkata');
$date = date('Y-m-d H:i:s');
$date = date('Y-m-d');
$sql = "UPDATE subadmin SET name = '$login_form_email'
WHERE id = '8'";
if ($con->query($sql) === TRUE) {
echo $login_form_email.'success';
} else { echo $login_form_email.'fail'; }
?>
DB.php
<?php
$servername = "localhost";
$dbusername = "dbuser";
$dbpassword = "dbpass";
$dbname = "dbname";
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
define('HOST','localhost');
define('USERNAME', 'dbuser');
define('PASSWORD','dbpass');
define('DB','dbname');
$con = mysqli_connect(HOST,USERNAME,PASSWORD,DB);
$con->set_charset('utf8mb4');
$db = new PDO('mysql:host=localhost;dbname=dbname','dbuser','dbpass');
$admin = 'vdofymail@gmail.com';
//vdofymail@gmail.com
session_start();
?>
Now the console log looks like this:
as you see, I get 2x (success) in the console, one is from the script and the other is from.php file which should be concatenated with the posted var, .i.e., $login_form_email
Can someone explain what am I doing wrong, that it is not posting data to the backend file?
Network Payload for .php file (seems like data is posting.. I guess)
FYI, I have tried static replacing the $login_form_email
in the .php file to see if there is any wrong in there, worked fine.
It's just that, data is not posted.
Any help is greatly appreciated.