2013-08-20 130 views
1

我想在php中獲取所選單選按鈕的值。如何從php中獲取選定的單選按鈕值onclick

代碼: update.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> 

代碼
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' 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中所選的單選按鈕值。

我該怎麼做?

回答

1

檢查表單是否已提交,如果已使用$_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 
} 

這通常會工作,如果它不會再嘗試使用隱藏域。

相關問題