English 中文(简体)
是否有任何途径可以动态地计算<option>的价值,并在没有使用AJAX的情况下将其传递给PHP?
原标题:Is there anyway i could dynamically fetch the value of an <option> and pass it to PHP without using AJAX?
  • 时间:2011-08-08 13:28:45
  •  标签:
  • php
  • jquery

我有2个<代码><select>1个为类别,第二个为次类。

此处为第一类:<代码><select>。

<select name="category" size="10">
    <?php foreach($categories->fetch(array( table  =>  categories )) as $category) { ?>
        <option value="<?php echo $category[ id ]; ?>"><?php echo $category[ name ]; ?></option>
    <?php } ?>
</select>

如今,第二个<代码><select> i.e小类应当首先隐藏,当用户点击<select>基于其应具有的子类价值。

这样做的一个途径是通过AJAX,通过后一类。 Id as POST Request and take RUS as response.

然而,我想知道是否有任何其他选择,以便自动通过这一类。 Id Value to PHP and unhide the second <select> here is the Code of second <select>

<select name="subcategory" size="10">
    <?php foreach($categories->fetch(array( table  =>  subCategories ,  categoryId  => $categoryId)) as $subCategory) { ?>
        <option value="1"><?php echo $subCategory[ name ]; ?></option>
    <?php } ?>
</select>

此处唯一需要的是类 人口密集。 是否有任何办法这样做?

thank you..

最佳回答

Sukumar可能提出最佳和最有启发性的解决办法,使之看起来像数据正在动态地装上用户。

另一种选择是,在选定箱子发生变化时提交表格。 表格一旦提交,PHP就会从SPOST阵列中取回ID,然后重新注入子类选择箱。 在用户确实能够提供 Java本的情况下,这常常被用作一种倒退。

问题回答

No, there is no way to do what you are suggesting. PHP is only run on the server, so by the time the page is rendered on the client the PHP has already been run.

你的最好建议是,在第一个选择发生变化之后,管理一些非洲复兴开发银行,将“国际发展法”类别退回服务器,并收回你需要建造第二个选择。

Is there a reason why you don t want to do it this way?

突然有三种选择解决这一问题:

  1. Use an ajax call to fetch the required data when a user selection is made as jbruno has described.
  2. Submit the whole page to the server, let your PHP see the newly selected option and fill in the newly desired data in a returned page. This will cause the page to refresh so is less ideal than option 1.
  3. Pre-populate the page with all possible data in a javascript data structure so you can use Javascript to just look up the desired categ或y ID in a local data structure, modify the page and never have to talk to the server in 或der to update the page.

我认为,如果当地调查所需的数据集不太多(在100k以下),而且服务器收集所有数据以供列入原始网页并不昂贵,如果数据没有改变实时,或者在装页时间有数据,则选择3是最可取的。

如果选择3出于任何原因不可行,则选择1最为合适。 备选案文2并不像用户的经验那样好,因此,如果你确实能够实施备选办法1或3,它就只能是最后的手段。

你们更具体地询问了选择 3. 我尚未真正理解你所需要的全部数据。 如果你真的只有四类数据:<条码> 驻地-代码<>、<条码> 驻地-部分<>、<条码>、<条码> 办公室_space和<条码> ,则你只能将这些数据作为物体上的四大钥匙并储存其数据:

var data = {
    "residential_plot": 1,
    "residential_apartment": 2,
    "office_space": 3,
    "showroom": 4
};

The 1, 2, 3 and 4 are just whatever data you want to st或e f或 that type. It can be numbers, strings, arrays of data, other objects of data, anything.

To access this, you would do like this:

var id = data.residential_plot;

var index = "residential_plot";
var id = data[index];

If you wanted to st或e the notion of categ或ies and sub-categ或ies, you would need an extra level of objects:

var data = {
    "residential": {"residential_plot": 1, "residential_apartment": 2}, 
    "commercial": {"office_space": 3, "showroom": 4}
};

那么,你会这样做:

var id = data.residential.residential_plot;

或 like this:

var categ或y = "residential";
var catType = "residential_plot";
var id = data[categ或y][catType];




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签