<form action="<?php bloginfo('url'); ?>/color-search/" method="post" accept-charset="utf-8" name="color-search">
<select name="color-choice" id="color-choice">
<option value="">Choose a Color</option>
<option value="Black">Black</option>
<option value="Blue">Blue</option>
<option value="Brown">Brown</option>
<option value="Gray">Gray</option>
<option value="Green">Green</option>
<option value="Orange">Orange</option>
<option value="Purple">Purple</option>
<option value="Red">Red</option>
<option value="Pink">Pink</option>
<option value="White">White</option>
</select>
</form>
<script type="text/javascript">
$("#color-choice").change(function() 开发者_开发问答{
$(this).closest("form").submit();
});
</script>
This code is rendered in my sidebar. When the drop down is changed the first time, the browser properly posts the form to http://myurl/color-search/ and the code on that page properly renders the results.
if(!empty($_POST['color-choice'])) {
$pageColor = $_POST['color-choice'];
}
The complication? The resulting page (http://myurl/color-search) has that exact same form on it. When I change the drop down, it submits, but PHP does not get any POST data.
So, the question is: why would this form fail to POST properly when posting back to its own URL versus when it is posting from other pages?
It makes no sense.
P.S. I know my jQuery treatment of the form is a bit heavy. I originally had it using onchange="this.form.submit();" but changed it just to see if it would behave the same. It continued to have the same error.
Thanks in advance :) Clif
You should also echo the $pageColor for it to be displayed. (Maybe you already did this but I cant see it in the code)
Thank you Lawrence Cherone for triggering the thought that led to this solution.
As I didn't write this code, I made the faulty assumption that the dropdown form was only being included from one place. In fact, it existed in two separate sidebar template files, one of which was used on the actual destination page and contained a typo in the URL. (plural instead of singular)
After fixing this, it started working.
Apparently WordPress was ignoring the typo and redirecting to the correct destination page, but the $_POST
data was being lost in the process.
Sorry for the false alarm. :)
精彩评论