簡體   English   中英

使用隱藏輸入將表單信息和附加變量從HTML發送到PHP

[英]Send form information and an additional variable from HTML to PHP using a hidden input

我有一個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";
    }

有誰知道如何解決這個問題? 提前致謝!

代碼的問題在於您發布的代碼中有多個名稱為geo的元素。 要么使用名稱為geo的單個元素,要么使用不同的名稱,如geo1,geo2,geo3。 getElementsByName(name)返回具有相同名稱的元素集合,因此,還需要按索引引用。 像document.getElementsByName('elementname')[0];

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";

}

在你的HTML中你必須使“geo”輸入標簽單一也改變方法而不是使用3你可以使用單個js方法和傳遞參數,這應該工作。

請參閱下面的代碼段

 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> ... 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM