简体   繁体   English

使用Post发送默认的Select2选项

[英]Send default Select2 option with Post

I spent all day trying to find the answer and I just gave up. 我花了一整天的时间来寻找答案,但我只是放弃了。 Here's my problem, hope you can help me: 这是我的问题,希望您能为我提供帮助:

I need to pass multiple selections from Select2 through Post method. 我需要通过Post方法从Select2中传递多个选择。 Until here I have no problem if I pick them one by one on the Select2 options, I press submit and everything goes as it should. 直到这里,如果我在Select2选项上一个接一个地选择它们,我都没有问题,我按了提交,一切按预期进行。

The thing is when the page loads: I can see the default value with the last saved options, but if I press submit, without picking anything from the list, I expect that the same value will be passed again through the Post method, but no... It goes in blank, erasing the last option saved. 问题是页面加载时:我可以看到带有最后保存的选项的默认值,但是如果我按提交,而不从列表中选择任何内容,我希望相同的值将再次通过Post方法传递,但不会...变成空白,删除最后保存的选项。

In other words, the only way this thing works is if I pick an option (or multiple options) every time, cause if I don't, the default option is not sent. 换句话说,这件事唯一可行的方法是,如果我每次都选择一个(或多个)选项,因为如果我不选择,则不会发送默认选项。

Here´s an example of my code so you can check it out and give me a hand: 这是我的代码示例,因此您可以检查一下并帮我一下:

 .....
<title>page1</title>
 <script type="text/javascript">
    $(document).ready(function(){
        $(".myselect").select2();
    });
 </script>
</head>
<body>
<?php
$previouschoice="This is one This is two"; //this variable is populated with a mysqli query.
?>
<form method="post" action="page2.php">
            <select id="test" class="myselect" name="mychoices[]" multiple="multiple">
                <option value="<?php echo $previouschoice;?>" selected="selected"><?php echo $previouschoice;?></option>
                <option value="This is one">This is one</option>
                <option value="This is two">This is two</option>
                <option value="This is three">This is three</option>
            </select>
</form>
</body>

On page2.php I store the Post value into a mysql database for later retrieval, grabbing and concatenating the data in one variable, like this: 在page2.php上,我将Post值存储到mysql数据库中,以便以后检索,抓取和连接数据到一个变量中,如下所示:

$mypost =$_POST['mychoices'];
foreach ($mypost as $a) {
    $mychoices=$mychoices.' '.$a;
}

So $mychoices will eventually be $previouschoice back on page1.php 所以$mychoices最终将成为$previouschoice回到page1.php

Also tried the initSelection suggested in other posts, with the exact same output. 还尝试了其他帖子中建议的initSelection,其输出完全相同。 Did this: 做过这个:

    $(".myselect").select2().select2("val", null);
    $('#test').val('<?php echo $previouschoice;?>').trigger('change');

Please help! 请帮忙!

I'm not sure I understand your issue completely, but I'll give it a shot. 我不确定我是否完全理解您的问题,但是会给您一个机会。

  1. You can pick a few items from the select, submit and retrieve ok. 您可以从选择,提交和检索中选择一些项目。
  2. You can read the prior selections when the page reloads 重新加载页面时,您可以阅读先前的选择
  3. Resubmitting "forgets" the selection unless you pick the same choices again. 重新提交“忘记”选择,除非再次选择相同的选择。

If this works for you, for ever option, I would add something like 如果这对您有效,那么我会添加一些类似的内容

<?php if(in_Array("This is one", $previousChoice) echo "selected">

Where you would substitute "This is one" where necessary. 如果需要,您可以在其中替换“这是一个”。 This assumes $previousChoice is an array, of course. 当然,这假定$ previousChoice是一个数组。

That way, the options you selected in the first place will automatically be re-selected and should carry over if you submit the form again. 这样,您首先选择的选项将被自动重新选择,并且如果您再次提交表单,这些选项应该会保留下来。

Wayne 韦恩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM