Hello
I’m having some troubles with my code. My current code couse that I’m doing should give me alert which depends on infromation from server - if email was written to subscribe list or not. And it almost works - the only problem is that I’m getting only false alert, even if the emal has been written.
Below is some code
Here is JS script:
$("button.submit").click(function() {
$.ajax({
type: "POST",
url: "email.php",
data: { email: $(".email").val() },
success: function(result) { console.log(result)
alert(result ? "Email has been added to our subscribe list!" : "Email already exist in our subscribe list!");
},
error: function(jqXHR, textStatus, errorThrown) {
console.error(errorThrown);
}
})
And there is PHP file :
$email = filter_input(INPUT_POST, 'email');
if (!empty($email)){
$host = "localhost";
$dbusername ="root";
$dbpassword ="";
$dbname = "email";
$conn = new mysqli ($host,$dbusername, $dbpassword , $dbname);
if (mysqli_connect_error()){
die('Connection problem('.mysqli_connect_errno().')'.mysqli_connect_error());
}
else{
$sql = "INSERT INTO `email` (`email_id`, `email`) VALUES (NULL, '$email');";
if ($conn->query($sql)){
$return = true;
}
else{
$return = false;
}
$conn->close();
}}
?>
Do you have any ideas what did I do wrong ? My JS level is kinda low ( I’m still new to JS )
I’d be grateful for your advices