如何从下拉表单显示值到另一个PHP脚本

时间:2015-11-13 15:33:38

标签: javascript php mysql html-select

这是一个index.php页面,我有下拉列表 当我从下拉列表中选择值时,我不会在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 tag-s下,我不会显示index.php中的选定值

<?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');
?>

2 个答案:

答案 0 :(得分:0)

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

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

$_POST['zupanija']

此外,你不应该使用PHP的mysql_扩展,它是不安全和不赞成的。你应该使用PDO或mysqli。

答案 1 :(得分:0)

我认为执行此操作的最佳方法是将onchange事件处理程序附加到select元素,然后在每次用户选择某些内容时触发对PHP脚本的AJAX请求。 axlsx_rails docs