我使用THIS api搜索周围范围内的邮政编码。 例如,当我输入邮政编码(1234)时,它会生成一个自动输出。 它正在使用jquery自动完成功能。
现在是我的问题。 如何在会话中保存该输出并在该会话中搜索以匹配我公司数据库中的任何邮政编码?
我的代码如下:
我的表格:
<form action="#" method="post" class="pro6pp_range">
<input type="text" class="postcode" placeholder="Postcode (1234)" maxlength="4">
<select class="range">
<option value="5" selected="selected">5 km</option>
<option value="10">10 km</option>
<option value="15">15 km</option>
<option value="20">20 km</option>
<option value="25">25 km</option>
</select>
<br/><br/>
<span class="message"></span>
<div class="output"></div>
<br/>
</form>
我的javascript:
<script type="text/javascript">
var pro6pp_auth_key = "MY_AUTH_KEY";
$(document).ready(function() {
$(".pro6pp_range").applyRange({ 'assemble_func': assemble_one });
function assemble_one(result) {
distance_km = Math.round(result.distance / 1000);
return "Afstand tot " + result.nl_fourpp + " is " +
distance_km + " km vanaf uw locatie.<br />";
}
});
</script>
Range.js:
// Create closure to keep namespace clean and hide implementation.
(function($) {
$.fn.applyRange = function(options) {
var instance = this;
function getConfig(field, default_value) {
if (typeof options === 'undefined' ||
typeof options[field] === 'undefined') {
return default_value;
} else {
// Developer chose to specify form field manually.
return options[field];
}
}
instance.postcode = getConfig('postcode', instance.find('.postcode'));
instance.range = getConfig('range', instance.find('.range'));
instance.message = getConfig('message', instance.find('.message'));
instance.spinner = getConfig('spinner', instance.find('.spinner'));
instance.output = getConfig('output', instance.find('.output'));
instance.distance = getConfig('distance', instance.find('.distance'));
instance.lat = getConfig('lat', instance.find('.lat'));
instance.lng = getConfig('lng', instance.find('.lng'));
instance.nl_fourpp = getConfig('nl_fourpp', instance.find('.nl_fourpp'));
instance.assemble_func = getConfig('assemble_func', assemble_one);
instance.postcode.keyup(function() {
range(instance);
});
};
var pro6pp_cache = {};
function pro6pp_cached_get(obj, url, params, callback) {
var key = url + $.param(params);
if (pro6pp_cache.hasOwnProperty(key)) {
if (typeof callback !== 'undefined') {
callback(obj, pro6pp_cache[key]);
}
} else {
$.getJSON(url + "?callback=?", params, function(data) {
pro6pp_cache[key] = data;
if (typeof callback !== 'undefined') {
callback(obj, data);
}
});
}
}
function getApiBaseUrl() {
// Use HTTPS API if website itself is also secure.
// Otherwise, some browsers might complain about insecure content.
if ("https:" === document.location.protocol) {
return 'https://pro6pp.appspot.com/v1';
} else {
return 'http://api.pro6pp.nl/v1';
}
}
function range(obj) {
obj.message.hide().empty();
var postcode = obj.postcode.val();
var range = obj.range.val();
// Trigger on '5408'
var nl_fourpp_regex = /[0-9]{4,4}\s?/;
if (nl_fourpp_regex.test(postcode)) {
var url = getApiBaseUrl() + "/range";
var params = new Object();
params.auth_key = pro6pp_auth_key;
params.per_page = 100;
params.nl_fourpp = postcode;
// User puts in range in kilometers, API uses meters.
range = parseInt(range, 10) * 1000;
params.range = range;
pro6pp_cached_get(obj, url, params, fillin);
}
}
function assemble_one(result) {
distance_km = Math.round(result.distance / 1000);
return "Afstand tot " + result.nl_fourpp + " is " + distance_km + " km<br />";
}
function fillin(obj, json) {
if (json.status == 'ok') {
obj.output.empty();
var postcode = obj.postcode.val();
$.each(json.results, function(i, result) {
if (result.nl_fourpp === postcode) {
return true;
}
obj.output.append(obj.assemble_func(result)).show();
});
if (json.results.length > 1) {
obj.distance.val(json.results[1].distance);
obj.nl_fourpp.val(json.results[1].nl_fourpp);
obj.lat.val(json.results[1].lat);
obj.lng.val(json.results[1].lng);
} else {
translated_message = 'Geen resultaten gevonden';
obj.message.html(translated_message).show();
}
} else {
var translated_message = json.error.message;
if (json.error.message == 'nl_fourpp niet gevonden') {
translated_message = 'Onbekende postcode';
} else if (json.error.message == 'Invalid nl_fourpp format') {
translated_message = 'Ongeldig postcode formaat';
} else if (json.error.message == 'invalid target_nl_fourpps format') {
translated_message = 'Ongeldig target_nl_fourpps formaat';
}
obj.message.html(translated_message).show();
}
}
})(jQuery);
我希望有人知道如何将请求的输出保存到会话/ cookie。
答案 0 :(得分:1)
如果我理解你想要什么,我认为当你完成功能时,你必须调用ajax函数将put发送到服务器上的php脚本,这将在会话中保存输出,你可以做你想要的任何处理。
function send_data_to_server(output)
{
var postal_code=$('.postcode').val();
var url = 'http://kees.een-site-bouwen.nl/script.php?output='+output+'&postal_code='+postal_code;
$.ajax({
url : url,
success : function (data)
{
alert(data);
}
});
}
就在您返回之前“Afstand tot”+ result.nl_fourpp +“is”+ distance_km +“km vanaf uw locatie。
”;
放下这行代码:
send_data_to_server(result.nl_fourpp);
的script.php
<?php
session_start();
$output=$_GET['output'];
$_SESSION['postal'][]=$output;
echo $output;
?>