点击提交按钮后,从下拉列表中获取状态名称(第3部分) [英] Get state name from drop down list after click submit button (part 3)

查看:66
本文介绍了点击提交按钮后,从下拉列表中获取状态名称(第3部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

续在将城市名称从php传递给js(第2部分) a>



状态数据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 ++)
{
$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆