点击提交按钮后,从下拉列表中获取状态名称(第3部分) [英] Get state name from drop down list after click submit button (part 3)
本文介绍了点击提交按钮后,从下拉列表中获取状态名称(第3部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
状态数据json( $ stateJsonObject ):
Array(
[0] => stdClass Object([stateId] => s1 [stateName] =>吉隆坡)
[1] => stdClass Object [stateId] => s2 [stateName] => Selangor)
)
(stateName):
< html>
< head>< / head>
< body>
< form action =test3.phpmethod =post>
州:
< option value =>选择一个< / option> ($ i = 0; $ i< count($ stateJsonObject); $ i ++)
{
echo'< option value ='。
<?php
。 $ stateJsonObject [$ i] - > STATEID。>;
echo $ stateJsonObject [$ i] - > Statename的;
echo'< / option>';
}
?>
< / select>
< input type =submitname =submitvalue =Submit/>
< / form>
< / body>
< / html>
我的问题是:
从下拉列表中选择 Selangor
,点击提交按钮后,如何在下拉菜单中保存 Selangor
名称list selected?
解决方案
如果在同一页面上提交,您可以添加当前提交的条目并将一个checked属性放入循环:
< form action =test3.phpmethod =post>
州:
< option value =>选择一个< / option> ($ i = 0; $ i< count($ stateJsonObject); $ i ++)
{
$ selected =($ stateJsonObject [$ i ] - > stateId == $ _POST ['state'])? 'checked':'';
echo< option value ='。$ stateJsonObject [$ i] - > stateId。''$ selected>>;
echo $ stateJsonObject [$ i] - > Statename的;
echo'< / option>';
}
?>
< / select>
< input type =submitname =submitvalue =Submit/>
< / form>
或者使用foreach变体:
foreach($ stateJsonObject as $ state){
$ stateId = $ state-> stateId;
$ stateName = $ state-> stateName;
$ selected =($ stateId == $ _POST ['state'])? 'checked':'';
echo< option value ='$ stateId'$ selected> $ stateName< / option>;
}
Cont. on Pass city name from php to js (part 2)
State data json ($stateJsonObject):
Array (
[0] => stdClass Object ( [stateId] => s1 [stateName] => Kuala Lumpur)
[1] => stdClass Object ( [stateId] => s2 [stateName] => Selangor)
)
Code (stateName):
<html>
<head></head>
<body>
<form action="test3.php" method="post">
State:
<select name="state" id="state" onchange="showCity(this, 'city')">
<option value ="">select one</option>
<?php
for($i = 0; $i < count($stateJsonObject); $i++)
{
echo '<option value = '.$stateJsonObject[$i] -> stateId.'>';
echo $stateJsonObject[$i] -> stateName;
echo '</option>';
}
?>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
My question is:
When I choose Selangor
from drop down list, after I click submit button, how do I keep the Selangor
name in the drop down list selected?
解决方案
If this is submitted on the same page, you could add the currently submitted entry and put a checked attribute inside the loop:
<form action="test3.php" method="post">
State:
<select name="state" id="state" onchange="showCity(this, 'city')">
<option value ="">select one</option>
<?php
for($i = 0; $i < count($stateJsonObject); $i++)
{
$selected = ($stateJsonObject[$i]->stateId == $_POST['state']) ? 'checked' : '';
echo "<option value='".$stateJsonObject[$i]->stateId."' $selected>";
echo $stateJsonObject[$i] -> stateName;
echo '</option>';
}
?>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
Or with a foreach variant:
foreach($stateJsonObject as $state) {
$stateId = $state->stateId;
$stateName = $state->stateName;
$selected = ($stateId == $_POST['state']) ? 'checked' : '';
echo "<option value='$stateId' $selected>$stateName</option>";
}
这篇关于点击提交按钮后,从下拉列表中获取状态名称(第3部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文