【问题标题】:Send form information and an additional variable from HTML to PHP using a hidden input使用隐藏输入将表单信息和附加变量从 HTML 发送到 PHP
【发布时间】:2018-11-21 12:59:24
【问题描述】:

我有一个 HTML 表单,我将它们的变量正确发送到 PHP 文件。我有不同的容器,有几个与地理相关的选项。这意味着,用户可以在国家/地区/经济级别的显示数据之间进行选择。为了在 PHP 中了解哪种表示方式正在过滤用户,我想使用一个额外的隐藏输入,其值为 1,2 或 3,具体取决于所选方式。我无法更改这个隐藏的输入值。

我使用以下代码:

<div class="popcountry">
        <div class="funcion" onclick="country_function();setGeo()">
        <h2 class="geography" style="text-align:center;" id="geoselect">Country
        <input type="hidden" name="geo">
        </h2>
        </div>
        <span class="popmenu"  id="countrypopmenu">
        <br>

        <label class="container" style="text-align:center" id="selectControlcountry"> <b>Clear</b>
            <input type="checkbox" id="selectall" />
            <span class="checkmark2" style="background-color:#F0FFF0"></span>
        </label>
        <div class="scrollbox2">
        <label class="container">Afghanistan
            <input type="checkbox" id="Afghanistan" name="country[]" value="Afghanistan" checked="checked">
            <span class="checkmark"></span>
        </label>
        <label class="container">Albania
            <input type="checkbox" id="Albania" name="country[]" value="Albania" checked="checked">
            <span class="checkmark"></span>
        </label>
...

    <div class="popregion">
    <div class="funcion"  onclick="region_function();setGeo2()">
    <h2 class="geography" style="text-align:center" id="geoselect">Region
    <input  type="hidden" name="geo">
    </h2>
    </div>
    <span class="popmenu"  id="regionpopmenu">
    <br>

    <label class="container" style="text-align:center" id="selectControlregion"> <b>Clear</b>
        <input type="checkbox"  id="selectall" />
        <span class="checkmark2" style="background-color:#F0FFF0"></span>
    </label>
    <label class="container">Africa
        <input type="checkbox" id="Africa" name="region[]" value="Africa" checked="checked">
        <span class="checkmark"></span>
    </label>
    <label class="container">Americas
        <input type="checkbox" id="Americas" name="region[]" value="Americas" checked="checked">
        <span class="checkmark"></span>
    </label>
...
    <div class="popclass">
    <div class="funcion" onclick="class_function();setGeo3()">
    <h2 class="geography" style="text-align:center" id="geoselect">Country Classification
    <input  type="hidden" name="geo">
    </h2>
    </div>
    <span class="popmenu"  id="classpopmenu">
    <br>

    <label class="container">Developed
        <input type="checkbox" id="Developed" name="classification[]" value="Developed" checked="checked">
        <span class="checkmark"></span>
    </label>
    <label class="container">Economies in transition
        <input type="checkbox" id="Economies in transition" name="classification[]" value="Economies in transition" checked="checked">
        <span class="checkmark"></span>
    </label>
...

这些函数在javascript中定义为:

function setGeo(){
        var geo =getElementByName("geo");
        geo.value= "1";
    }
    function setGeo2(){
        var geo =getElementByName("geo");
        geo.value = "2";
    }
    function setGeo3(){
        var geo =getElementByName("geo");
        geo.value = "3";
    }

有谁知道如何解决这个问题?提前致谢!

【问题讨论】:

  • 发生了什么问题,你想要什么?
  • 当我在 PHP 中回显地理变量时,该变量未定义。我想根据用户单击的位置更改该变量的值
  • 如果您在输入上添加idgeo,则您可以选择元素。但是,id 应该是唯一的。
  • 那么,您希望当用户点击任何geo 时,此输入将被更改?不是其他geo

标签: javascript php html forms input


【解决方案1】:
function setGeo(){
    var geo = document.getElementsByName("geo")[0];
    geo.value= "1";
}
function setGeo2(){
    var geo = document.getElementsByName("geo")[0];
    geo.value = "2";
}
function setGeo3(){
    var geo = document.getElementsByName("geo")[0];
    geo.value = "3";

}

【讨论】:

  • 当我这样做时:$geo= htmlspecialchars($_POST["geo"]);回声“$地理”;在PHP中我得到未定义的变量,所以当我点击时值不会改变
  • 是的,但您应该删除名称为 geo 的多个输入。因此,对于我们的 JS 代码,它只会为第一个元素设置。其余的将具有空值,因此您无法在 PHP 代码中获取它。
  • 我做到了。我正在尝试在 jsfiddle 中复制我的这部分代码。当我点击提交时,代码必须返回 geo 的值,但我得到一个错误。这是代码:jsfiddle.net/ac320qrw/7
  • document.getElementByName 不是函数。如果是拼写错误,document.getElementsByName 返回一个数组。该代码不起作用。
【解决方案2】:

在您的 HTML 中,您必须将“geo”输入标记设为单个,并且在方法中进行更改而不是使用 3,您可以使用单个 js 方法并传递参数,这应该可以工作。

请看下面的代码sn-p

function setGeo(geoVal) {

  document.getElementById('geodata').value = geoVal;
  console.log(document.getElementById('geodata').value);
}

/* blank functions, defined in order to repair the snippet */
function country_function() { }
function class_function() { }
function region_function() { }
<input type="hidden" id="geodata" name="geo">
<div class="popcountry">
  <div class="funcion" onclick="country_function();setGeo(1)">
    <h2 class="geography" style="text-align:center;" id="geoselect">Country

    </h2>
  </div>
  <span class="popmenu" id="countrypopmenu">
        <br>

        <label class="container" style="text-align:center" id="selectControlcountry"> <b>Clear</b>
            <input type="checkbox" id="selectall" />
            <span class="checkmark2" style="background-color:#F0FFF0"></span>
  </label>
  <div class="scrollbox2">
    <label class="container">Afghanistan
            <input type="checkbox" id="Afghanistan" name="country[]" value="Afghanistan" checked="checked">
            <span class="checkmark"></span>
        </label>
    <label class="container">Albania
            <input type="checkbox" id="Albania" name="country[]" value="Albania" checked="checked">
            <span class="checkmark"></span>
        </label> ...

    <div class="popregion">
      <div class="funcion" onclick="region_function();setGeo(2)">
        <h2 class="geography" style="text-align:center" id="geoselect">Region

        </h2>
      </div>
      <span class="popmenu" id="regionpopmenu">
    <br>

    <label class="container" style="text-align:center" id="selectControlregion"> <b>Clear</b>
        <input type="checkbox"  id="selectall" />
        <span class="checkmark2" style="background-color:#F0FFF0"></span>
      </label>
      <label class="container">Africa
        <input type="checkbox" id="Africa" name="region[]" value="Africa" checked="checked">
        <span class="checkmark"></span>
    </label>
      <label class="container">Americas
        <input type="checkbox" id="Americas" name="region[]" value="Americas" checked="checked">
        <span class="checkmark"></span>
    </label> ...
      <div class="popclass">
        <div class="funcion" onclick="class_function();setGeo(3)">
          <h2 class="geography" style="text-align:center" id="geoselect">Country Classification
          </h2>
        </div>
        <span class="popmenu" id="classpopmenu">
    <br>

    <label class="container">Developed
        <input type="checkbox" id="Developed" name="classification[]" value="Developed" checked="checked">
        <span class="checkmark"></span>
        </label>
        <label class="container">Economies in transition
        <input type="checkbox" id="Economies in transition" name="classification[]" value="Economies in transition" checked="checked">
        <span class="checkmark"></span>
    </label> ...

【讨论】:

  • 我试过了,但是如果我没有定义一个值: 当我读入它时它不会返回任何东西的PHP。如果我在定义隐藏输入的时候设置了一个初始值=0,它不会改变,所以就像它没有进入setGeo()函数一样
  • 您可以将 name 属性添加到 HTML 输入元素。您只需使用 id 来使用 JavaScript 和 name 属性来操作您的字段,以确保 PHP 接收字段的值。
  • 在您的 html 中,如果未设置“geo”值,则不会在 php 中找到该元素,因为它未设置或使用 post 发送。因此,传递默认值可能会有所帮助,或者在您的 php 中,您可以检查是否使用 isset() 方法设置了值来验证它。
  • 我设置了一个默认值 =0 并且我的 PHP 读取了 0 值,所以函数 setGeo() 没有被执行
【解决方案3】:

您的代码的问题是您发布的代码中有多个名称为 geo 的元素。要么使用名称为 geo 的单个元素,要么使用不同的名称,例如 geo1、geo2、geo3。 getElementsByName(name) 返回具有相同名称的元素的集合,因此需要通过索引进行引用。像 document.getElementsByName('elementname')[0];

【讨论】:

  • 它的名称相同,因为我想根据我单击的位置更改该元素值。我的意思是,我命名了三个地理区域,但目标是将地理区域更改为 1,2 或 3,具体取决于您是否单击国家、地区或国家分类。如果我使用相同的元素,如果我必须将其更改为 1,2 或 3 no,我无法区分?
  • 我建议保留一个隐藏变量,比如 geo 并在函数调用中根据位置 setGeo(1)、setGeo(2) 和 setGeo(3) 传递值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-11
  • 2014-11-16
  • 2018-04-16
  • 2018-11-09
  • 1970-01-01
  • 2017-06-16
  • 2016-07-02
相关资源
最近更新 更多