Php 如何使两个选择器都保留其值

Php 如何使两个选择器都保留其值,php,select,post,selector,Php,Select,Post,Selector,我编写了这段代码,希望这样,如果我为另一个选择器选择两个选择器中的任何一个,都不会重置为起始值。 我似乎不知道我在代码上哪里出错了。有什么想法吗 <?php $filter = true; $order = "&orderby=post_date&order=DESC"; if ($sort1 == '' && $sort2 == '' && $sort3 == '') { $sort1 = ' s

我编写了这段代码,希望这样,如果我为另一个选择器选择两个选择器中的任何一个,都不会重置为起始值。 我似乎不知道我在代码上哪里出错了。有什么想法吗

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

排序:
>老的
>名单

首先,您必须更改第二个表单的id。大多数浏览器都会获得第一个可用的DOM元素(在您的示例中是第一个表单,因为两个表单id相同)。由于您是基于onChange事件提交表单的,因此它可以正常工作,但请尝试遵循标准

您必须使用Ajax表单提交并相应地处理结果,这样整个页面将不会刷新,并且不会丢失第二个表单选择值

问候,,
在代码中,您忘记关闭if(){}


此外,如果您使用的是未定义的变量,请检查它们是否使用isset($var)设置,如果没有设置,则为它们提供一个默认值

除了我收到的关于未定义变量的通知外,它对我很有用

编辑:这是可行的,问题是你有两个表单,如果一个表单被提交,其他的值不会被提交,提交的每个表单只传递它的值,我现在只做了一个表单,如果你想要两个表单,在这两个表单中用其他值进行隐藏输入,然后用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>

我的头衔
排序:
>老的
>名单

我忘了在底部粘贴那段代码。但是我确实定义了未定义的变量:$filter=true;$sort1='';$sort2='';$sort3='';$view1='';$view2='';但仍然不起作用。我编辑了我的回复,现在检查它并尝试代码。它对我来说很好。你太棒了!是的,这起作用了。我明白你所说的“isset”的意思,也明白你把两个选择器放在同一个表格中的事实。谢谢顺便问一下,什么是“声誉”?我需要15个人投票支持你的答案。(尽管我读到了:)好的,我把ID名称改为order1和order2。我该如何使用ajax提交表单呢?如果我查一下它,它是不是很简单?让它工作起来了!将两个选择器置于同一窗体下。我仍然对你提到的Ajax表单提交感到好奇。尽管我做到了这一点,但如果我能让尽可能多的东西不刷新,这对网站是有好处的。@user2502562 Ajax表单可以作为jQuery插件提供,如果您不打算构建它的话。以下材料将帮助您解决问题。希望这有帮助。:)谢谢我去看看!
<?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>