简体   繁体   English

使用PHP下拉框

[英]drop down box using php

I have a drop down box it pulls values from db.It will display only one column in drop down box.If i select one column in drop down box,it should display other values in text box. 我有一个下拉框,它从db中提取值。它将仅在下拉框中显示一列。如果我在下拉框中选择一列,则应在文本框中显示其他值。

I have done displaying values in drop down box but i am struck here.If i select a value in drop down corresponding values has to be displayed in text box 我已经完成了在下拉框中显示值的操作,但是我很惊讶。如果我在下拉菜单中选择一个值,则必须在文本框中显示相应的值

eg: car bmw classc 125hp 例如:宝马Classc汽车125hp

here if i select bmw in drop down box i should get that class c and 125ho in text box. 如果我在下拉框中选择bmw,则应该在文本框中输入c类和125ho。

you could store your display value in an attribute - eg data-text 您可以将显示值存储在属性中-例如数据文本

  <select id="car">
    <option name="none" data-text="" value="">---</option>
    <option name="bmw" data-text="class c" value="bmw">bmw</option>
    <option name="ford" data-text="4 door" value="ford">ford</option>
  </select>

  <input type="text" id="display"/>

you can update the text field as follows (if you are using jquery) 您可以按以下方式更新文本字段(如果使用的是jquery)

​$()​.ready(function() {

  $("#car").bind("change", function() {
     $("#display").val($("#car :selected").attr("data-text"))
  })
})​

I put a live example at - http://jsbin.com/ifefo4 我在-http://jsbin.com/ifefo4上放了一个实时示例

PHP works server-side, it processes your web site when requested by the client, then sends the result page to the client. PHP在服务器端工作,它在客户端请求时处理您的网站,然后将结果页面发送到客户端。 For instance, your drop box is filled on the server and sent to the client. 例如,您的投递箱已在服务器上填充并发送给客户端。

Selecting an item in the drop box, however, is done in the client's browser. 但是,在客户端的浏览器中选择了下拉框中的一项。 Alas, the server does not know about it, hence there is no way for your PHP script to react to this (unless you force a refresh of the site everytime the user changes the dropbox item, which would be an extremely bad concept). the,服务器对此一无所知,因此您的PHP脚本无法对此做出反应(除非您每次用户更改保管箱项目时都强制刷新网站,这是一个非常糟糕的概念)。

You will need to work with a script type that works on the client end, JavaScript is most common here. 您将需要使用可在客户端上使用的脚本类型,在这里JavaScript最常见。 A JavaScript can react on a drop box selection and fill in the data in your text fields accordingly. JavaScript可以对下拉框选择做出反应,并相应地在文本字段中填写数据。

Note, however, that the JavaScript on the client side has no access to any server resources, say texts from your database. 但是请注意,客户端的JavaScript无法访问任何服务器资源,例如数据库中的文本。 Those resources would have to be inserted into the JavaScript by your PHP script so they get sent over to the client as well. 这些资源必须由您的PHP脚本插入到JavaScript中,以便它们也可以发送到客户端。

You must use a Javascript for client-side. 您必须在客户端使用Javascript。

Make a javascript array that you fill with the values for the textbox. 创建一个用文本框的值填充的javascript数组。 Then on the onchange method for the dropbox you reload the content in the textbox from this array. 然后,在保管箱的onchange方法上,从该数组重新加载文本框中的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM