dongle7882 2015-09-14 21:01
浏览 93
已采纳

从下拉列表中选择值后,下次显示没有任何值

Not working :- After selecting the value from dropdown list then after submitting it shows no any value next time.

Working :- But the last selected value is saved in session. If page is refreshed the session value shown as selected in dropdown list.

Working :- After any keyword is submited the value in dropdown must show.

Please give me solution to make first one work. I have done it using POST.

Here is my code:-

<?php
session_start();
$selected='';
$offer_type_selected='';
function get_options($select)
{
    $incent_type=array('All'=>'none','Incent'=>'Incent','Non-Incent'=>'Non-Incent');
    $options='';
    while(list($k,$v)=each($incent_type))
    {
        if($select==$v)
        {
            $options.='<option value="'.$v.'" selected>'.$k.'</option>';   
        }
        else
        {
            $options.='<option value="'.$v.'">'.$k.'</option>';
        }        
    }
    return $options; 
}
if(isset($_POST['incent_type']))
    {
     $selected=$_POST['incent_type'];
     echo $selected;
      $_SESSION['incent_type']=$selected;
    }
?>
<body link="white">

<div class="container">
        <div class="content" >
           <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">

                <div class="label_div">Type a keyword :</div>
                <div class="input_container">
                    <input type="text" name="subquery" id="name_id" onkeyup="autocomplet()">
                    <ul id="name_list_id"></ul>                    
                </div>

                <div class="lable_div" >
                    <input type="submit" class="submit_query" name="submit_id" value="Submit Query" >
                    Select Incent Type :<select class="custom-dropdown" name="incent_type" onchange="this.form.submit();">
                        <?php 
                        $flag=0;
                        if(isset($_POST['subquery']))
                        {
                            if(strcmp("",$_POST['subquery'])!=0)
                            {
                                unset($_SESSION['incent_type']);
                                unset($_SESSION['offers_type']);
                                echo get_options('none');
                                $flag=1;
                            }                            
                        }
                        else if(isset($_SESSION['incent_type']) && $flag==0)
                             echo get_options($_SESSION['incent_type']);
                        else if($flag==0)
                            echo get_options('none');
                        ?>
                    </select>
                   </div>
            </form>
    </div>  
</div>
  • 写回答

1条回答 默认 最新

  • doujiyan0031 2015-09-14 22:07
    关注

    You have problem with checking $_POST['subquery'], you all time getting true this - if(isset($_POST['subquery'])), try change to if(!empty($_POST['subquery']))

    Also you can remove session and some if:

    <?php
    function get_options($select=null)
    {
        $incent_type=array('All'=>'none','Incent'=>'Incent','Non-Incent'=>'Non-Incent');
        $options='';
        foreach($incent_type as $k=>$v){
            $options.='<option value="'.$v.'" '.(!empty($select) && $select==$v?"selected":"").'>'.$k.'</option>';  
        }
        return $options; 
    }
    ?>
    <body link="white">
    
    <div class="container">
            <div class="content" >
               <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
    
                    <div class="label_div">Type a keyword :</div>
                    <div class="input_container">
                        <input type="text" name="subquery" id="name_id" onkeyup="autocomplet()">
                        <ul id="name_list_id"></ul>                    
                    </div>
    
                    <div class="lable_div" >
                        <input type="submit" class="submit_query" name="submit_id" value="Submit Query" >
                        Select Incent Type :<select class="custom-dropdown" name="incent_type" onchange="this.form.submit();">
                            <?php 
                            echo get_options(empty($_POST['subquery'])?$_POST['incent_type']:null);
                            ?>
                        </select>
                       </div>
                </form>
        </div>  
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条