2016-03-04 78 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>