PHP Form sending value from an id instead of value

2020-05-06 10:08发布

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

<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

4条回答
走好不送
2楼-- · 2020-05-06 10:22

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

$someVar = $_REQUEST['someName'];

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

$someNewVar = $_REQUEST['someNewName'];

and in javascript, you can get its value like:

var somenewvar = document.getElementById("element_id_name").value;
查看更多
Juvenile、少年°
3楼-- · 2020-05-06 10:27

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

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.

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

查看更多
地球回转人心会变
4楼-- · 2020-05-06 10:38

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

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

$("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'];

update

set onclick=submitform() in submit button and also assign name and id attribute to form and write this

javascript

<script type="text/javascript">
function submitform()
{
  var hiddenData = document.getElementById('rout_markers').value;
  document.getElementById('someNewName').value = hiddenData;
  document.myform.submit();
}
</script>
查看更多
老娘就宠你
5楼-- · 2020-05-06 10:39

There is no such thing like DOM in HTTP protocol.

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

查看更多
登录 后发表回答