Skip to content

Instantly share code, notes, and snippets.

@duyet
Created May 20, 2015 12:12
Show Gist options
  • Save duyet/0d95768bbfa375ffbfe0 to your computer and use it in GitHub Desktop.
Save duyet/0d95768bbfa375ffbfe0 to your computer and use it in GitHub Desktop.
demo ajax
<?php
// MYSQL Connect here
// mysql_connect('...........');
$id = (isset($_REQUEST['id'])) ? intval($_REQUEST['id']) : 0;
if ($id) {
// Mysql query here
// $result = mysql_query("SELECT * FROM .........");
$result = array('customer_addr' => 'Some where', 'customer_name' => 'Van-Duyet Le');
header('Content-Type: application/json');
echo json_encode($result);
}
<!DOCTYPE html>
<html>
<head>
<title>DEMO</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
function reloadData() {
var id = $('#input_id').val();
$.ajax({
url: 'ajax.php',
type: 'POST',
data: {id:id},
success:function(data){
$('#input_name').val(data.customer_name);
$('#input_addr').val(data.customer_addr);
}
});
}
</script>
</head>
<body>
<div class="container">
<div class="col-md-10">
<form action="#" method="POST" role="form">
<legend>Demo Ajax</legend>
<div class="form-group">
<label for="">ID</label>
<input type="text" class="form-control" id="input_id" onchange="reloadData()">
</div>
<div class="form-group">
<label for="">Name</label>
<input type="text" class="form-control" id="input_name">
</div>
<div class="form-group">
<label for="">Address</label>
<input type="text" class="form-control" id="input_addr" >
</div>
</form>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment