请输入您的位置信息:
搜索结果:
-
汽车美容装饰
-
地址:XX路XX号
-
电话:XXXXXXXX
-
汽车充电桩
-
地址:XX路XX号
-
电话:XXXXXXXX
CSS代码:
css
body {font-family: Arial, sans-serif;
}h1 {font-size: 24px;margin-bottom: 10px;
}p {font-size: 14px;
}form {width: 500px;margin: 0 auto;
}label {display: block;margin-bottom: 5px;
}input {width: 100%;padding: 5px;border: 1px solid ccc;
}input[type="submit"] {width: 100px;background-color: 007bff;color: fff;border: none;cursor: pointer;
}results {width: 500px;margin: 0 auto;
}h3 {font-size: 18px;margin-bottom: 10px;
}ul {list-style-type: none;padding: 0;
}li {margin-bottom: 5px;
}a {color: 007bff;text-decoration: none;
}
JavaScript代码:
js
function search() {// 获取地址信息var address = document.getElementById("address").value;// 拼接请求地址var url = "search.php?address=" + address;// 创建XMLHttpRequest对象var xhr = new XMLHttpRequest();// 打开请求xhr.open("GET", url);// 设置请求头xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");// 发送请求xhr.send();// 处理响应xhr.onreadystatechange = function() {if (xhr.readyState === 4 && xhr.status === 200) {// 获取响应结果var results = JSON.parse(xhr.responseText);// 清空结果列表var resultList = document.getElementById("results");resultList.innerHTML = "";// 将搜索结果添加到结果列表for (var i = 0; i < results.length; i++) {var result = results[i];var li = document.createElement("li");li.innerHTML = result.name;var a = document.createElement("a");a.href = result.url;a.innerHTML = result.address;li.appendChild(a);resultList.appendChild(li);}}};
}
PHP代码:
php
"汽车美容装饰", "url" => "http://www.example.com/car-beauty", "address" => "XX路XX号");
$results[] = array("name" => "汽车充电桩", "url" => "http://www.example.com/charging-pile", "address" => "XX路XX号");// 输出JSON结果
echo json_encode($results);?>