使用 AJAX 更改 smarty 变量的值;多项选择

发布于 2024-10-25 03:31:11 字数 958 浏览 2 评论 0原文

我在页面上有 3 个选择,第二个和第三个选择应填充选项,具体取决于第一个选择中选择的值。 例如:第一个选择有一个国家/地区列表,如果我从此选择中选择国家 A,第二个选择将填充来自该国家/地区的城市,第三个选择将填充,我不知道,该国最受欢迎的名字。

我想用AJAX来实现这一点,但由于该网站是使用Smarty编写的,所以我遇到了困难。 每个选择都由一个从 php.ini 分配的数组填充。我想以某种方式更改第二个和第三个数组的值,而不重新加载页面,具体取决于我从第一个选择中获得的 id。

我尝试请求将值分配给 smarty 的页面,并尝试更改数组,但它在前端不起作用。 有什么想法吗?

编辑: 代码看起来像这样:

$countries = Country::getCountries();
$cities = City::getCities($country_id); //the parameter is not necessary 
$names = Name::getNames($country_id); //the parameter is not necessary 

$smarty->assign("countries",$countrires);
$smarty->assign("cities",$cities);
$smarty->assign("names",$names);
//display template etc

在模板页面上

<select name="countries">
{foreach from=$countries item=country}
   <option value="{$country.id}">{$country.name}</option>
{/foreach}
</select>

,其他两个选择看起来相同。

I have 3 selects on a page, the 2nd and 3rd select should populate with options depending on the value selected in the first select.
For instance: the first select has a list of countries, if I select country A from this select, the 2nd select will populate with cities from this country, the 3rd select populates with, i don't know, most popular names in the country.

I want to realize this with AJAX, but because the site is written with the use of Smarty, I'm having a hard time.
Each select is populated by an array which is assigned from a php. I would like to somehow change the values of the 2nd and 3rd array, without reloading the page, depending on an id I get from the 1st select.

I tried requesting the page that assigns the values to smarty and tried changing the arrays but it didn't work in the frontend.
Any ideas?

EDIT:
The code looks something like this:

$countries = Country::getCountries();
$cities = City::getCities($country_id); //the parameter is not necessary 
$names = Name::getNames($country_id); //the parameter is not necessary 

$smarty->assign("countries",$countrires);
$smarty->assign("cities",$cities);
$smarty->assign("names",$names);
//display template etc

on the template page

<select name="countries">
{foreach from=$countries item=country}
   <option value="{$country.id}">{$country.name}</option>
{/foreach}
</select>

and the 2 other selects look the same.

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

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

发布评论

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

评论(2

能怎样 2024-11-01 03:31:11

查看相关代码肯定会有所帮助,但我将简要描述您可能应该采取的过程:

(1) 完全按照您在任何框架内设置任何其他页面的方式设置城市页面或者可能没有使用。

(2) 城市页面的输出模板应如下所示(无标题或包含):


{foreach $cities 作为 $city}
<选项值=“{$city.id}”>{$city.name}
{/foreach}

(3) 城市页面必须能够接受一些参数来指定我们应该选择哪个国家/地区的城市,例如使用 GET 变量。

(4) AJAX 调用应使用回调请求 url(例如,/cities?country=USA),以用响应替换城市选择元素的innerHTML。

(5) 没有步骤5。

It would certainly help to see the code for this, but I'll briefly describe the process that you should probably be taking:

(1) Set-up the cities page exactly as you'd set-up any other within whatever framework you may or may not be using.

(2) The output template for the cities page should look something like this (no header or includes):


{foreach $cities as $city}
<option value="{$city.id}">{$city.name}</option>
{/foreach}

(3) The cities page must be able to accept some parameter for specifying what country we should select cities for, for example, with a GET variable.

(4) The AJAX call should request the url (for example, /cities?country=USA) with a callback to replace the innerHTML of the cities select element with the response.

(5) There is no step 5.

怕倦 2024-11-01 03:31:11

countries select 调用了一个函数,该函数执行以下操作:

function updateSelect(){     
 var country_id = document.getElementById("country_id").value;
 ajax.requestFile = 'ajax.php?country_id='+sec_id;
 ajax.onCompletion = todo;
 ajax.runAJAX();
}

function todo(){
 var value = eval(ajax.response);
 document.getElementById('city_tr').style.display = 'table-row';
 if (value !=undefined) document.getElementById('td_city').innerHTML = value ;
             else document.getElementById('city_tr').style.display = 'none';
}

ajax.php 脚本回显基于它获得的 id 生成的 select。

The countries select called a function, which did the following:

function updateSelect(){     
 var country_id = document.getElementById("country_id").value;
 ajax.requestFile = 'ajax.php?country_id='+sec_id;
 ajax.onCompletion = todo;
 ajax.runAJAX();
}

function todo(){
 var value = eval(ajax.response);
 document.getElementById('city_tr').style.display = 'table-row';
 if (value !=undefined) document.getElementById('td_city').innerHTML = value ;
             else document.getElementById('city_tr').style.display = 'none';
}

the ajax.php script echoed a select generated based on the id it got.

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