Last active
February 21, 2018 10:33
-
-
Save jagroop/62fe0bf0b3440a997dcfcad1f2927caa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'init.php'; | |
$done = false; | |
if(isset($_POST['username'], $_POST['userjob'])) | |
{ | |
$data = [ | |
'name' => $_POST['username'], | |
'job' => $_POST['userjob'] | |
]; | |
$done = true; | |
} | |
echo json_encode(['success' => $done]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Ajax Save to DB example</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> | |
</head> | |
<body> | |
<form action="" method="POST" role="form"> | |
<legend>Registration</legend> | |
<div class="form-group"> | |
<label for="">Name</label> | |
<input type="text" name="name" class="form-control" id="name" placeholder="Input field"> | |
</div> | |
<div class="form-group"> | |
<label for="">Job</label> | |
<input type="text" name="job" class="form-control" id="job" placeholder="Input field"> | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
</form> | |
<script> | |
$(document).ready(function(){ | |
$('button').click(function(e){ | |
e.preventDefault(); | |
var name = $('#name').val(); | |
var job = $('#job').val(); | |
$.post('ajax.php', {username : name, userjob : job}, function(res, s){ | |
if(s === 'success') | |
{ | |
if(res.success == true) | |
{ | |
return alert('Data inserted !!'); | |
} | |
return alert('ERROR'); | |
} | |
}, 'json'); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment