如何从PHP获取单选按钮值

时间:2013-08-20 06:32:02

标签: php javascript jquery html ajax

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

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

的index.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;

?>

1 个答案:

答案 0 :(得分: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;
}
}