2016-04-27 20 views
0

私は2つの選択ボックスがあるページを構築しています。 Select#1からSelect#2に項目を追加します。POST経由で選択からすべてのオプションを取得

送信ボタンをクリックすると、選択されているかどうかに関わらず、すべてのオプションが選択#2から取得されます。

<?php 
require('handlers/handler.php'); 
$active = $_POST['activeProperties']; 
$slider = $_POST['sliderProperties']; 
$array = array(); 
if(isset($_POST['submit'])){ 
    if(isset($_POST['sliderProperties'])){ 
     foreach($_POST['sliderProperties'] as $item){ 
      $array[] = $item; 
     } 
    } 
    echo "<pre>"; 
    print_r($array); 
    echo "</pre>"; 
} 
?> 

<form method="post" style="height: 600px;"> 
      <select id="source" name="activeProperties[]" data-text="Source list" data-search="Search for options" style="height: 600px;"> 
       <?php 
       $query = $handler11->query("SELECT ID, name FROM properties WHERE active = 'yes' "); 
       while($row = $query->fetch()){ 
        echo '<option value="'.$row['ID'].'">'.$row['name'].'</option>'; 
       } 
       ?> 
      </select> 
      <select id="destination" name="sliderProperties[]" data-text="Destination list" data-search="Search for options"> 
      </select> 
      <input type="submit" name="submit" class="btn btn-primary" value="Save"/> 
    </form> 

このコードでは、選択ボックスの最初の項目のみを取得しています。どのようにすべてのアイテムを手に入れることができますか?

+0

あなたは 'Fを選択されていません。)、これを試してみてください。あなたのデータベースの中で 'eld1'が使用されているか、' $ row ['name'] 'を使用する必要があります。どちらが真であるかによって異なります。 – Mihai

+0

これはタイプミスでした...最初の選択はデータベースから取り込まれています。どのように私は選択されているかどうかを宛先からすべてのオプションを得ることができますか? – BRG

答えて

2

あなたは、HTMLの複数選択を使用することができます(ドキュメントhere他のオプションはJavascriptてやっている

<form method="post" style="height: 600px;"> 
    <select multiple id="source" name="activeProperties[]" data-text="Source list" data-search="Search for options" style="height: 600px;"> 
     <?php 
      $query = $handler11->query("SELECT ID, name FROM properties WHERE active = 'yes' "); 
      while($row = $query->fetch()){ 
       echo '<option value="'.$row['ID'].'">'.$row['name'].'</option>'; 
      } 
     ?> 
    </select> 
</form> 

1

選択かない場合は、すべてのデータが必要な場合は、このクエリによって、あなたのPHPで同じ結果を得るカント:

$query = $handler11->query("SELECT ID, name FROM properties WHERE active = 'yes');

+0

すべてのデータを宛先に渡すselect – BRG

関連する問題