php - 如何从 php 获取选定的单选按钮值 onclick

标签 php javascript html ajax

我想在 php 中获取所选单选按钮的值。

代码: 更新.php

<html>
    <head>
        <link rel="stylesheet" href="css/common.css" type="text/css">
        <link rel="stylesheet" type="text/css" href="css/page.css">
        <link href="css/loginmodule.css" rel="stylesheet" type="text/css" />

        <script>
            function showUser(str) {
                if (str == "") {
                    document.getElementById("txtHint").innerHTML = "";
                    return;
                }

                if (window.XMLHttpRequest) {
                    xmlhttp = new XMLHttpRequest();
                } else {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }

                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET", "gettheater.php?q=" + str, true);
                xmlhttp.send();
            }



        </script>       





    </head>
    <body>

        <h1>Welcome <?php echo $_SESSION['SESS_FIRST_NAME']; ?></h1>
        <a href="member-profile.php" class="log">My Profile</a> | <a href="logout.php" class="log">Logout</a>
        <p>This is a password protected area only accessible to Admins. </p>

    <center>
        <div class="pan"><br><br>
            <div class="head">Remove Theater From List</div>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >


                <table>
                    <tr>
                        <td>
                            <table>
                                <tr>
                                    <td><label for="Theatername">Theater Name</label></td>
                                    <td>
                                        <?php
                                        try {
                                            $dbh = new PDO('mysql:dbname=theaterdb;host=localhost', 'tiger', 'tiger');
                                        } catch (PDOException $e) {
                                            echo 'Connection failed: ' . $e->getMessage();
                                        }

                                        $sql = "SELECT theater_name FROM theater;";

                                        $sth = $dbh->prepare($sql);
                                        $sth->execute();

                                        echo "<select name='theater_name' id='course' onchange='showUser(this.value);'>";
                                        echo "<option>----Select Theater----</option>";
                                        while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
                                            echo "<option value='" . $row['theater_name'] . "'>" . $row['theater_name'] . "</option>";
                                        }
                                        echo "</select>";
                                        ?>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr> 
            </form>


            <tr>
                <td>
                    <form method="POST">
                        <table>
                            <tr>
                                <td><label for="Address">Address</label></td>
                                <td><div id="txtHint">
                                    </div></td>
                            </tr>


                            <tr>
                                <td></td>
                                <td><input type="submit" name='Remove From List' value="Remove From List" class="getvalue" onclick="< ? php myfunc() ? >"/></td>
                            </tr>
                        </table>

                        </table> 
                    </form>
                    <br><br>
            </tr>
            </td>
            <?php
            if (isset($_POST['Submit'])) {
                echo $_POST['address'];
            }
            ?>
        </div>
    </center>
</body>
</html>

代码
获取剧院.php

<?php
$q = strtolower(trim($_GET["q"]));

try {
    $dbh = new PDO('mysql:dbname=theaterdb;host=localhost', 'tiger', 'tiger');
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

$sql = 'SELECT address FROM theater WHERE LOWER(theater_name) = :q';

$sth = $dbh->prepare($sql);
$sth->bindValue(':q', $q);
$sth->execute();

echo "<form name='theater' method='POST' action='update.php'>";
echo "<table border='0' class='tabs'><tr><th>Theater Address</th><th>Select</th></tr>";

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
    echo "<tr>";
    echo "<td class='ad'>" . $row['address'] . "</td>";
    echo "<td>";
    echo '<input type="radio" name="address" value="43"  />';
    echo "</td>";
    echo "</tr>";
}

echo "</table>";
echo "</form>";
$dbh = null;
?>

单选按钮的 html 位于 gettheater.php 页面中。但我正在运行 update.php gettheater 页面在 update.php 中打开,但如何获取选定的单选按钮值,我想在 update.php 中获取选定的单选按钮值。

我该怎么做?

最佳答案

检查表单是否已提交,如果已提交,则使用 $_POST[] 访问数据。

参见http://php.net/manual/en/reserved.variables.post.php

您可能想要包含一个提交按钮,这将允许您首先检查表单是否已提交。

echo "<form name='theater' method='POST' action='update.php'>";
echo '<input type="submit" value="Enter" name="submit">';

检查

if (isset($_POST['submit'])) {
    //Do something
}

这通常会起作用,如果不起作用,请尝试使用隐藏字段。

关于php - 如何从 php 获取选定的单选按钮值 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18329472/

相关文章:

javascript - JSON 模型纹理未加载

javascript - Highcharts 堆叠条未对齐且条形尺寸奇怪

javascript - HTML 5 检查字段

javascript - 使用 Javascript 计算列表项的长度

php - 我可以有一个与任何其他表无关的数据库表吗?

javascript - 我怎样才能提前载入传单 map ?

php - 将 num_rows 从 php 返回到 javascript

javascript - 调用未定义函数 where() laravel

php - Laravel 5 Route::group 与公共(public)变量

PHPExcel - 从代码编辑时格式丢失