So, I need to use ajax in order to submit a form (click any of 6 buttons and increment by 1 a field in the db). I think I got my function correct(well, if I did, it'd be working), but it's still refreshing the page after every submit. What am I doing wrong here?
Both my PHP and HTML are inside the same page (index.php) Here's my code:
<?php
include '/Login/db_login.php';
$page = 'index.php';
if(isset($_POST['ContBt1'])){
$sql = "UPDATE senhas2 SET Contador=Contador+1 WHERE ID = 1";
mysqli_query($conn, $sql);
mysqli_close($conn);
}
if(isset($_POST['ContBt2'])){
$sql = "UPDATE senhas2 SET Contador=Contador+1 WHERE ID = 2";
mysqli_query($conn, $sql);
mysqli_close($conn);
}
if(isset($_POST['ContBt3'])){
$sql = "UPDATE senhas2 SET Contador=Contador+1 WHERE ID = 3";
mysqli_query($conn, $sql);
mysqli_close($conn);
}
if(isset($_POST['ContBt4'])){
$sql = "UPDATE senhas2 SET Contador=Contador+1 WHERE ID = 4";
mysqli_query($conn, $sql);
mysqli_close($conn);
}
if(isset($_POST['ContBt5'])){
$sql = "UPDATE senhas2 SET Contador=Contador+1 WHERE ID = 5";
mysqli_query($conn, $sql);
mysqli_close($conn);
}
if(isset($_POST['ContBt6'])){
$sql = "UPDATE senhas2 SET Contador=Contador+1 WHERE ID = 6";
mysqli_query($conn, $sql);
mysqli_close($conn);
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Index</title>
<link href="index.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body class="Background">
<div class="BtLogin">
<a href="Login/paginalogin.php">
<input type="image" id="admin" src="http://i.imgur.com/I3D3nqm.png">
</a>
</div>
<form action="" id="myform" method="POST" onsubmit="return false">
<table class="Table">
<tr>
<td><input class="Button ButtonTxt" type="button" name="ContBt1" value="A"></td>
<td><input class="Button ButtonTxt" type="button" name="ContBt2" value="B"></td>
</tr>
<tr>
<td><input class="Button ButtonTxt" type="button" name="ContBt3" value="C"></td>
<td><input class="Button ButtonTxt" type="button" name="ContBt4" value="D"></td>
</tr>
<tr>
<td><input class="Button ButtonTxt" type="button" name="ContBt5" value="E"></td>
<td><input class="Button ButtonTxt" type="button" name="ContBt6" value="F"></td>
</tr>
</table>
</form>
<script>
$(function(){
$('.Button').on('click', function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'index.php',
data: $('#myform').serialize(),
success: function () {
alert('The form was submitted successfully');
}
});
});
});
</script>
</body>
</html>
Here's my db_login.php:
<?php
$servername = "localhost";
$username = "root";
$password = null;
$dbname = "pap1";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
?>
<html >
<head>
<meta charset="UTF-8">
<title>Error Page</title>
</head>
<body>
<h1>ERROR</h1>
<center><h2> Error ($dbname) does not exist</h2></center>
</form>
</body>
</html>
<?php
}
?>
, I now get the alert saying the form was submited, but the database values remain unchanged
– Azazel May 08 '17 at 23:18