简体   繁体   English

所选选项未放入发布变量中

[英]Selected option does not get put into post variable

Edit: Thanks for the help everyone, I've changed the topic since I discovered that the problem was more specific to PHP. 编辑:谢谢大家的帮助,自从我发现问题更特定于PHP以来,我更改了主题。

I'm new to jQuery and Javascript and I'm creating a page with a search function that requires the user to enter a region to retrieve information that is region-specific. 我是jQuery和Javascript的新手,正在创建一个带有搜索功能的页面,该功能要求用户输入区域以检索特定于区域的信息。 Within a form, I have included a drop-down menu: 在一个表单中,我包含了一个下拉菜单:

<select name="region" id="region">

   <option selected="selected" value="na">NA</option>
   <option value="euw">EUW</option>
   <option value="eune">EUNE</option>
   <option value="tr">TR</option>
   <option value="lan">LAN</option>
   <option value="las">LAS</option>
   <option value="br">BR</option>
   <option value="ru">RU</option>
   <option value="oce">OCE</option>
   <option value="kr">KR</option>
</select>

Even if I try to select another region other than the default (NA), when the form is submitted, the value for the $_POST region variable always remains as 'na'. 即使我尝试选择默认区域(NA)以外的其他区域,在提交表单时,$ _ POST区域变量的值也始终保持为“ na”。

Also, I have a script on form submit: 另外,我在表单提交时有一个脚本:

 <script>
     $(function () {
        $('#myForm').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'redirect.php',
            data: $('form').serialize(),
            success: function () {
                location.reload();

            }
          });

        });

      });
    </script>

My form: 我的表格:

<form id="myForm">
        <li id="search">
                <input type="text" name="name" id="name" placeholder="Search Word"/>
                <input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" tabindex="-1" />

                <select name="region" id="region">

                    <option selected="selected" value="na">NA</option>
                    <option value="euw">EUW</option>
                    <option value="eune">EUNE</option>
                    <option value="tr">TR</option>
                    <option value="lan">LAN</option>
                    <option value="las">LAS</option>
                    <option value="br">BR</option>
                    <option value="ru">RU</option>
                    <option value="oce">OCE</option>
                    <option value="kr">KR</option>

                </select>


            </li>
</form>

redirect.php redirect.php

<?php
    session_start();

    if (isset($_POST['name']) && isset($_POST['region'])) {
        $_SESSION['name'] = $_POST['name'];
        $_SESSION['region'] = $_POST['region'];

    }
    if (isset($_POST['name'])) {
        $_SESSION['name'] = $_POST['name'];
        $_POST['region'] = 'na';   // I think this is where the problem persists
        $_SESSION['region'] = $_POST['region'];
    }

?> ?>

It seems like $_POST['region'] is not set. 似乎未设置$ _POST ['region']。 I've included session_start in both files. 我在两个文件中都包含了session_start。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

OP changed the topic of the question, going to leave my code here just for reference, but I'm fully aware it's not an answer anymore. OP更改了问题的主题,将我的代码留在这里仅供参考,但我完全意识到这不再是答案。

I wrote a quick CodePen to test what was going on in your problem, I didn't check with a POST request because I don't have your code, but with jQuery all I had to do was remove the selected atribute from NA: 我写了一个快速的CodePen来测试问题的根源,我没有用POST请求进行检查,因为我没有您的代码,但是使用jQuery,我要做的就是从NA中删除所选的属性:

http://codepen.io/leofontes/pen/ZBGZZZ http://codepen.io/leofontes/pen/ZBGZZZ

<select name="region" id="region">

   <option value="na">NA</option>
   <option value="euw">EUW</option>
   <option value="eune">EUNE</option>
   <option value="tr">TR</option>
   <option value="lan">LAN</option>
   <option value="las">LAS</option>
   <option value="br">BR</option>
   <option value="ru">RU</option>
   <option value="oce">OCE</option>
   <option value="kr">KR</option>
</select>

<button id="button">Something</button>

And JS: 和JS:

$(document).ready(function() {
   $("#button").click(function() {
      alert($('#region').val());
   });
});

Just click the button to see which option was selected, it works.. Not 100% sure if this is what your problem revolved around but I'm pretty sure it was that simple. 只需单击按钮即可查看选择了哪个选项,它可以正常工作。.不是100%确定这是否是您所困扰的问题,但是我很确定这很简单。

Hope this is what you were looking for, if not let me know so that I can help you out some more ;) 希望这就是您想要的,如果没有让我知道,以便我可以帮助您更多;)

Try this: 尝试这个:

var $region = $('#region');
$region.val([]);//now $('#region').val() return undefined
//exacttext version
region.find('option').filter(function (index, elem) { return $(this).html() === textSearchValue; }).prop("selected",true);
//containstext version
region.find('option').filter(function (index, elem) { return $(this).html().indexOf(textSearchValue)>0; }).prop("selected",true);

PS: always cache the elements in a $variable PS:始终将元素缓存在$ variable中

what I usually do is this: 我通常这样做的是:

$(document).on("change",function(){
  if($("#region").val()!="na"){
    var region=$("#region").val();
    alert(region);
  }
});

with this you will be able to check if the value is actually changing, and now you can use that variable wherever you want. 有了它,您将能够检查该值是否在实际更改,现在您可以在任何需要的地方使用该变量。 And it will work for everytime the value changes until you submit Hope this helps! 它将在每次值更改时起作用,直到您提交希望对您有所帮助!

Ok so I found out what I had done wrong... It was because of a small error in ordering of conditionals in redirect.php 好的,所以我发现我做错了...这是由于redirect.php中的条件排序的小错误

if (isset($_POST['name'])) {
    $_SESSION['name'] = $_POST['name'];
    $_POST['region'] = 'na';   // Where I thought the problem persisted
    $_SESSION['region'] = $_POST['region'];
}

This part of the code will always run even after the $_POST variables are set. 即使设置了$ _POST变量,这部分代码也将始终运行。 My intention was for it to run when $_POST for region was not set. 我的意图是在未设置区域的$ _POST时运行它。 As a result, I changed it to the following and it worked. 结果,我将其更改为以下内容并成功运行。

if (!isset($_POST['region'])) {
        $_SESSION['name'] = $_POST['name'];
        $_POST['region'] = 'na';
        $_SESSION['region'] = $_POST['region'];
    }

Thanks again everyone! 再次感谢大家!

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

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