我整天都在做这件事,我的头都炸了,我希望我是有道理的。我什至不擅长 php 或类似的东西。
我正在尝试为 worpress 自定义一个搜索表单,它有下拉列表并且搜索功能运行良好,我只是希望它在提交时保留下拉列表中的值。
我说类似的线程,但我无法将其应用于我的案例。
您的帮助将不胜感激。
这就是我所拥有的,对不起,如果它有点草率,我正处于我忘记我在做什么的阶段
<section id="searchpropertieswidget-4" class="widget wpp_property_attributes">
<div class="wpp_search_properties_widget">
<span class="wpp_widget_no_title"></span>
<?php
if($_POST["submit"]) {
$name_title = $_POST["wpp_search[property_type]"];
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
method="post" class="wpp_shortcode_search_form">
<input type="hidden"
name="wpp_search[pagination]"
value="off">
<input type="hidden"
name="wpp_search[strict_search]"
value="false">
<ul class="wpp_search_elements">
<li class="wpp_search_group wpp_group_not_a_group">
<ul class="wpp_search_group wpp_group_not_a_group">
<li for="wpp_search_element_4963"
class="typemargin wpp_search_label wpp_search_label_property_type">Type
<span class="wpp_search_post_label_colon">:</span>
</li>
<li class="wpp_search_form_element seach_attribute_property_type wpp_search_attribute_type_property_type ">
<div class="wpp_search_attribute_wrap">
<select id="wpp_search_element_6302"
class="wpp_search_select_field wpp_search_select_field_property_type property_type"
name="wpp_search[property_type]">
<?php
$names = array(
'' => 'Any',
'house_rental' => 'House (rental)',
'apartment_rental' => 'Apartment (rental)',
);
foreach ($names as $key => $name)
{
$selection = ($key === $name_title) ? 'selected="selected"' : '';
echo '\t<option value="' . $key . '" ' . $selection. '> ' . $name . '</option>';
}
?>
?>
</select>
更新:
因此,下面的代码用于保持下拉列表被选中,但我不得不重命名它,这意味着它没有连接到插件来进行搜索。
我重命名的原因是名称中的方括号 ("wpp_search[property_type]") 似乎导致 $name_title = $_POST["wpp_search[property_type]"]; 不工作。
任何人都可以为此提出解决方法吗?
<?PHP
$name_title = $_POST["derp"];
if($_POST["submit"]) {
$name_title = $_POST["derp"];
//Check the name title that it is selected or none.
if($name_title === none){
//if selected is none, add error to $errors array.
$errors['name_title'] = "Please select the title of your name!";
}
// sending form
if(empty($errors)){
$mail_sent = wp_mail( $to, $subject, $mailBody, $headers );
}
}
if ($mail_sent) {
?>
<h1 style="color: #007f00;">Request sent.</h1>
<?php
} else {
?>
<div id="propertysearch">
<section id="searchpropertieswidget-4" class="widget wpp_property_attributes"><div class="wpp_search_properties_widget"><span class="wpp_widget_no_title"></span>
<form id="" name="" action="<?php echo get_permalink(); ?>" method="post">
<div class="label-input-wrapper">
<ul class="wpp_search_elements">
<li><div class="form-label">Title</div></li>
<div class="form-input">
<li><select id="wpp_search_element_6302"
class="wpp_search_select_field wpp_search_select_field_property_type property_type"
name="derp">
<?php
$names = array(
'' => 'Any',
'house_rental' => 'House (rental)',
'apartment_rental' => 'Apartment (rental)',
);
foreach ($names as $key => $name)
{
$selection = ($key === $name_title) ? 'selected="selected"' : '';
echo '\t<option value="' . $key . '" ' . $selection. '> ' . $name . '</option>';
}
?>
?>
</select>
</li>
<li class="wpp_search_form_element submit"><input type="submit" class=" searchmargin wpp_search_button submit btn btn-large" value="Search"></li>
</ul>
</div>
</div>
</form>
</section>
</div>
<?php
}
?>