dousao1175 2013-02-07 00:26
浏览 67
已采纳

如何在php中的下拉列表中保留所选项?

I am using this code to switch language in my website. Everything works fine but when I change language always first item is selected. I want to keep selected which language is website language. Here is my code.

<td width="85%" align=right class=top_section> 
          <form method='POST'>
          <select name="switchlanguage" onchange="this.form.submit()"> 
          <option value="en" <?php if($_POST['switchlanguage'] == 'en') echo 'selected';?>English</option> 
          <option value="ru" <?php if($_POST['switchlanguage'] == 'ru') echo 'selected';?>Русский</option> 
          <option value="tj" <?php if($_POST['switchlanguage'] == 'tj') echo 'selected';?>Точики</option> 
          </select> 

    </form>
    </td>

How to make it work properly? If russian language is selected it should stay in russian. But it always goes to English.

And using this code to change website language

if(!isset($_POST['switchlanguage']))
  {
  include_once($DOCUMENT_LANG.'en.lng.php');
  }
  else
  {
  include_once($DOCUMENT_LANG.$_POST['switchlanguage'].'.lng.php');
  }

Even when I use tab to enter dropdownbox and using down key, it is automatically goes to first item.

  • 写回答

7条回答 默认 最新

  • dongmubi4375 2013-02-07 05:05
    关注

    I found what was the problem. I did not know that for smarty we need <{php}> and <{/php}> tags. So writing was not been known by smarty. I wrote finally this code and it worked.

    <form method='POST'>
          <select name="switchlanguage" onchange="this.form.submit()"> 
          <option value="en" <{php}>if($_POST['switchlanguage'] == 'en') echo 'selected="selected"';<{/php}>> English</option> 
          <option value="ru" <{php}>if($_POST['switchlanguage'] == 'ru') echo 'selected="selected"';<{/php}>> Русский</option> 
          <option value="tj" <{php}>if($_POST['switchlanguage'] == 'tj') echo 'selected="selected"';<{/php}>> То?ик?</option> 
          </select> 
    

    Thanks for everyone who helped me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部