Last active
August 29, 2015 14:27
-
-
Save igorpronin/f93b8e2d6694c9f54a79 to your computer and use it in GitHub Desktop.
Ajax example 1, get method
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
-------client side | |
<!DOCTYPE html> | |
<html lang="ru"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Работа с json</title> | |
<link rel="stylesheet" href="/jslibs/bootstrap/dist/css/bootstrap.min.css"> | |
<link rel="stylesheet" href="/jslibs/bootstrap/dist/css/bootstrap-theme.min.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="row"> | |
<button class="btn btn-primary btn-success">Получить данные</button> | |
</div> | |
<div class="row"> | |
<div class="panel panel-default"> | |
<div class="panel-body"> | |
<table class="table table-bordered"> | |
<thead> | |
<th>Название товара</th> | |
<th>Марка товара</th> | |
<th>Цена товара</th> | |
<th>Количество товара</th> | |
<th>Описание товара</th> | |
</thead> | |
<tbody id="tablecontent"> | |
</tbody> | |
</table> | |
</div> | |
</div> | |
</div> | |
</div> | |
<script src="/jslibs/jquery/dist/jquery.min.js"></script> | |
<script> | |
$(document).ready(function(){ | |
$('.btn').on({ | |
'click': function(e){ | |
$.ajax({ | |
url: 'json_return.php', | |
type: 'GET', | |
dataType: 'json', | |
success: function(data){ | |
result = ''; | |
for(var i = 0, length = data.length; i < length; i++){ | |
result += '<tr>'; | |
result += '<td>' + data[i].name + '</td>'; | |
result += '<td style="text-align: center;">' + data[i].mark + '</td>'; | |
result += '<td style="text-align: center;">' + data[i].price + '</td>'; | |
result += '<td style="text-align: center;">' + data[i].count + '</td>'; | |
result += '<td>' + data[i].description + '</td>'; | |
result += '</tr>'; | |
} | |
$('#tablecontent').append(result); | |
console.log(data); | |
} | |
}); | |
} | |
}); | |
}); | |
</script> | |
</body> | |
</html> | |
------server side | |
<?php | |
require_once '../connection.php'; | |
$sql = "SELECT products.id_product, products.name, products.mark, products.price, products.count, products.description, | |
catalogs.name as category | |
FROM products | |
LEFT JOIN catalogs ON products.id_catalog = catalogs.id"; | |
$result = $pdo->query($sql); | |
$records = $result->fetchAll(PDO::FETCH_ASSOC); | |
unset($pdo); | |
header("Content-Type: application/json"); | |
echo json_encode($records); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment