Php 从select返回span
我有一个小的学校项目,我正在做。在选择菜单中,我可以选择在我的数据库中注册的赌场。这很好。但我需要有一个跨度,我选择的名字,我打印 PHP工作:Php 从select返回span,php,jquery,Php,Jquery,我有一个小的学校项目,我正在做。在选择菜单中,我可以选择在我的数据库中注册的赌场。这很好。但我需要有一个跨度,我选择的名字,我打印 PHP工作: <select class="form-control input-sm" name="choosecasino" id="rl_select_casino"> <option>Choose Casino</option> <?php $sql ="SELEC
<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>
函数showSelectedItem(){
var item=document.getElementById(“selectcasino”).value;
document.getElementById(“currentcasino”).innerHTML=item;
}
document.getElementById(“selectcasino”).addEventListener(“更改”,showSelectedItem);
选择我正在处理的语句:
Casino: <span id="currentcasino">
<?php
$sql = "SELECT FROM casinos WHERE name='?'";
echo $sql;
?>
</span>
赌场:
考虑到您已经用jquery
标记了这个问题,我假设您可以使用jquery(即使您标记为“jquery工作”的代码是原始javascript,而不是jquery)。如果你这样做了,这应该对你有用
您必须删除引号“?”
,必须将某些内容绑定到占位符,必须执行查询并获取结果。除此之外,你应该很乐意去“我选择的名字我打印的地方”。如果只需要打印名称,为什么需要第二个查询?你已经知道名字了。非常感谢你的回答。是的,我只需要名字。但是我怎样才能打印呢?我似乎只是为了打印一个名字而编写了这么多代码?
<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>
<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>