0

嗨,我已经使用 3 个表的 mysql 查询在表单上创建了一个下拉组合框,这工作正常,现在我想在运行查询的下拉组合中选择一个项目时在数据库上执行另一个查询在数据库上并返回在定义的字段中使用附加数据填充表单所需的值。以下是我到目前为止的进度....

$query3 = "SELECT customers.id, customers.email, customers_customfields.customer_id, customers_customfields.field_value, orders.sign_date, orders.sub_id, orders.domain_name, orders.cust_status 
FROM customers, customers_customfields, customers_orders, orders 
WHERE customers_orders.order_id = orders.sub_id
AND customers.id = customers_orders.customer_id 
AND customers.id = customers_customfields.customer_id
AND customers_customfields.field_id = 1";

$result = mysql_query ($query3);
echo "<select name=orders value='Select from the list provided '>";


while($drop=mysql_fetch_array($result)){

//data stored in $drop
echo "<option value=$drop[id]>$drop[sub_id]&nbsp;&nbsp;&nbsp;&nbsp;$drop[id]&nbsp;&nbsp;$drop[field_value]&nbsp;&nbsp;$drop[sign_date]&nbsp;&nbsp;$drop[domain_name]&nbsp;&nbsp;&nbsp;&nbsp;$drop[cust_status]</option>";

}
echo "</select>";        
4

2 回答 2

0

您确实应该使用 AJAX 和 PHP。

例如,首先在下拉列表中设置一个 jQuery.change 触发器,如下所示:

$(document).ready(function () {

    $('#yourDropDown').change(function (e) {
        yourFunction($(this).val());
    });
});

function yourFunction(inputString) {
        $.ajax({

            url: "../folder/yourPHP.php",
//The inputString will contain the value of your DropDown, you will give this parameter in the querystring
            data: 'queryString=' + inputString,
            success: function (msg) {
//MSG will be the "message" from the query
//As you see you'll put the MSG in your HTML of your #yourFormTag
//This message can contain everything you want
                if (msg.length > 0) {
                    $('#yourFormTag').html(msg);

                }
            }
        });
}

只是一个 .PHP 文件的示例:

<?php
if (isset($_REQUEST['queryString'])) {

    include '../yourConnection.php';



    $inputString = $_GET['queryString'];

    $select_query = "SELECT <yourColumns>

            FROM <yourTable>
            WHERE <Where you want to check if inputString is the same> LIKE CONCAT('%', :inputString, '%')
            ORDER BY <order if you want? ASC";

    $get = $db->prepare($select_query);

    $get->execute(array(':inputString' => $inputString);

    //For example to fill a select
    echo '<select>';

    while ($row = $get->fetch(PDO::FETCH_ASSOC)) {
        echo "<option value='" . $row['yourColumn'] . "'>" . $row['yourColumn'] . "</option>";
    }

    echo '</select>';

}
?>

正如我所说,.PHP 文件就是一个例子。

希望这会有所帮助,干杯!

于 2013-09-12T08:49:02.357 回答
0

PHP 严格来说是一种服务器端语言。如果您想让数据库中的字段为用户动态更新应用程序(无需提交和刷新),您将不得不使用 AJAX。

通读一些 AJAX 教程以了解总体思路。

此外,使用诸如 PDO 之类的数据库抽象层也是一个好主意。

它几乎在任何情况下都有用,允许使用准备好的语句以及其他好东西。此外,使用 MySqli 代替已弃用的 MySql。

Ajax 教程:http ://www.smashingmagazine.com/2008/10/16/50-excellent-ajax-tutorials/

PDO: http: //php.net/manual/en/book.pdo.php

准备好的报表: http: //php.net/manual/en/pdo.prepared-statements.php

MySqli:http ://php.net/manual/en/book.mysqli.php

于 2013-09-09T01:18:14.867 回答