简体   繁体   English

结合php ajax jquery来实现由sql填充的下拉列表

[英]combining php ajax jquery for drop downs populated by sql

I am trying to get three different bits of php to work in order and work in a way that is easy for the user. 我正在尝试使php的三个不同部分按顺序工作,并且以对用户来说容易的方式工作。 This is the some of the code I have already that gets data from a database. 这是我已经从数据库获取数据的一些代码。 I have a similar statement to this in a drop down that I need to populate using the answer from this first bit of code when it changes I also need to take the answers of both of these and put them into another sql statement that will display a set of results. 我在下拉列表中有一个与此类似的语句,当它更改时,我需要使用第一部分代码中的答案进行填充,我还需要同时获取这两个答案,并将它们放入另一个显示以下内容的sql语句中结果集。 I know I need to use something like jquery or ajax but I am unsure of how to use it in WordPress to get the desired effect. 我知道我需要使用诸如jquery或ajax之类的东西,但是我不确定如何在WordPress中使用它来获得理想的效果。

<select name="raceTrack2" onchange="">
      <?php
            $postids = $wpdb->get_col("SELECT trackName FROM " . $trackTableName . ";");

            foreach ($postids as $value) 
            {
                echo '<option>' . $value . '</option>' ;

            }

      ?>
</select>

Any help would be appreciated 任何帮助,将不胜感激

There are several ways to skin this cat. 有几种方法可以给这只猫蒙皮。 Since you mentioned jQuery/Ajax I will guide you through the high level steps using those technologies: 自从您提到jQuery / Ajax以来,我将指导您使用这些技术完成高级步骤:

  1. Write a javascript function that makes an Ajax call to a PHP script that returns the data in JSON format to populate your 2nd dropdown with. 编写一个JavaScript函数,该函数对PHP脚本进行Ajax调用,该脚本以JSON格式返回数据,以填充第二个下拉列表。 Inside your ajax() method, upon successful completion of the call, parse the JSON data and construct HTML code for the values for your 2nd dropdown. 在您的ajax()方法内部,成功完成调用后,解析JSON数据并为第二个下拉列表的值构造HTML代码。 Then using jQuery selectors , select your 2nd dropdown and set the HTML contents of it using html() method. 然后使用jQuery 选择器 ,选择第二个下拉列表,并使用html()方法设置其HTML内容
  2. Modify the onchange() event handler for your 1st dropdown to call the JS function created in Step 1. 修改第一个下拉菜单的onchange()事件处理程序,以调用在步骤1中创建的JS函数。
  3. Write another javascript function that grabs the selected value from your 1st dropdown and that from your 2nd dropdown. 编写另一个JavaScript函数,该函数从第一个下拉菜单和第二个下拉菜单中获取所选值。 Then make another Ajax call to a PHP script that takes those two values as input and does whatever it needs to do with those values. 然后,对PHP脚本进行另一个Ajax调用,该脚本将这两个值用作输入,并对这些值进行任何处理。
  4. Lastly, modify the onchange() event handler for your 2nd dropdown to call the JS function created in Step 3. 最后,修改第二个下拉菜单的onchange()事件处理程序,以调用在步骤3中创建的JS函数。

At a high level, this should do it. 在较高级别,这应该做到。 As I said, there are many ways to skin this cat. 正如我说的,有很多方法可以给这只猫蒙皮。 This is probably the simplest way to go and does not require a post-back/round-trip to your server. 这可能是最简单的方法,不需要回退/往返服务器。

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

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