index.php
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('#submit').click(function(){
var s = $('#submit').val();
var nm = $('#name').val();
var em = $('#mail').val();
console.log("Starting ajax");
$.ajax({
url: "./records.php",
type: "post",
data: {
submit:s,
name:nm,
mail:em
},
success:function(data) {
alert('done');
}
});
});
});
</script>
</head>
<body>
<?php
if(isset($_GET['id'])) {
$id = $_GET['id'];
$query = mysqli_query($conn, "SELECT FROM form WHERE my_id = '".$id."' ");
$q = mysqli_fetch_array($query);
}
?>
<form>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="mail" id="mail"></td>
</tr>
<tr>
<td></td>
<?php
if(isset($_GET['id']) > 1)
{
?>
<input type = "submit" class = "btn btn-primary" style="width:49%" value = "Save" name = "submit">
<?php
} else {
?>
<input type = "hidden" name = "uid" id = "uid">
<input type = "submit" class = "btn btn-primary" style="width:49%" value = "Update" name = "submit">
<?php
}
?>
</tr>
</table>
</form>
</body>
</html>
How to use same button for insert & update, make single form with PHP, jQuery & ajax. so solve the problem so its make single button for insert and update and also give me idea how to make it in one form with one button and perform insert and update operation