似乎无法保持提交时选择的下拉值(对我来说是哑巴)

时间:2015-08-18 01:05:49

标签: php select drop-down-menu options search-form

我整天都在这一天,我的脑子被炸了,我希望我有道理。我甚至都不擅长使用p​​hp或类似的东西。

我正在尝试为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
}  
?>

2 个答案:

答案 0 :(得分:0)

测试一下: 重命名select标签的id并再次测试。

<select id="wpp_search[property_type]" 
    class="wpp_search_select_field wpp_search_select_field_property_type property_type" 
    name="wpp_search[property_type]">

答案 1 :(得分:0)

这似乎与搜索结果页面有关,阻止了这项工作。

我最终放弃了并开始使用css和新的小部件区域将他们的小部件放在我想要的位置,并让它看起来像我想要的。

这是我的最终代码:

&#13;
&#13;
<?PHP
$type = "hello";
$name_title = $_POST['derp'];
    if($_POST['submit']) {
        $name_title = $_POST['derp'];
?>

<?php 
} else {
?>
<div id="propertysearch">
	<div id="printit"><?php print_r($_POST ["wpp_search"]["property_type"]); ?></div>
	<div id="printit2"><?php print_r($_POST ["derp"]); ?></div>
<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="http://seoanseo.ie/property/listings/" 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>

<input id="property_type" class="" name = "wpp_search[property_type]">
<select id="demo" type="hidden" name="derp" onchange="myFunction()">
	<?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>
<script>
function myFunction() {
    var x = document.getElementById("demo").value;
    document.getElementById("property_type").value = x;
}
</script>
 </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>

<?php
}  
?>
&#13;
&#13;
&#13;

因为似乎开始搜索的表单的一部分(name =&#34; wpp_search [property_type]&#34;)没有以某种方式在结果页面上发布,我尝试将值复制到发布(&#34; derp&#34;)。

仍然不知道什么没有用它,但这是一个很好的学习经验,因为我还是相对较新的php和javascript。所以我开始工作的部分很有用:)

相关问题