php 如何从标签中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19480914/
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
How to get the value from a label
提问by spacebean
Okay so the problem is that I have a label which is in a form and I'm trying to access this label when I press a button, my form looks like:
好的,问题是我有一个表单中的标签,当我按下按钮时我试图访问这个标签,我的表单看起来像:
<form method = 'POST'>
<img src = 'ItemIcons/<?php echo $ItemPicture;?>.png' alt = '<?php echo $row["ItemDesc"];?>'/>
<label name = 'lblItemName'><?php echo $ItemName; ?></label>
<br>
<label> <?php echo "Gold:" . $row["ItemPrice"]; ?> </label>
<input type = 'submit' value = 'Buy <?php echo $ItemName; ?>' name = 'ItemPurchase'/>
</form>
and my code for calling the button looks like:
我调用按钮的代码如下所示:
if(isset($_POST['ItemPurchase'])) {
$ItemName = $_POST["lblItemName"];
?>
<script>
alert('<?php echo $ItemName;?>');
</script>
<?php
}
Currently it alerts an empty value, it works when I place it as a textbox and not a label, but it needs to be a label sadly, any help would be great, thanks.
目前它会提醒一个空值,当我将它作为文本框而不是标签放置时它可以工作,但遗憾的是它需要是一个标签,任何帮助都会很棒,谢谢。
回答by spacebean
Labels aren't submitted with forms, so it isn't going to work the way you have it. Why not create a hidden input field beneath it that echoes the same value? Then your $_POST[''] will work.
标签不与表单一起提交,因此它不会像您拥有的那样工作。为什么不在它下面创建一个隐藏的输入字段来回显相同的值?然后您的 $_POST[''] 将起作用。
<input type="hidden" name="lblItemName" value="<?php echo $ItemName; ?>">
回答by Gary Hayes
Very easily done.
很容易做到。
alert($('label').html());
回答by MC ND
Without great changes in your code, you can get what you need using a hidden field initialized with the same value that the label.
无需对代码进行重大更改,您就可以使用使用与标签相同的值初始化的隐藏字段来获得所需的内容。
回答by bjuice
You can't get the value of a label from a form post because the information is not sent. What you can do is add a hidden field to your form with the value of the label
您无法从表单帖子中获取标签的值,因为未发送信息。您可以做的是使用标签的值在表单中添加一个隐藏字段
回答by sumit
Labels are simply lables for any other visible entities on your HTML page, with the help of javascript and its libraries you may access the HTML within label to send it to the server. But this approach is not good, as suggested by others, it would be better to use hidden fields to post such data without using javascript.
标签只是 HTML 页面上任何其他可见实体的标签,借助 javascript 及其库,您可以访问标签内的 HTML 以将其发送到服务器。但是这种方法并不好,正如其他人所建议的那样,最好使用隐藏字段来发布此类数据而不使用 javascript。
Further I would like to add that if your page is using HTML5 then you can use data attribute with most of the html tags to store data linked to those elements. use jQuery to access those data, serialize it and send it to server through Ajax requests.
此外,我想补充一点,如果您的页面使用 HTML5,那么您可以使用带有大多数 html 标签的 data 属性来存储链接到这些元素的数据。使用 jQuery 访问这些数据,将其序列化并通过 Ajax 请求将其发送到服务器。