Подсказки - PHP Online
Form of PHP Sandbox
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of Подсказки.php
- <?php
- if (!empty($_POST['text']) && is_string($_POST['text'])) {
- header('content-type: application/json'); // говорим что контент JSON
- $query = json_encode(['text' => $_POST['text']]); // сохраняем JSON
- exit; // завершаем скрипт если это POST-запрос
- $url = 'http://ahunter.ru/site/suggest/address?addresslim=3;output=json|pretty;query=' . $query;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- $response = curl_exec($ch);
- $data = json_decode($response, true);
- curl_close($ch);
- var_dump($data);
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Подсказки</title>
- </head>
- <style>
- input {
- padding: 10px;
- }
- .city1 {
- display: none;
- margin: 5px;
- padding: 10px;
- width: 465px;
- }
- .city2 {
- display: none;
- margin: 5px;
- padding: 10px;
- width: 465px;
- }
- .city3 {
- display: none;
- margin: 5px;
- padding: 10px;
- width: 465px;
- }
- .city1:hover {
- cursor: pointer;
- background-color: #d9d9d9;
- }
- .city2:hover {
- cursor: pointer;
- background-color: #d9d9d9;
- }
- .city3:hover {
- cursor: pointer;
- background-color: #d9d9d9;
- }
- .button {
- text-decoration: none;
- padding: 10px;
- border: 1px solid black;
- color: #000;
- transition: 0.7s;
- }
- .button:hover {
- background-color: #d2d2d2;
- transition: 0.7s;
- }
- </style>
- <body>
- <form method="post">
- <input size="70" id="input" value="" type="text" placeholder="Введите город:" />
- <a href="#" class="button">Очистить</a>
- </form>
- <div class="city1"></div>
- <div class="city2"></div>
- <div class="city3"></div>
- <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
- <script>
- window.addEventListener('DOMContentLoaded', function() {
- var input = document.querySelector('input#input');
- input.addEventListener('input', function(e) {
- // Избавляемся от пустых запросов к API
- if (e.target.value === '' || e.target.value === null) {
- return;
- }
- $.ajax({
- type: 'POST',
- url: '',
- dataType: 'JSON', // говорим JQUERY что ждём JSON
- data: {
- text: e.target.value
- },
- success: function(r) {
- // r хранит пришедший JSON в виде JS объекта или массива
- console.log(r['text']);
- }
- });
- });
- });
- </script>
- </body>
- </html>
File Description
- Подсказки
- PHP Code
- 09 Aug-2022
- 3.15 Kb
You can Share it: