数据库支持的输入字段的代理过滤器值

发布于 2024-11-04 01:00:03 字数 2503 浏览 0 评论 0原文

背景

对于国家/地区字段,系统检测并默认为加拿大。对于城市字段,用户输入加拿大城市名称,并以 Facebook 风格填充。

国家/地区列表和城市列表均使用 AJAX 和 JSON 填充,这些数据来自使用代理的远程数据库(通过 PHP 查询)。

使用 MaxMind 的 GeoIP PHP API,该国家/地区默认为用户的原籍国。

当用户选择不同的国家/地区时,其两个字母的 ISO 代码必须作为参数传递给城市输入,以便驱动它的查询可以搜索所选国家/地区的城市。

例如,IP地址在加拿大的用户可以选择美国。当这种情况发生时:

  1. 城市字段将被删除;
  2. 城市领域获得关注;然后
  3. 仅显示美国城市(根据用户类型)。

问题

以下 PHP 代码片段将城市列表的检索代理到远程服务器:

echo file_get_contents( 'http://server/city.dhtml?q=' . 
  urlencode( $_GET['q'] ) );

jQuery tokenInput 函数似乎没有提供传递附加内容的机制。相关的 jQuery 片段如下。

获取国家/地区

以下代码获取国家/地区代码和国家/地区列表。它将用户的国家/地区设置为默认国家/地区。

  // Where in the world?
  $.getJSON( 'geoip.dhtml', function( data ) {
    country = data[0].id;
  });

  // Select from countries that have significant amounts of data.
  $.getJSON( 'country.dhtml', function( data ) {
    var h = '';
    var len = data.length;

    for( var i = 0; i < len; i++ ) {
      var s = '';

      // Make the person's own country the default selection.
      if( data[i].id == country ) {
        s = '" selected="selected"';
      }

      h += '<option value="' + data[i].id + s + '">' + data[i].name + '</option>';
    }

    $('#country').html(h);
  });

获取城市

以下代码下载城市列表。 tokenInput 函数自动将 q 参数传递给 city.dhtml 的“本地版本”内的代码(PHP 源代码片段显示为多于)。 q 参数是用户为城市键入的文本。

  $('#city').tokenInput( 'city.dhtml', {
    hintText: "Type a city name.",
    tokenLimit: 1,
    classes: {
      tokenList: "token-input-list-facebook",
      token: "token-input-token-facebook",
      tokenDelete: "token-input-delete-token-facebook",
      selectedToken: "token-input-selected-token-facebook",
      highlightedToken: "token-input-highlighted-token-facebook",
      dropdown: "token-input-dropdown-facebook",
      dropdownItem: "token-input-dropdown-item-facebook",
      dropdownItem2: "token-input-dropdown-item2-facebook",
      selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
      inputToken: "token-input-input-token-facebook"
    }
  });

city.dhtml 必须按国家/地区代码过滤城市。

问题

如何在不使用 cookie 的情况下向 city.dhtml 提供国家/地区代码?

想法

最新版本的token输入支持JSONP,有用吗?

谢谢你!

Background

For the country field, the system detects and defaults to Canada. For the city field, the user types a Canadian city name, populated Facebook-style.

Both the country list and city list are populated using AJAX and JSON, sourced from a remote database using a proxy (queried via PHP).

The country is defaulted to the user's country of origin using MaxMind's GeoIP PHP API.

When the user selects a different country, its two-letter ISO code must be passed as a parameter to the city input so that the query that drives it can search for cities in the selected country.

For example, a user whose IP address is in Canada can choose the United States. When this happens:

  1. the city field will be erased;
  2. the city field gains focus; and then
  3. only U.S. cities are displayed (as the user types).

Problem

The following PHP snippet proxies retrieval of the city list to a remote server:

echo file_get_contents( 'http://server/city.dhtml?q=' . 
  urlencode( $_GET['q'] ) );

The jQuery tokenInput function does not seem to provide a mechanism to pass additional content. The relevant jQuery snippets follow.

Acquire Countries

The following code gets a country code and a list of countries. It sets the user's country as the default country.

  // Where in the world?
  $.getJSON( 'geoip.dhtml', function( data ) {
    country = data[0].id;
  });

  // Select from countries that have significant amounts of data.
  $.getJSON( 'country.dhtml', function( data ) {
    var h = '';
    var len = data.length;

    for( var i = 0; i < len; i++ ) {
      var s = '';

      // Make the person's own country the default selection.
      if( data[i].id == country ) {
        s = '" selected="selected"';
      }

      h += '<option value="' + data[i].id + s + '">' + data[i].name + '</option>';
    }

    $('#country').html(h);
  });

Acquire Cities

The following code downloads the list of cities. The tokenInput function automatically passes the q parameter to the code inside the "local version" of city.dhtml (the PHP source code snippet is shown above). The q parameter is the text that the user types for the city.

  $('#city').tokenInput( 'city.dhtml', {
    hintText: "Type a city name.",
    tokenLimit: 1,
    classes: {
      tokenList: "token-input-list-facebook",
      token: "token-input-token-facebook",
      tokenDelete: "token-input-delete-token-facebook",
      selectedToken: "token-input-selected-token-facebook",
      highlightedToken: "token-input-highlighted-token-facebook",
      dropdown: "token-input-dropdown-facebook",
      dropdownItem: "token-input-dropdown-item-facebook",
      dropdownItem2: "token-input-dropdown-item2-facebook",
      selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
      inputToken: "token-input-input-token-facebook"
    }
  });

The city.dhtml must to filter the cities by country code.

Question

How would you provide the country code to city.dhtml, without using a cookie?

Ideas

The latest version of the token input supports JSONP, would that be useful?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈场末日恋爱 2024-11-11 01:00:03

问题是一旦设置了输入,tokenInput URL 参数就无法更改。虽然也许可以删除现有的 tokenInput 并在每次选择新国家/地区时重新创建它,但解决方案将是一种 hack。

解决方案是使用 jQuery TokenInput 的补丁,该补丁允许根据函数调用的结果设置 URL 参数。

https://github.com/loopj/jquery-tokeninput/pull/77

应用补丁并使用以下代码动态更改查询:

  function cityURL() {
    return 'city.dhtml?c=' + $('#country').val();
  }

  $('#city').tokenInput( cityURL, { /* ... */ });

并在 jquery.tokeninput.js 中禁用缓存(第 650 行附近):

    //var cached_results = cache.get(query);
    var cached_results = false;

The problem is that the tokenInput URL parameter cannot be changed once the input has been set. While perhaps possible to delete the existing tokenInput and recreate it each time a new country is selected, the solution would be a hack.

The solution is to use a patch for the jQuery TokenInput that allows setting the URL parameter based on the result from a function call.

https://github.com/loopj/jquery-tokeninput/pull/77

Apply the patch and use the following code to change the query dynamically:

  function cityURL() {
    return 'city.dhtml?c=' + $('#country').val();
  }

  $('#city').tokenInput( cityURL, { /* ... */ });

And disable caching in jquery.tokeninput.js (near line 650):

    //var cached_results = cache.get(query);
    var cached_results = false;
单挑你×的.吻 2024-11-11 01:00:03

很好的问题,+1!是什么让您无法这样做:

$('#city').tokenInput('city.dhtml?c=' + $('#country').val(), { /* ... */ });

您可能需要将其包含在 onChange 事件中,但它应该可以工作。

Neat question, +1! What's keeping you from just doing:

$('#city').tokenInput('city.dhtml?c=' + $('#country').val(), { /* ... */ });

You probably need to enclose that within a onChange event, but it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文