dongwen3410
2017-04-24 15:23
浏览 48
已采纳

PHP使用<select>更改内容

I have select menu and I want to change content when I change values like jquery but with php. I find this in answers but it doesn't work.

<?php
$page = null;
if(isset($_POST['page'])){
$page = $_POST['page'];
}
switch($page){
case 'page3': include_once('/path/to/page3content.php'); break;
case 'page2': include_once('/path/to/page2content.php'); break;
case 'page1': include_once('/path/to/page1content.php'); break;
default: include_once('/path/to/defaultcontent.php'); break;
}
?>
<form name="myform" action="" method="post">
<select name="page" onchange="this.form.submit()">
    <option value="page1"<?php if($page == "page1"){ echo " selected"; }?>>Page 1</option>
    <option value="page2"<?php if($page == "page2"){ echo " selected";}?>>Page 2</option>
    <option value="page3"<?php if($page == "page3"){ echo " selected";}?>>Page 3</option>
</select>

EDIT: The problem was in my browser. Now content is show before my form I want content to show after </select> How to do this?

图片转代码服务由CSDN问答提供 功能建议

我有选择菜单,我想在更改像jquery这样的值但是用php时更改内容。 我在答案中找到了这个但它不起作用。

 &lt;?php 
 $ page = null; 
if(isset($ _ POST ['page'])  ){
 $ page = $ _POST ['page']; 
} 
switch($ page){
case'page3':include_once('/ path / to / page3content.php');  break; 
case'page2':include_once('/ path / to / page2content.php');  break; 
case'page1':include_once('/ path / to / page1content.php');  break; 
default:include_once('/ path / to / defaultcontent.php');  break; 
} 
?&gt; 
&lt; form name =“myform”action =“”method =“post”&gt; 
&lt; select name =“page”onchange =“this.form.submit()”  &gt; 
&lt; option value =“page1”&lt;?php if($ page ==“page1”){echo“selected”;  }?&gt;&gt; Page 1&lt; / option&gt; 
&lt; option value =“page2”&lt;?php if($ page ==“page2”){echo“selected”;}?&gt;&gt; Page 2&lt;  ; / option&gt; 
&lt; option value =“page3”&lt;?php if($ page ==“page3”){echo“selected”;}?&gt;&gt; Page 3&lt; / option&gt; 
&lt; / 选择&gt; 
   
 
 

编辑:问题发生在我的浏览器中。 现在内容显示在我的表单之前我希望在&lt; / select&gt; 之后显示内容如何执行此操作?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpochi9741 2017-04-24 17:05
    已采纳

    if you want to show content after select then update this code

     <?php
    session_start();
    $page = null;
    if (isset($_POST['page'])) {
        $page = $_POST['page'];
        $_SESSION['page'] = $page;
    } else {
        $page = $_SESSION['page'];
    }
    
    ?>
        <form name="myform" action="" method="post">
            <select name="page" onchange="this.form.submit()">
                <option value="page1"<?php if ($page == "page1") {
                    echo " selected";
                } ?>>Page 1
                </option>
                <option value="page2"<?php if ($page == "page2") {
                    echo " selected";
                } ?>>Page 2
                </option>
                <option value="page3"<?php if ($page == "page3") {
                    echo " selected";
                } ?>>Page 3
                </option>
            </select>
        </form>
    
    <?php
    switch ($page) {
        case 'page3':
            include_once('/path/to/page3content.php');
            break;
        case 'page2':
            include_once('/path/to/page2content.php');
            break;
        case 'page1':
            include_once('/path/to/page1content.php');
            break;
        default:
            include_once('/path/to/defaultcontent.php');
            break;
    }
    ?>
    
    点赞 打赏 评论

相关推荐 更多相似问题