使用jQuery动态链接的选择框 [英] Dynamic Chained SELECT Boxes using jQuery

查看:160
本文介绍了使用jQuery动态链接的选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据

我使用一个jQuery / JS / AJAX脚本,动态地从我的数据库填充选择框,每个后续的选择框被填充的previous选择。

例如。从我的第一选择某人选择英格兰,这则填充接下来选择与英国等城镇。

我有越来越第二选择功能来识别变量的麻烦。这是一个很大的code,所以我不想粘贴所有,但...这是第一个函数,它填充的第一个选择框的页面加载速度:

 函数getTierOne()
{
 $ tblname = $ _SESSION ['国家'];
 $结果= mysql_query(SELECT DISTINCT县从$ tblname ORDER BY县)
 或死亡(mysql_error());
 而($层= mysql_fetch_array($结果))
 {
  回声'<期权价值='。$层['县'。'>'$层['县']。'< /选项>';
 }
}
 

这是罚款,$ tblname是包含用户的国家,obvs一贴$ _SESSION变量。

现在的问题是这第一个选择框的onblur,第二个选择框使用这个JS函数和PHP调用提出了:

  $(文件)。就绪(函数()
{
    $('#wait_1)隐藏()。
    $('#drop_1')。改变(函数()
        {
            $('#wait_1)显示()。
            $('#result_1)隐藏()。
            $获得(func.php
                {
                    FUNC:drop_1
                    drop_var:$('#drop_1)VAL()。
                },
            功能(响应)
                {
                    $('#result_1)淡出();
                    的setTimeout(finishAjax('result_1','+逸出(响应)+'),400);
                });
            返回false;
        });
});
 

...

 如果(使用isset($ _ GET ['FUNC'])及和放大器; $ _GET ['FUNC'] ==drop_1){
drop_1($ _ GET ['drop_var']);
}
 

调用该函数:

 函数drop_1($ drop_var)
{
  include_once('的functions.php');
  $结果= mysql_query(SELECT DISTINCT小镇,距离$ tblname WHERE县='$ drop_var')
  或死亡(mysql_error());

  回声'<选择名称=drop_2ID =drop_2>
  <期权价值=禁用=禁用选择=选择>选择你的镇/邻近的城镇和LT; /选项>';

  而($ drop_2 = mysql_fetch_array($结果))
  {
  回声'<期权价值='$ drop_2 ['镇']。'>'$ drop_2 ['镇']。'< /选项>';
  }
  回声'< /选择>';
  回声'<输入类型=提交名称=提交值=继续/>';
}
 

不过,这个功能是不会认$ tblname变量,即使我用全球!它承认的唯一的变量是$ drop_var,这是第一个选择框的结果。

有没有人有任何想法如何,我可以通过$ tblname可变进

 函数drop_1($ drop_var){...
 

解决方案

如果你使用的PHP会话来存储用户所在的国家(和getTierOne()看起来你是第1行),你应该可以添加行至您的drop1开始()PHP函数:

 函数drop_1($ drop_var)
{
  $ tblname = $ _SESSION ['国家'];
  include_once('的functions.php');
  ...
 

jQuery的应该是随着GET参数(FUNC和dropvar),它可以让PHP知道哪些会话属于用户发送用户的cookie,并给他们的会话变量回来(你可以检查此与网络萤火虫/ Chrome浏览器的标签督察查找调用func.php后的onblur已经解雇了,并寻找曲奇请求头)。

另外,如果你不使用会话,但你必须存储在客户端的国家(例如,作为第一个drop_0选择框?),你可以有jQuery的从tier1通过选择(县)选择get调用框。即

  $。获得(func.php
{
  FUNC:drop_1
  国家:值国家在这里,
  drop_var:$('#drop_1)VAL()。
},
功能(响应)
{...
 

和修改服务器端的PHP code接受2个参数,而不是一个。

从可维护性来说,考虑不使用多个表(每个国家之一),而是使用一个表与一个额外的国家一栏。这是很少有多个相同结构的表是一个好主意,MySQL的一般处理数百万行的超过千桌的要好得多,它使联接和更改您的表的结构更加困难。 你也应该消毒的投入,不使用$ GET ['drop_var']直接在SQL查询,或者它会打破报价并打开您SQL注入攻击。 (道歉,如果你有一个很好的理由这样做以下任一:显然,你更多地了解您的具体要求比我做的)

I am using a jQuery/JS/Ajax script to dynamically fill SELECT boxes from my database, with each subsequent SELECT box being populated based on the the previous SELECT.

Eg. Someone selects 'England' from my first SELECT, this then populates the next SELECT with towns in England etc.

The trouble I'm having is getting the second SELECT function to recognise a variable. It's a lot of code so I don't want to paste it all but... this is the first function which populates the first SELECT box on page load:

function getTierOne()
{   
 $tblname = $_SESSION['country'];
 $result = mysql_query("SELECT DISTINCT county FROM $tblname ORDER BY county") 
 or die(mysql_error()); 
 while($tier = mysql_fetch_array( $result ))  
 {
  echo '<option value="'.$tier['county'].'">'.$tier['county'].'</option>';
 }
}

This is fine, $tblname is a posted $_SESSION variable containing the users country, obvs.

The problem is 'onBlur' of this first SELECT box, a second SELECT box is brought up using this JS function and PHP call:

$(document).ready(function() 
{
    $('#wait_1').hide();
    $('#drop_1').change(function()
        {
            $('#wait_1').show();
            $('#result_1').hide();
            $.get("func.php", 
                {
                    func: "drop_1",
                    drop_var: $('#drop_1').val()
                }, 
            function(response)
                {
                    $('#result_1').fadeOut();
                    setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
                });
            return false;
        });
});

...

if (isset($_GET['func'])&& $_GET['func'] == "drop_1") {
drop_1($_GET['drop_var']);
}

Which calls this function:

function drop_1($drop_var)
{  
  include_once('functions.php');
  $result = mysql_query("SELECT DISTINCT town FROM $tblname WHERE county='$drop_var'") 
  or die(mysql_error());

  echo '<select name="drop_2" id="drop_2">
  <option value=" " disabled="disabled" selected="selected">Select Your Town/Nearest Town</option>';

  while($drop_2 = mysql_fetch_array( $result )) 
  {
  echo '<option value="'.$drop_2['town'].'">'.$drop_2['town'].'</option>';
  }
  echo '</select>';
  echo '<input type="submit" name="submit" value="Continue" />';
}

But this function will not recognise my $tblname variable, even if i use Global! The only variable it recognises is $drop_var, which is the result of the first SELECT box.

Does anyone have any idea how i can pass the $tblname variable into

function drop_1($drop_var) { ...

解决方案

If you're using PHP sessions to store the user's country (and from line1 of getTierOne() it looks like you are), you should be able to add the line to the start of your drop1() PHP function:

function drop_1($drop_var)
{  
  $tblname = $_SESSION['country'];
  include_once('functions.php');
  ...

Jquery should be sending your user's cookie along with the GET parameters (func and dropvar), which allows PHP to know which session belongs to the user, and give them their session variables back (You can check this with the "Network" tab of Firebug / Chrome inspector- look for the call to func.php after onBlur has fired and look for the "Cookie" request header).

Alternatively, if you're not using sessions, but you have the country stored on the client-side (eg. as the first "drop_0" select box?), you could have jquery pass the selections from tier1 (county) select boxes in the get call. ie.

$.get("func.php", 
{
  func: "drop_1",
  country: "VALUE OF COUNTRY HERE",
  drop_var: $('#drop_1').val()
}, 
function(response)
{ ...

And modify your server-side PHP code to accept 2 arguments instead of one.

From a maintainability point of view, consider not using multiple tables (one per country) and instead use a single table with an additional "country" column. It's rarely a good idea to have multiple identically structured tables, MySQL generally handles millions of rows much better than hundreds of tables, and it makes joins and making changes to your tables' structure more difficult. You also should be sanitizing the inputs, not using $GET['drop_var'] directly in a SQL query, or it'll break on quotes and opens you up to SQL injection attacks. (Apologies if you have a good reason for doing either of these: Obviously you know more about your specific requirements than I do!)

这篇关于使用jQuery动态链接的选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆