如何将下拉表单中的值显示到另一个 php 脚本

how to display value from dropdown form to another php script

这是一个 index.php 页面,其中有下拉列表 当我 select 下拉列表中的值时,我不会在 user.php 页面中显示该值 我使用 javascript onchange

<html>
    <head>
        <title>Test Selected Dropdown Value</title>
        <script type="text/javascript">
            function selected_region(){

            var vr = document.getElementById("region").value;

            alert("Selected region is: "+vr);

            }
        </script>
    </head>
    <body>
        <form name="selected_region_form" method="POST" action="user.php">
            <?php ?>
            <select name="region" id="region" onchange="selected_region();">
                <label for="region">Odaberite zupaniju:</label>
                <option> 
                    <?php 
                    $region_data = all_regions();
                    while($region = mysqli_fetch_array($region_data)){
                            $id_region = $region['zupanija_id'];
                            $name_region = $region['naziv'];
                    ?>
                    <option value="<?php $id_region; ?>"><?php echo $name_region; ?></option>
                    <?php
                    }
                    ?>
                </option>
            </select>
            <input type="submit" name="send" value="Send"/>
        </form>
    </body>
        </table>
    </body>
</html>

这是 user.php 脚本 在 td 标签下,我不会显示来自 index.php

的 selected 值
<?php

include('connect.php');
include('functions.php');
?>

<html>
    <head>
        <title>User</title>
    </head>
    <body>
        <h1>User</h1>
        <h2>selected region:</h2>
        <table border="1"> 
            <tr> 
                <td> <!-- here i need display a selected region --> <td>
            </tr>
        </table>
    </body>
    <a href="index.php.">Back</a>
</html>

functions.php 脚本,其中包含我的查询:

<?php

    function confirm_query($result_set){
        if(!$result_set){
            die("Query data faild!");
        }
    }

    function all_regions(){
        global $db_connection;

        $query  = "SELECT `zupanija_id`,`naziv` ";
        $query .= "FROM `zupanija` ";
        $query .= "ORDER BY `zupanija_id` ASC";
        $zupanije_data = mysqli_query($db_connection, $query);
        confirm_query($zupanije_data);
        return $zupanije_data;
    }

?>

连接脚本:

<?php
$connect_error = 'Connection faild!';
$select_db_error = 'Database not found!';
$db_connection = mysqli_connect('localhost', 'iwa_2013', 'foi2013') or die($connect_error);
$db_select = mysqli_select_db($db_connection, 'iwa_2013_sk_projekt') or die($select_db_error);


//setup charset to utf-8
    mysqli_set_charset($db_connection, 'utf8');
?>

您的表单实际上是通过 post 方法提交到 kreiran_zahtjev.php 页面的,因此您单击的元素将通过 kreiran_zahtjev.php[=11= 上的 $_POST 变量可用]

本质上,如果你想访问被点击的元素,你所要做的就是:

$_POST['zupanija']

此外,您不应使用 PHP 的 mysql_ 扩展,它不安全且已弃用。您应该使用 PDO 或 mysqli。

我认为最好的方法是将一个 onchange 事件处理程序附加到您的 select 元素,然后让它每隔一段时间向您的 PHP 脚本发出一个 AJAX 请求为用户 select 计时。 sending a value from drop down box to another page