简体   繁体   English

php,javascript和mysql如何组合到一个组合(如果单击)通过另一个查询进入另一个组合

[英]How php, javascript and mysql to one combo, if clicked comes in another through another query


I have tried to get problem to be done through many different post this forum have. 我试图通过这个论坛有很多不同的帖子来解决问题。 But couldn't reach into my specific goal. 但是无法达到我的特定目标。
I am trying to have data from a column in a table to a combo, then if any one data clicked, it should list to another combo from another column in another table according to data clicked. 我正在尝试使数据从表中的列到组合,然后单击任何一个数据,它应该根据单击的数据从另一个表中的另一列列出到另一个组合。
Please check my site: raihans.co.uk, user: dbuser, passwd. 请检查我的网站:raihans.co.uk,用户:dbuser,passwd。 Then hover User Log In/Out, click on 'DB Test'. 然后将鼠标悬停在用户登录/退出上,单击“数据库测试”。
As a sample, you may have a look the combo created on the top. 作为示例,您可能会看到在顶部创建的组合。 At the bottom I have tried myself with mysql, javascript and php. 在底部,我尝试使用mysql,javascript和php进行测试。 Succeded to populate my first column in 1st Combo from one of my Table. 从我的一张桌子成功地填充了我在第一组合中的第一列。 But can't carry to second or third. 但是不能带到第二或第三。
Please have a look the code. 请看一下代码。 I have used to populate the two combos. 我曾经填充两个组合。 I am not sure, where I have done the mistake/s. 我不确定在哪里犯了错误。 Please help me on this. 请帮我。 Thanking you in advance. 预先感谢您。

<html><div><head><?php
    $link = mysql_connect("localhost","db_user_name","db_user_passwd");
    if (!$link) {    die('Could not connect to Database: ' . mysql_error());   }
    mysql_select_db("db_name",$link);
?>

<script type="text/javascript">
<?php
    $category = array();
    $query = "SELECT State_Name FROM Test_State";
    $q_result = mysql_query($query);
    if (!$q_result) {     die('Could not make query: ' . mysql_error());      }
    while($row = mysql_fetch_assoc($q_result))  {    ?>
category[] = ["<?php  $row;    }    ?>"];

function fillSelect(sel,ary,nxt){
 if (ary&&sel.form){
 var frm=sel.form,nme=sel.name.replace(/\d/g,""),i=Number(sel.name.replace(/\D/g,""))+1,nxt=frm[nxt],opts=sel.options,oary=[],z0=nxt==sel?0:1,z1=0,z1a;
 while (frm[nme+i]){
   frm[nme+i].length=1;
   frm[nme+i].selectedIndex=0;
   i++;
 }
 for (;z0<opts.length;z0++){
   if (opts[z0].selected&&ary[opts[z0].value]){
     oary=oary.concat(ary[opts[z0].value]);
 }  }
 if (nxt){
   for (;z1<oary.length;z1++)  {    nxt.options[z1+1]=new Option(oary[z1],oary[z1]);   }
 nxt.selectedIndex=0;
 }   }   }
 function getValue(isValue) {   alert(isValue);   }

 function init() {   fillSelect(document.forms[0]['List1'],category,'List1')   }

 navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);
 </script></div></head>

 <body>
 <form action="">

 <select name='List1' onchange="fillSelect(this,category,'List2')">
     <option value="" selected>Select a country</option>
 <?php
    $category = "SELECT Country_Name FROM Test_Country";
    $query_result = mysql_query($category);
    while($result = mysql_fetch_assoc($query_result))
    {    ?>
 <option value = "<?php echo $result['Country_Name']?>"><?php echo $result['Country_Name']?></option>
 <?php    } ?>
 </select>

 <select name='List2' onchange="getValue(this.value)">
 <option selected>Select State</option>
 </select>
 </form></body></html>

I think the best technique for you is to use the example code you have exactly as it is, with one change: when you build the page in PHP you want to dynamically create the categories array. 我认为,最适合您的技术是使用示例代码,只做一次更改:在PHP中构建页面时,您想动态创建Categories数组。 You can do this very neatly by creating the array in PHP, and then use json_encode() to turn it into javascript. 您可以通过在PHP中创建数组,然后使用json_encode()将其转换为javascript来非常巧妙地完成此操作。

Here I have tried to show you how to build the $categories variable that you can use with your existing Javascript. 在这里,我试图向您展示如何构建可与现有Javascript一起使用的$categories变量。 I haven't tested it, so you should expect a few errors. 我尚未测试过,因此您应该会遇到一些错误。 But the technique is what is important. 但是技术很重要。

<html><div><head>
    <?php
        $link = mysql_connect("localhost","db_user_name","db_user_passwd");
        if (!$link)
            die('Could not connect to Database: ' . mysql_error());
        mysql_select_db("db_name",$link);

        $categories = array();

        $countryQuery = "select State_Name FROM Test_State";
        $countryResult = mysql_query($countryQuery);
        while( $row = mysql_fetch_assoc($countryResult) )
            $categories['startList'][] = $row['State_Name'];

        /*  I don't know your db schema, so I'm making it up as I go along
            A db row of "California","SFO" becomes:
                $categories["California"] = array("SFO");
            Subsequent airports in California are appended to the same array.
        */
        $airportQuery = "select Airport_Name, State_Name from Test_Airport";
        $airportResult = mysql_query($airportQuery);
        while( $row = mysql_fetch_assoc($airportResult) )
            $categories[$row['State_Name']][] = $row['Airport_Name'];

        // you can use the same technique to build a map of airports->resorts
    ?>

    <!-- Now we just need to export the big PHP array as a Javascript variable.
        Use the json_encode() function. -->
    <script type="text/javascript">
        $category = <?php echo json_encode($categories); ?> ;
    </script>

I have some additional notes for you, as well: 我也为您提供一些其他说明:

First, don't use the mysql library. 首先,不要使用mysql库。 It has been deprecated for new code. 不推荐使用新代码。 Use mysqli or PDO instead. 改用mysqliPDO

Second, I think you are confused about how PHP and Javascript interact. 其次,我认为您对PHP和Javascript如何交互感到困惑。 In a word, they don't. 总之,他们没有。 PHP runs and creates the page, then the browser loads the page and runs the Javascript. PHP运行并创建页面,然后浏览器加载页面并运行Javascript。 It looks like you are expecting the Javascript to interact with the PHP, and that isn't going to happen unless you employ AJAX techniques. 您似乎希望Javascript与PHP交互,除非您采用AJAX技术,否则这不会发生。 You are not yet ready for AJAX. 您尚未准备好使用AJAX。

Third, if you are going to ask other people to read your code, you should format it nicely first. 第三,如果您要让其他人阅读您的代码,则应首先对其进行格式化。 Messy code is very hard to read, and most people will just avoid it and not answer your question. 凌乱的代码很难阅读,大多数人只会避免使用它,而不会回答您的问题。 If you want other people to help you with your code, you need to be willing to put some extra effort into your questions. 如果您希望其他人帮助您编写代码,则需要愿意花更多的精力解决问题。

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

相关问题 如何在另一个查询中使用一个查询的结果(PHP / MySQL) - How to use the result of one query in another query (PHP/MySQL) 如何通过JavaScript将一个PHP值从一个PHP页面传递到另一个PHP页面? - How to pass a PHP value from one PHP page to another PHP page through JavaScript? 使用php在另一个查询中执行一个mysql查询的结果 - result of one mysql query in another query using php 在PHP Envirnment中使用来自另一个查询中的一个MySQL查询的结果 - Using results from one MySQL query in another query in a PHP Envirnment Mysql和php查询到另一个查询 - Mysql and php query into another query 如何使用查询遍历从 mysql 数据库表创建的数组并在另一个 php 文件中使用该数组? - How to go through an array which is created from mysql database table with a query and use the array in another php file? MYSQL PHP查询将数据从一个表获取到另一个表 - MYSQL PHP query to get data from one table to another 在PHP中从MySQL查询创建时的XML结构(另一种) - XML structure while creating from MySQL query in PHP (another one) 如何使用javascript将表单验证中的表单验证重定向到带有MySQL查询的PHP网页到另一个页面 - How to redirect with form validation in javascript to PHP webpage with MySQL query to another page 如何在mysql中将一个查询结果减去另一个查询 - how to subtract one query result to another query in mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM