2013-12-16 81 views
0

我嘗試了多種不同的答案在這裏建議,但所有似乎並沒有與我想要完成的工作。PHP開關案例 - 保持選項

我試圖設置選擇框中的選項來選擇選項並提交它。

我正在使用開關盒,因爲它似乎是一種更快的方式來編程,以反對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 --> 

任何幫助和指導是非常感謝!

+2

你從來沒有設置'選擇=「選中」'在你的html – Horen

+0

不應該'case'選擇''是'case'chooseListing''? – OneOfOne

+0

'$ selected'變量似乎沒有被使用。您需要讓您的開關設置一個變量,以區分您選擇的選項,然後將「selected =」選定的「'屬性添加到更正的元素」

回答

2

這是一個有點髒,但它應該解決您的問題。首先從交換機中刪除$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> 

它不是完美的,但它應該幫助你作爲一個體面的開始。

+0

Thank you!This worked! – MoakDesigns

+0

@MoakDesigns非常歡迎!樂於幫助! – Crackertastic

0

嘗試使用:即你需要在HTML中添加selected<option>標籤像<option value="Your vale" selected='selected'>Your text</option>

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


     <?php $listingsOptions = array('choose'=>'Choose an option', 'captivaCondo'=>'captivaCondo', ......); 

     <select name="listings" class="form-control input-lg"> 
      <?php foreach($listingsOptions as $key=>$listing):?> 
       <option value="<?php echo $key;?>" <?php if($listings==$key): echo "selected = 'selected'" ; endif; ?> ><?php echo $listing;?></option> 
      <?php endforeach;?> 
     </select> 

,而不是

  <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>  
+0

我試過你的方法,它仍然將所有選項設置爲選中狀態,而不是選擇值並將該選項設置爲選中狀態。 – MoakDesigns

+0

I已經更新了我的答案,請檢查,不需要switch case語句。 –