I am generating a table via if loop, and if data is not found an is displayed so I can type the data needed.
Then I needed that when I click outside of the textbox the data is saved in the database. the problem I am having is that only the first textbox is saved the others that follow are not being saved.
bellow you can see the related code:
main.php
if(empty($code))
{
?>
<input type="text" id="new_text" placeholder="title" style="width: 80px;">
<div id="check_new_text" style="text-align: right; margin-right: 25px;"></div>
<script>
$("#new_text").blur(function()
{
//alert("This input field has lost its focus.");
var new_text = document.getElementById("new_text").value;
var myRequest = new XMLHttpRequest();
myRequest.open("GET", "save_query.php?code=<?=$code?>&new_text=" + new_text);
myRequest.onreadystatechange = function () {
if (myRequest.readyState === 4) {
document.getElementById('check_new_text').innerHTML = myRequest.responseText;
}
};
myRequest.send();
document.getElementById('reveal').style.display = 'none';
});
</script>
<?php
}
save_query.php
<?php
$code = $_GET['code'];
$new_text = $_GET['new_text'];
if(!empty($new_text))
{
$sql_new_text = "INSERT INTO `table` (`code`, `text`) VALUES ('$code', '$new_text');";
if ($connection->query($sql_new_text) === TRUE)
{
echo "<font color=green style='font-size: 12px;'><i class='fas fa-check'></i> Saved sucessfully.</font>";
}
else
{
echo "<font color=red style='font-size: 12px;'><i class='fas fa-times'></i>Error updating record: " . $connection->error."</font>";
}
}
?>