如何在表单中发布空选项

时间:2018-05-09 12:56:43

标签: html forms

我的表单中有各种表单字段,但是它们都要求用户能够提交空值。以下始终是POST的第一个选项中的文本。

我怎样才能使POST的值=“”

<select class="form-control" name="type">
  <option value="" hidden>All</option>
  <option value="Apartment">Apartment</option>
  <option value="Villa">Villa</option>
  <option value="Hotel">Hotel</option>
</select>

如果我转储$ _POST ['type']我会在选择no选项时得到'All'。我需要它空着。

更新:添加PHP函数

/*****************
Setup the type filter
*****************************/
if (!$_POST['type'] == '') { 

  $type = $_POST['type']; 
  $type_query = array(       
    'meta_key'  =>  'type_name',
    'value'     =>  array($type)              
  );
  array_push($meta_query, $type_query);        
  $_SESSION['type'] = $type;

} elseif(isset($_SESSION['type'])) { 

  $type = $_SESSION['type']; 
  $type_query = array(       
    'meta_key'  =>  'type_name',
    'value'     =>  array($type)           
  );
  array_push($meta_query, $type_query);
}  



//POST dump

Array
(
    [type] => All
    [location] => All Locations
    [bedfrom] => 
    [bedto] => 
    [ref] => 
    [issearch] => 1
)

//SESSION dump

Array
(
    [location] => All Locations
    [type] => All
)

1 个答案:

答案 0 :(得分:0)

<select class="form-control" name="type">
  <option value="" hidden>All</option>
  <option value="Apartment">Apartment</option>
  <option value="Villa">Villa</option>
  <option value="Hotel">Hotel</option>
</select>

if (isset($_POST['type']) ) { 
    $type = $_POST['type']; 
} elseif(isset($_SESSION['type'])) { 
     $type = $_SESSION['type']; 
}

$meta_query = [];
if(isset($type))//type == '' which means its set 
{
    $type_query = array(       
        'meta_key'  =>  'type_name',
        'value'     =>  array($type)              
    );
    array_push($meta_query, $type_query);
}
print_r($meta_query);