javascript 如何在 PHP 中获取 html <input type="hidden" id="hide"> 的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19873039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:06:29  来源:igfitidea点击:

How to get the value of html <input type=“hidden” id="hide"> in PHP?

javascriptphphtml

提问by user2971243

This is for generating the table.

这是用于生成表。

function makeTable()
{
var row_num = 20;
var cell_num = 4;

var tab = document.createElement('table');
tab.setAttribute('id','newtable');
var tbo = document.createElement('tbody');
tbo.setAttribute('id','tabody');
var cont;
for(var c = 0 ; c < row_num ; c++)
{
    var row = document.createElement('tr');
    for(var k = 0 ; k < cell_num ; k++)
    {
        var cell = document.createElement('td');
        if (k > 0)
        {
            cont = document.createElement("input");
            cont.setAttribute('type','text');
        }
        else
        {
            cont = document.createTextNode("0" + (c+1));
        }
        cell.appendChild(cont);
        row.appendChild(cell);
    }
    tbo.appendChild(row);
}
tab.appendChild(tbo);
document.body.appendChild(tab);
tab.setAttribute("align", "top-left");
}

This function is used to show the data in alert box but I want to retrieve document.getElementById('hide').value............

此函数用于在警报框中显示数据,但我想检索 document.getElementById('hide').value ........

function GetCellValues()
{
    var rows = document.getElementsByTagName('tr');
     var str = '';
    for (var c = 1 ; c < rows.length ; c++)
    {
        str += '\n';
        var row = rows[c];
        var inputs = row.getElementsByTagName('input');                
        for (var k = 0 ; k < inputs.length ; k++)
        {
            str += inputs[k].value + ', ';
        }

    }   
    document.getElementById('hide').value = str; 

I want to retrieve only this hidden control (i.e-"hide") in php.

我只想在 php 中检索这个隐藏的控件(即-“隐藏”)。

    alert(document.getElementById('hide').value); 
}


window.onload = function()
{
    makeTable();
};

</script>
    </head>
    <body>
      <h1><u>PROJECT</u> :</h1>
      <input type="button" value="Alert" onclick = "GetCellValues()">

I want to retrieve this id in php

我想在 php 中检索这个 id

      <input type="hidden" id="hide" /> <br />



      <div id="mytable">
       <table id = "project" border = "1" width ="60%" class = "newtable" cellpadding="10" cellspacing="0">
       <tr>
         <th align="center" width ="200px">Sl.No.</th>
         <th align="center" width ="200px">Project Name</th>
         <th align="center" width ="200px">ChangeDate</th>
         <th align="center" width ="200px">Changed By</th>
      </tr>
      </table>
     </div>
     </body>

Note:I want to retrieve the hidden control through id in php ##

注意:我想通过 php ## 中的 id 来检索隐藏的控件

I want the total php code to retrieve this hidden control data(i.e-input type="hidden" id="hide") because I'm a beginner

我想要全部的 php 代码来检索这个隐藏的控件数据(即输入type="hidden" id="hide"),因为我是一个初学者

回答by Tom

You have to add a name to your input so for example:

您必须在输入中添加一个名称,例如:

<input type="hidden" id="hide" name="foo" />

To retrieve the value in php after POST form submission you can do this:

要在 POST 表单提交后检索 php 中的值,您可以执行以下操作:

<?php $var1 = $_POST["foo"]; ?>

回答by jagruti

Can you please replace this line

你能更换这条线吗

<input type="hidden" id="hide" />

to

<input type="hidden" id="hide" name="hide" />

and use $_POST["hide"]in you php script.

$_POST["hide"]在您的 php 脚本中使用。

回答by Sunil Kumar

Input field must have some nameto access it

输入字段必须有一些name才能访问它

<input type="hidden" id="hide" name="hide" />

do normal fetch

做正常获取

$name = $_POST['hide']; 

回答by m-t

add name property to input type and value you want to get into you php

将 name 属性添加到要进入 php 的输入类型和值

<input type="hidden" id="hide" name="flag" value="1" />

and to retrieve the value in php after POST form

并在 POST 表单后检索 php 中的值

$var1 = $_POST["flag"];