gpt4 book ai didi

php - 如何在 http 请求生成的输出中搜索

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:10:33 25 4
gpt4 key购买 nike

我正在使用 THIS用于在其周围范围内搜索邮政编码的 api。例如,当我输入邮政编码 (1234) 时,它会生成自动输出。它为此使用了 jquery 自动完成功能。

现在是我的问题。如何将该输出保存在 session 中并在该 session 中进行搜索以匹配公司数据库中的任何邮政编码?

我的代码是这样的:

我的表单:

<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);

我希望有人知道如何将请求的输出保存到 session /cookie。

最佳答案

如果我很清楚你想要什么,我想当你的功能完成时,你必须调用一个ajax函数来将put发送到服务器上的一个php脚本,这将把输出保存在一个 session 中,你可以做您想要从那里进行的任何处理。

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);

脚本文件

<?php 
session_start();
$output=$_GET['output'];
$_SESSION['postal'][]=$output;
echo $output;
?>

关于php - 如何在 http 请求生成的输出中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16517247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com