php PHP表单从id而不是值发送值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5440823/
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-08-25 21:25:30  来源:igfitidea点击:

PHP Form sending value from an id instead of value

phpformsdomrequest

提问by Genadinik

What I have typically been doing is something like this in a form with a hidden field:

我通常所做的是在带有隐藏字段的表单中进行的操作:

   <input type="hidden" name="someName" value="someVal" />

and I have been able to get the value with something simple like

我已经能够通过一些简单的东西获得价值

$someVar = $_REQUEST['someVal'];

But now I am trying to send all the values in a DOM element id by something like this

但是现在我试图通过这样的方式发送 DOM 元素 id 中的所有值

<input type="hidden" name="someNewName" id="element_id_name" />

I am doing this correctly? Can this even be done? Or am I way off? How do I get the value out of the last line? Or how do I send that data to to request in a correct way?

我这样做正确吗?这甚至可以做到吗?还是我离题了?如何从最后一行中获取值?或者我如何以正确的方式将该数据发送到请求?

Thanks, Alex

谢谢,亚历克斯

回答by diEcho

you cant not retrieve data in php via ID it works on name..so you neeed to change

您不能通过 ID 在 php 中检索数据,它适用于名称..所以您需要更改

PHP

PHP

<form method="POST" action="nextpage.php" name="myform" id="myform">
<input type="text" id="rout_markers"  name="rout_markers"/>
<input type="hidden" name="someNewName" id="someNewName" value="" />
<input type="submit" id="send-btn"  class="button" value="SEND NOW" onclick="submitform()" />
</form>

jQuery

jQuery

$("form").bind('submit',function(e){
  e.preventDefault();
  var hiddenData=$("input[name=rout_markers]").val();
  // or var hiddenData=jQuery('#rout_markers').val(); 
  $("input[type=hidden][name=someNewName]").val(hiddenData);  
});

on

nextpage.php

下一页

retrieve data below way

以下面的方式检索数据

$_POST['someNewName'];

$_POST['someNewName'];

update

更新

set onclick=submitform()in submit button and also assign nameand idattribute to form and write this

onclick=submitform()在提交按钮中设置并分配nameid属性以形成并编写此

javascript

javascript

<script type="text/javascript">
function submitform()
{
  var hiddenData = document.getElementById('rout_markers').value;
  document.getElementById('someNewName').value = hiddenData;
  document.myform.submit();
}
</script>

回答by alex

You can only receive inputs with a nameattribute, which becomes the key of your $_POST(or equivalent) array.

您只能接收具有name属性的输入,该属性将成为$_POST(或等效的)数组的键。

Please use $_POST(or $_GET) instead of $_REQUEST, the latter also looks at cookies, and one could clobber what you expect. You should follow Principle of Least Privilege.

请使用$_POST(或$_GET) 代替$_REQUEST,后者也会查看 cookie,并且可能会破坏您的预期。您应该遵循最小权限原则

To send that one with an idto your server, give it a unique name or send the valueproperty to your server via XHR.

要将带有 的那个发送id到您的服务器,请为其指定一个唯一名称或value通过 XHR将该属性发送到您的服务器。

回答by Your Common Sense

There is no such thing like DOM in HTTP protocol.

HTTP 协议中没有像 DOM 这样的东西。

If you want to send an HTML form via HTTP request, you have to use valueattribute, that's all

如果你想通过 HTTP 请求发送一个 HTML 表单,你必须使用value属性,仅此而已

回答by shasi kanth

In the first case, you have to get the value of the field in php using:

在第一种情况下,您必须使用以下方法获取 php 中字段的值:

$someVar = $_REQUEST['someName'];

For the second case, you can get value in php using:

对于第二种情况,您可以使用以下方法在 php 中获取值:

$someNewVar = $_REQUEST['someNewName'];

and in javascript, you can get its value like:

在 javascript 中,你可以得到它的值,如:

var somenewvar = document.getElementById("element_id_name").value;