在php中获取自定义属性值

时间:2015-02-21 17:52:15

标签: php attributes

我尝试使用PHP创建的自定义属性获取输入字段的值。这是我的代码:

<form action="uploadform.php" method="post"> 
<input type="text" name="email" id="email" mynewattribute="myemail">
<input type="submit" name="submit">
</form>

//uploadform.php
<?php
//I know $name = $_POST['email']; will give me the value but I would like to get the value of the input field with "mynewattribute" and not name. Is it possible?
?>

1 个答案:

答案 0 :(得分:1)

网络浏览器不知道如何处理自定义属性,因此只会忽略它。提交表单时发送的唯一数据是“成功”元素的值。因此,您的自定义数据永远不会被发送,接收脚本永远无法读取。

将此类数据放入隐藏输入字段的最佳位置。一种可能性是使用带方括号的名称,PHP会自动将其转换为数组。 e.g。

<input type="text" name="email[value]">
<input type="hidden" name="email[magic]" value="true">

填充这样的数组:

$_POST['email']['value'] = '';
$_POST['email']['magic'] = 'true';