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

I've been at this all day and my head is fried, I hope I make sense. I'm not even close to good at php or anything like it.

I'm trying to customize a search form for worpress, it has dropdowns and the search function is working fine, I just want it to keep the value in the dropdown on submission.

I say similar threads but I'm unable to apply it to my case.

Your help would be much appreciated.

Here's what I've got, sorry if it's a bit sloppy, I'm at the stage where I've lost track of what I'm doing

<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>

UPDATE:

So, the code below works for keeping the dropdown selected, but I've had to rename the which means it's not connecting to the plugin to do the search.

The reason I renamed the is that the square brackets in the name ("wpp_search[property_type]") seemed to cause $name_title = $_POST["wpp_search[property_type]"]; not to work.

Can anyone suggest a workaround for this?

<?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
}  
?>

</div>

test this: rename id for select tag and test again.

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

It seems that it was something to do with the search result page that was preventing this working.

I gave up in the end and set about using css and new widget areas to put their widget where I wanted and make it look how I wanted.

here was my final code:

<?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
}  
?>

Because it seemed the part of the form that was starting the search (name = "wpp_search[property_type]") wasn't posting on the results page somehow, I tried duplicating the value to post ("derp").

Still don't know what didn't work out with it, but it was a good learning experience as I'm still relatively new to php and javascript. So the parts that I did get to work were useful :)

</div>