I am currently making a Chrome extention, where I can input my current mood and it should write my mood in my MySQL Database. I have the GUI all set up, but now only the backend part is missing. I know how to make it in PHP, but since a Chrome extention is on the client site. PHP isn’t a choise. So I made a PHP script, which updates the DB acording to the input its given. But how can I “Send” the Button clicks to the PHP server? I tried it with AJAX but with no success.
This is the html code for the buttons:
<div id="buttons">
<input type="button" id="bad" value="bad"></button>
<input type="button" id="normal" value="normal"></button>
<input type="button" id="great" value="great"></button>
</div>
And this is the js code for one button(I dont think I have to send all three here):
document.getElementById("normal").addEventListener("click", function() {
xhr.send("feeling=normal")
if (xhr.status === 200) {
alert(xhr.responseText);
} else {
alert('Request failed. Returned status of ' + xhr.status);
}
alert("Thank you for your feedback");
});
And this is my PHP script:
<?php
if (isSet($_POST['feeling'])) {
$con = mysqli_connect('localhost', 'NoUsernameNamed', 'YouWontGetMyPassword', 'JustADatabase');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
echo "Connected to Server";
}
$stmt = mysqli_prepare($con, 'INSERT INTO moods VALUES (?)');
mysqli_stmt_bind_param($stmt, 'i', $_POST['feeling']);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
mysqli_close($con);
}
?>
When I click one of the tree Buttons, then the Database should Add the mood(Can be stored as an INT(bad=1,normal=2, great=3) or as a String)