如何在表单中发布空选项

问题描述 投票:0回答:1

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

如何才能使其 POST 的 value=""

<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
)
php html forms
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.