PHP Switch Case - 选择保留选项

时间:2013-12-16 16:15:12

标签: php select switch-statement

我在这里尝试了多个不同的答案,但似乎并不能解决我想要完成的任务。

我在选择并提交选项时,尝试将选择框中的选项设置为已选中。

我正在使用switch case,因为它似乎是一种更快的编程方式,因为它反对if else语句。

我在页面上有iframe。所有目标链接都根据选项工作,但如果我尝试回显所选的选项,它将只显示选择的最后一个选项,因为它当前正在选择我的所有选项,这不是我想要的。

这是我的代码:

PHP:

if (isset($_POST['listings'])){
        $listings = $_POST['listings'];
    } else if (isset($_GET['listings'])) {
        $listings = $_GET['listings'];
    } else {
        $listings = 'chooseListing';
    }

    switch ($listings) {
        case 'captivaCondo':
            $iframe = 'http://sancapmls.com/san/idx/index.php?key=09b24ea58e44d14f9f5efed2bfd1edb2';
            $selected = 'selected';
            break;
        case 'captivaResidential':
            $iframe = 'http://sancapmls.com/san/idx/index.php?key=90f72cf9a9a8fe486dff35cede5208b1';
            $selected = 'selected';
            break;
        case 'sanibelCondo':
            $iframe = 'http://sancapmls.com/san/idx/index.php?key=2282d6f0deb849d6a4915685b7677848';
            $selected = 'selected';
            break;
        case 'sanibelResidential':
            $iframe = 'http://sancapmls.com/san/idx/index.php?key=ceb9ec5fc169c6405d234e9b361f8915';
            $selected = 'selected';
            break;
        case 'choose':
            $iframe = 'choose.php';
            $selected = 'selected';
            break;
    }

HTML:

    <!-- Search Button -->
    <div class="row">
        <form role="form">
        <div class="col-md-8">
            <select name="listings" class="form-control input-lg">
                <option value="choose">Choose an option</option>
                <option value="captivaCondo">Captiva Condo</option>
                <option value="captivaResidential">Captiva Residential</option>
                <option value="sanibelCondo">Sanibel Condo</option>
                <option value="sanibelResidential">Sanibel Residential</option>
            </select>
            </div>
            <div class="col-md-4">
            <button type="submit" class="btn btn-primary btn-lg btn-block"><i class="glyphicon glyphicon-home"></i> Search</button>
            </div>
        </form>
    </div><!-- /.row -->
    <br>
<!-- iFrame -->
    <div class="row">
        <div class="col-md-12">
            <iframe src="<?php echo $iframe; ?>"></iframe>
        </div>
    </div> <!-- /.row -->

非常感谢任何帮助和指导!

1 个答案:

答案 0 :(得分:3)

这有点脏,但它应该解决你的问题。首先从交换机中删除$selected变量。这不是必需的。相反,让我们使用switch设置iframe的网址,并将其留在那里。

if (isset($_POST['listings'])){
    $listings = $_POST['listings'];
} else if (isset($_GET['listings'])) {
    $listings = $_GET['listings'];
} else {
    $listings = 'chooseListing';
}

switch ($listings) {
    case 'captivaCondo':
        $iframe = 'http://sancapmls.com/san/idx/index.php?key=09b24ea58e44d14f9f5efed2bfd1edb2';
        break;
    case 'captivaResidential':
        $iframe = 'http://sancapmls.com/san/idx/index.php?key=90f72cf9a9a8fe486dff35cede5208b1';
        break;
    case 'sanibelCondo':
        $iframe = 'http://sancapmls.com/san/idx/index.php?key=2282d6f0deb849d6a4915685b7677848';
        break;
    case 'sanibelResidential':
        $iframe = 'http://sancapmls.com/san/idx/index.php?key=ceb9ec5fc169c6405d234e9b361f8915';
        break;
    case 'choose':
        $iframe = 'choose.php';
        break;
}

现在让我们在<option>元素中添加一个条件来放入selected="selected" atrribute:

<select name="listings" class="form-control input-lg">
     <option value="choose">Choose an option</option>
     <option value="captivaCondo" <?php if($listings == "captivaCondo") print('selected="selected"'); ?> >Captiva Condo</option>
     <option value="captivaResidential" <?php if($listings == "captivaResidential") print('selected="selected"'); ?> >Captiva Residential</option>
     <option value="sanibelCondo"  <?php if($listings == "sanibelCondo") print('selected="selected"'); ?> >Sanibel Condo</option>
     <option value="sanibelResidential" <?php if($listings == "sanibelResidential") print('selected="selected"'); ?> >Sanibel Residential</option>
</select>

这并不完美,但它可以帮助你成为一个不错的开始。