我制作了这段代码,并希望这样做,如果我选择另一个选择器中的任何一个不要重置为起始值。 我似乎无法弄清楚我的代码出错了。有什么想法吗?
<?php
$filter = true;
$order = "&orderby=post_date&order=DESC";
if ($sort1 == '' && $sort2 == '' && $sort3 == '') { $sort1 = ' selected="selected"'; }
if ($_POST['select1'] == 'newest') { $order = "&orderby=post_date&order=DESC"; $sort1 = ' selected="selected"'; $sort2 = ''; $sort3 = ''; }
if ($_POST['select1'] == 'oldest') { $order = "&orderby=post_date&order=ASC"; $sort2 = ' selected="selected"'; $sort1 = ''; $sort3 = ''; }
if ($_POST['select1'] == 'most-popular') { $order = "&meta_key=post_views_count&orderby=meta_value_num&order=DESC"; $sort3 = ' selected="selected"'; $sort1 = ''; $sort2 = ''; }
if ($view1 == '' && $view2 == '' && $view3 == '') { $view1 = ' selected="selected"'; }
if ($_POST['select2'] == 'list') { $view1 = ' selected="selected"'; $view2 = ''; }
if ($_POST['select2'] == 'thumbnail') { $view2 = ' selected="selected"'; $view1 = ''; }
?>
<?php if ($filter) { ?>
<div class="secondNavCategory">
<nav class="categorymenu">
<div class="leftNavCategory">
<form method="post" id="order">
SORT:
<select name="select1" onchange='this.form.submit()'>
<option value="newest"<?=$sort1?>>NEWEST</option>
<option value="oldest"<?=$sort2?>>OLDEST</option>
<option value="most-popular"<?=$sort3?>>MOST POPULAR</option>
</select>
</form>
</div>
<div class="leftNavCategory">
<form method="post" id="order">
VIEW:
<select name="select2" onchange='this.form.submit()'>
<option value="list"<?=$view1?>>LIST</option>
<option value="thumbnail"<?=$view2?>>THUMBNAIL</option>
</select>
</form>
</div>
</nav>
</div> <!-- secondNavCategory -->
<?php $filter = false; ?>
<?php } ?>
答案 0 :(得分:1)
首先你要改变第二种形式的id。大多数浏览器都会获得第一个可用的DOM元素(在您的情况下,第一个表单是因为您的表单ID都相同)。由于您基于onChange事件提交表单,它将正常工作,但请尽量遵循标准。
您必须使用Ajax表单提交并相应地处理结果,因此不会刷新整个页面,并且您不会丢失第二个表单选定值。
此致 HBKsagar
答案 1 :(得分:1)
在您的代码中,您忘记关闭if(){}
</div> <!-- secondNavCategory -->
<?php } ?> <!-- You are missing this line -->
你也在使用未定义的变量,检查它们是否用isset($ var)设置,如果没有则给它们一个默认值。
除了我得到的未定义变量的通知,它对我有用。
编辑:这是有效的,问题是你有2个表单,如果提交了一个表单,其他的值都没有提交,每个提交的表单只传递它的值,我现在只制作一个表单,如果你想要两个表单,使用彼此的值进行隐藏输入,并使用JS进行设置。或者使用AJAX<?php
$filter = true;
$sort1 = '';
$sort2 = '';
$sort3 = '';
$view1 = '';
$view2 = '';
$view3 = '';
if (isset($_POST['select1'])) $select1 = $_POST['select1'];
else $select1 = '';
if (isset($_POST['select2'])) $select2 = $_POST['select2'];
else $select2 = '';
$order = "&orderby=post_date&order=DESC";
if ($sort1 == '' && $sort2 == '' && $sort3 == '') { $sort1 = ' selected="selected"'; }
if ($select1 == 'newest') { $order = "&orderby=post_date&order=DESC"; $sort1 = ' selected="selected"'; $sort2 = ''; $sort3 = ''; }
if ($select1 == 'oldest') { $order = "&orderby=post_date&order=ASC"; $sort2 = ' selected="selected"'; $sort1 = ''; $sort3 = ''; }
if ($select1 == 'most-popular') { $order = "&meta_key=post_views_count&orderby=meta_value_num&order=DESC"; $sort3 = ' selected="selected"'; $sort1 = ''; $sort2 = ''; }
if ($view1 == '' && $view2 == '' && $view3 == '') { $view1 = ' selected="selected"'; }
if ($select2 == 'list') { $view1 = ' selected="selected"'; $view2 = ''; }
if ($select2 == 'thumbnail') { $view2 = ' selected="selected"'; $view1 = ''; }
?>
<html>
<head><title>My title</title></head>
<body>
<?php if ($filter) { ?>
<div class="secondNavCategory">
<nav class="categorymenu">
<form method="post" id="order">
<div class="leftNavCategory">
SORT:
<select name="select1" onchange='this.form.submit()'>
<option value="newest"<?php echo $sort1; ?> >NEWEST</option>
<option value="oldest"<?php echo $sort2; ?> >OLDEST</option>
<option value="most-popular"<?php echo $sort3; ?> >MOST POPULAR</option>
</select>
</div>
<div class="leftNavCategory">
VIEW:
<select name="select2" onchange='this.form.submit()'>
<option value="list"<?php echo $view1; ?> >LIST</option>
<option value="thumbnail"<?php echo $view2; ?> >THUMBNAIL</option>
</select>
</div>
</form>
</nav>
</div> <!-- secondNavCategory -->
<?php } ?>
</body>
</html>