php关于下拉列表框回显(PHP下拉列表)

本文目录一览:

PHP下拉表单菜单

1、新建一个php文件,命名为test.php,用于讲解PHP实现下拉表单菜单。

2、在test.php文件内,使用html中的select标签创建下拉菜单,代码如下。

3、在test.php文件内,使用option标签创建一个提示选项“请选择职业”。

4、在test.php文件内,在select标签内,创建一个php数组,在数组中存储三个不同的职业名称。

5、在test.php文件内,使用foreach遍历上一步创建的数组$arr,每次遍历的数组值为$v。

6、在test.php文件内,使用echo输出option菜单,option菜单的value值和选项名称都为$v。

7、在浏览器运行test.php文件,查看实现的效果。

怎么把php查询到的值显示到下拉框中

解决思路:将查询结果,遍历赋值给下拉框的option/option即可;

//数据库查询

$list=$this-db-GetList(“select * from `goods_list`”);

//循环

$html=”select”;

foreach ($list as $item){

$html.=”option value =\”{$item[‘cateid’]}\”{$item[‘title’]}/option”;

}

$html.=”/select”;

echo $html;

php下拉菜单选中值怎么在选择之后保留并且显示而不是跳回默认值

?php

$sSelect=isset($_POST[‘sel’])?$_POST[‘sel’]:”; // 这里接收选择的值

// 然后把它保存到 session

$_SESSION[‘sel’]=$sSelect;

$sSel=isset($_SESSION[‘sel’])?$_SESSION[‘sel’]:”;

?

!– html 部分 —

select name=”sel”

option value=”30″ ?php if($sSel==30){ ?selected=”selected”?php } ?30/option

option value=”20″ ?php if($sSel==20){ ?selected=”selected”?php } ?20/option

option value=”10″ ?php if($sSel==10){ ?selected=”selected”?php } ?10/option

/select

不知道是不是你说的那样,希望能帮到你,谢谢!

php下拉列表怎么显示被选择的项

用js获取下拉框中的值:

用js获取下拉框中的值具体方法如下:

现在有一id=test的下拉框,分别使用javascript原生的方法和jquery方法

select id=”test”  name=””   

  option   value=”1″text1/option   

  option   value=”2″text2/option   

 /select

code:

一:javascript原生的方法

  1:拿到select对象: var  myselect=document.getElementById(“test”);

  2:拿到选中项的索引:var index=myselect.selectedIndex ;             // selectedIndex代表的是你所选中项的index

  3:拿到选中项options的value:  myselect.options[index].value;

  4:拿到选中项options的text:  myselect.options[index].text;

二:jquery方法(前提是已经加载了jquery库)

1:var options=$(“#test option:selected”);  //获取选中的项

2:alert(options.val());   //拿到选中项的值

3:alert(options.text());   //拿到选中项的文本

php中下拉列表选择后,如何在另一个文本框中显示出与选择项匹配的数据库中的值?

?php

$query=”select * from test where 1″;

$query1=mysql_query($query) or die(mysql_error());

if(mysql_num_rows($query1) 0){

$row = mysql_fetch_row($query1);

@mysql_free_result($query1);

?

html

script

function areas_change(th){

//alert(document.getElementById(‘areas_str’).value);

if(2==th){

//alert(document.getElementById(‘city’).value);

document.getElementById(‘area’).value=document.getElementById(‘city’).value;

}

else if(1==th)

document.getElementById(‘area’).value=document.getElementById(‘pro’).value;

else

document.getElementById(‘area’).value=document.getElementById(‘county’).value;

}

/script

body

select name=”areas” id=”areas” onchange=” areas_change(this.value);”

option value=”3″ selected=”selected”请选择/option

option value=”?php echo $row[0];?”县级/option

option value=”2″市级/option

option value=”1″省级/option

/select

input type=”hidden” readonly name=”city” id=”city” value=’123′

input type=”hidden” name=”pro” id=”pro” value=’123′

input type=”hidden” name=”county” id=”county” value=’213′

input type=”text” maxlength=’18’ name=”area” id=”area” value=’132′ onafterpaste=”this.value=this.value.replace(/\’/g,”)”

/body

/html

如何获得下拉列表的值,只需要$_POST[‘areas’]; 这个例子是php和html代码混合的例子,是事先提取数据库的值放到select的value中,然后change后用于post提交。

当然你可以用ajax进行异步调用

原创文章,作者:OLW9J,如若转载,请注明出处:https://www.506064.com/n/129001.html

(0)
OLW9JOLW9J
上一篇 2024-10-03
下一篇 2024-10-03

相关推荐

发表回复

登录后才能评论