简体   繁体   English

HTML 列表框用 PHP 更新另一个列表框

[英]HTML Listbox update another Listbox with PHP

I have an HTML listbox, I want to be able to select any value in that listbox and update a second listbox with a PHP database query based on that selection.我有一个 HTML 列表框,我希望能够选择该列表框中的任何值,并根据该选择使用 PHP 数据库查询更新第二个列表框。

Options I have tried:我尝试过的选项:

-I have tried using JSON. - 我试过使用 JSON。 I update the second listbox using Javascript and calling an external PHP script, but the returned PHP value would be an array and that wont convert well with Javascript.我使用 Javascript 更新第二个列表框并调用外部 PHP 脚本,但返回的 PHP 值将是一个数组,并且不能很好地与 Javascript 转换。

-I am not able to use cookies, so I use HTML Sessions. - 我不能使用 cookie,所以我使用 HTML Sessions。 When you select a value in the first listbox it updates a session value and refreshes the page.当您在第一个列表框中选择一个值时,它会更新会话值并刷新页面。 Upon reload, the page does a PHP script using the Session value to update the second listbox, but I cant update the HTML Session with the selected value of the first listbox using Javascript by calling onChange="function(); with the first listbox".重新加载后,页面使用 Session 值执行 PHP 脚本来更新第二个列表框,但我无法通过调用 onChange="function(); with first listbox" 使用 Javascript 使用第一个列表框的选定值更新 HTML 会话.

Is there any other available options to implement this?有没有其他可用的选项来实现这一点? Or how can I fix one of these options to make this work?或者我如何修复这些选项之一以使其工作?

You say that you've tried using JSON, but you clearly don't completely understand what that means.你说你已经尝试过使用 JSON,但你显然没有完全理解这意味着什么。 JSON is simply a way of formatting strings that can be understood universally by any machine which has a parser for it. JSON 只是一种格式化字符串的方法,任何具有解析器的机器都可以普遍理解它。 The idea that the PHP value is an "array" and therefore unconvertible makes no sense; PHP 值是一个“数组”因此不可转换的想法是没有意义的; all that matters is that it's sent to the client in JSON format .重要的是它以JSON 格式发送到客户端。

If you have an array in PHP, you can convert it to JSON format using the json_encode function:如果您在 PHP 中有一个数组,则可以使用json_encode函数将其转换为 JSON 格式:

<?php
my_arr = array('a' => 'b', 'c' => 'd');
// do stuff to fill array
echo json_encode($my_arr);
?>

When you receive it in JSON format in your JavaScript code, you can then parse it into a JavaScript object - see this question for details.当您在 JavaScript 代码中以 JSON 格式接收它时,您可以将其解析为 JavaScript 对象 - 有关详细信息,请参阅此问题

So here is how I would do it using ajax.所以这里是我将如何使用 ajax 来做到这一点。 I would have an onChange event pass the value of the list to a hidden input.我会让一个 onChange 事件将列表的值传递给一个隐藏的输入。 I would then post the input in the following manner.然后我会以以下方式发布输入。

var params = $('#hiddenInputId').serialize();
$.post('post.php', params, function(data){
                if(data.match(/^[\r\n\s]*OK=/)) {
                    $('#listOut').html(data.slice(data.indexOf('OK=') + 3));
                } 
                else if(data.match(/^[\r\n\s]*ERRORS=/)) {
                    alert(data.slice(data.indexOf('ERRORS=') + 7));
                }
}); 

The #listOut would point to a span or something that you want to fill in with your return select list. #listOut 将指向一个跨度或您想用返回选择列表填充的内容。 You would set up a if statement on the post.php page that listens for the submit of the hidden input and returns the correct value.您将在 post.php 页面上设置一个 if 语句来监听隐藏输入的提交并返回正确的值。

if(isset($_POST['hiddenInputName'])){
    $query = "SELECT * FROM table WHERE something LIKE'" . $_POST['hiddenInputName']."';";
    $result = mysql_query($query) or die(mysql_error());
    echo 'OK=created Select: <select>';
    while($row = mysql_fetch_array($result)){
        echo"<option value='" . $row['field_name'] . "'>" . $row['field_name'] ."</option>";
        }    
    }
    echo "</select>";
}

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

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