0

我想在 php 中获取单选按钮值。问题是我在一页中有下拉菜单,当我从下拉列表中选择一些值时,它会将值发送到 ajax 和 php,并从 php 页面生成单选按钮.

现在我想获取单选按钮的值。我怎么才能得到它?

代码

索引.php

   <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>


<div id="txtHint">


</div>

代码:gettheater.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'>";
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;

?>
4

1 回答 1

0

从纯 javascript 点,您可以获取所有带有名称的单选框address并找到被选中的单选框。

function getSelectedValue() {
var addresses = document.getElementsByName('address');
for (var i=0; i < addresses.length; i++) {
   if (addresses[i].checked) return addresses[i].value;
}
}
于 2013-08-20T06:38:00.863 回答