Skip to content

Instantly share code, notes, and snippets.

@jagroop
Last active February 21, 2018 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jagroop/62fe0bf0b3440a997dcfcad1f2927caa to your computer and use it in GitHub Desktop.
Save jagroop/62fe0bf0b3440a997dcfcad1f2927caa to your computer and use it in GitHub Desktop.
<?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]);
<!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