2016-03-04 100 views
0

我有一個小型學校項目,我正在研究。在選擇菜單中,我可以選擇在我的數據庫中註冊的娛樂場。這工作正常。但我需要有一個跨度,我選擇我打印的名稱。從選擇的範圍內返回

PHP工作:

<select class="form-control input-sm" name="choosecasino" id="rl_select_casino"> 
     <option>Choose Casino</option> 
      <?php 
      $sql ="SELECT * FROM casinos ORDER BY name;"; 
      $res = $mysqli->query($sql); 
      //print($res); 
      if($res){          
       while($row = $res->fetch_assoc()){ 
        ?> 
        <option value="<?php echo $row['c_id'];?>"><?php echo $row['name'];?></option> 
        <?php           
       }        
      } 
     ?>         
</select> 

JQuery的工作:

<script> 
function showSelectedItem() { 
    var item = document.getElementById("selectcasino").value; 
    document.getElementById("currentcasino").innerHTML = item; 
} 

    document.getElementById("selectcasino").addEventListener("change", showSelectedItem); 
</script> 

Select語句我的工作:

Casino: <span id="currentcasino"> 
     <?php 
      $sql = "SELECT FROM casinos WHERE name='?'"; 
      echo $sql; 
     ?> 
     </span> 

我需要什麼更多的在我的sql語句?

最好的問候。

+1

你必須刪除引號'「?」',你要的東西結合到佔位符,你必須執行查詢,你必須獲取結果。除此之外,你應該很好去。 –

+0

「我選擇的名字是我打印的」。如果您只需要打印名稱,爲什麼需要第二個查詢?你已經有了這個名字。 –

+0

非常感謝您的回答。是的,我其實只需要這個名字。但是我怎麼打印呢?我看起來只有很多代碼才能打印出名字? –

回答

0

考慮到你已經用jquery標籤標記了這個問題,我假設你有jQuery可用(即使你標記爲「JQuery Working」的代碼是原始javascript,而不是jQuery)。如果你這樣做,這應該適合你。 Here's a sample fiddle

<script> 
function showSelectedItem() { 
    // take the text of the selected option and inject it into the 'currentcasino' span 
    $("#currentcasino").html($("#selectcasino option:selected").text()); 
} 

    $("#selectcasino").on("change", showSelectedItem); 
</script> 

您可以從currentcasino跨度刪除PHP代碼。

如果你是而不是使用jQuery,它有點複雜,但仍然可以完成。 Here's a fiddle for this version

<script> 
function showSelectedItem() { 
    // take the text of the selected option and inject it into the 'currentcasino' span 
    var theSelectedIndex = document.getElementById("selectcasino").selectedIndex; 
    var theSelectedText = document.getElementById("selectcasino").options[theSelectedIndex].innerHTML; 

    document.getElementById("currentcasino").innerHTML(theSelectedText); 
} 

    document.getElementById("selectcasino").addEventListener("change", showSelectedItem); 
</script>