本文目錄一覽:
- 1、PHP下拉表單菜單
- 2、怎麼把php查詢到的值顯示到下拉框中
- 3、php下拉菜單選中值怎麼在選擇之後保留並且顯示而不是跳回默認值
- 4、php下拉列表怎麼顯示被選擇的項
- 5、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/zh-hk/n/129001.html