I am trying to update my database through a JS Function. After googling for a while I understood that it is clearly not possible without using AJAX.
So it is my first time and I tried, here is my JS code:
$("#update").click(function(event){
var showArr = document.forms['ownForm'].elements['showArr'].value;
$.ajax({
url: './../pages/updateDatabase.php',
type: 'POST',
data: {
showArr: showArr
} ,
success: function( msg ) {
alert( "Data Saved: " + msg );
}
});
return false;
});
and my PHP file:
<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=+++', '+++', '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE Users SET SHOWARR = true WHERE ID = 1";
$stmt = $conn->prepare($sql);
$stmt->execute();
echo $stmt->rowCount() . " records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
But nothing happens! Where is my mistake?