2014-09-01 39 views
0

我有這樣的選擇按鈕獲取選擇值:在同一頁面

<form method="post"> 
    <label> 
     <select id="label" name="challengeX" style="margin-top:150px;"> 
      <option selected value="0"> Global Statistics </option> 
      <option value="1">Challenge 1</option> 
      <option value="2">Challenge 2</option> 
      <option value="3">Challenge 3</option> 
     </select> 
    </label> 
</form> 

在同一頁上,幾行波紋管,我有:

<div id="challenge"> 
    <?php 
     $Ch = $_POST['challengeX']; 
     echo "Challenge: ".$Ch; 

     showSomething($Ch,1,1); 
    ?> 
</div> 

但是,當我更改選項,它沒有改變崗位價值。

+1

哪裏是提交按鈕? – 2014-09-01 12:17:27

+0

你能說爲什麼投票嗎? – 2014-09-01 13:16:33

回答

2
$Ch = $_POST['challengeX']; 

直到您提交表單纔會獲得價值。向您的表單添加一個提交按鈕。

<form method="post" action=""> 
    <label> 
     <select id="label" name="challengeX" style="margin-top:150px;"> 
      <option selected value="0"> Global Statistics </option> 
      <option value="1">Challenge 1</option> 
      <option value="2">Challenge 2</option> 
      <option value="3">Challenge 3</option> 
     </select> 
    </label> 
    <input type="submit" value="Submit"> 
</form> 

只有在提交表單的情況下才能訪問表單值。

<div id="challenge"> 
    <?php 
     if(isset($_POST)) // checks whether any value is posted 
     { 
      $Ch = $_POST['challengeX']; 
      echo "Challenge: ".$Ch; 
      showSomething($Ch,1,1); 
     } 
    ?> 
</div> 
-1

在這種方式,你可以使用Javascript。如果你不想使用任何服務器,如PHP或東西

<form method="post"> 
    <label> 
    <select id="label" name="challengeX" style="margin-top:150px;"> 
     <option selected value="0"> Global Statistics </option> 
     <option value="1">Challenge 1</option> 
     <option value="2">Challenge 2</option> 
     <option value="3">Challenge 3</option> 
    </select> 
    <input type="submit" /> /* add this line */ 
</label> 
</form> 

由JavaScript

<select id="label" name="challengeX" style="margin-top:150px;" onChange="getSelect(this)"> 
    <option selected value="0"> Global Statistics </option> 
    <option value="1">Challenge 1</option> 
    <option value="2">Challenge 2</option> 
    <option value="3">Challenge 3</option> 
    </select> 
    <div id="resultSelect"></div> 
    <script> 
    function getSelect(thisValue){ 
     document.getElementById('resultSelect').innerHTML=thisValue.options[thisValue.selectedIndex].text; 
    } 
    </script> 
1

需要提交下拉的變化形式。

更改HTML這樣的:

<form method="post"> 
    <label> 
     <select id="label" name="challengeX" style="margin-top:150px;" onchange="this.form.submit()"> 
      <option selected value="0"> Global Statistics </option> 
      <option value="1">Challenge 1</option> 
      <option value="2">Challenge 2</option> 
      <option value="3">Challenge 3</option> 
     </select> 
    </label> 
</form> 

爲你的PHP執行,你可以嘗試這樣的:

<div id="challenge"> 
    <?php 
if($_SERVER['REQUEST_METHOD'] == 'POST') { 


     $Ch = $_POST['challengeX']; 
     echo "Challenge: ".$Ch; 

     showSomething($Ch,1,1); 

     function showSomething($Ch,1,1){ 

     //do your stuff 
     } 
} 
    ?> 
</div> 
0

PHP在服務器上運行,並提供HTML(或其他數據)的瀏覽器。

如果您想用PHP處理表單數據,那麼您必須將其包含在新的HTTP請求中(通常通過提交表單)並讓PHP使用該數據生成新頁面。

或者,如果您要處理客戶端上的數據,那麼您將需要使用瀏覽器支持的編程語言。除非你想開始使用插件(或特定於瀏覽器的技術),否則這會限制你使用JavaScript。您可以將bind更改事件監聽器添加到select元素,並將then manipulate the DOM與結果一起使用。