访问 html php 中的 5 个数据属性
access html 5 data attribute in php
我有一个表格,其中包括
<input id="user-id" type="text" data-user-id="" name="message" placeholder="type and send..." class="form-control">
data-user-id
HTML5 数据属性。当我提交表单时,我想在 php class 中使用数据属性的值。如何在 php?
中访问此数据属性
如果您将此表单直接 post 发送到 PHP 脚本,则无法访问数据属性。如果你想能够做到这一点,你需要在 Javascript 中监听表单提交,然后在提交时获取你需要的数据并将其 post 到 PHP 脚本处理一下。
这是一些示例代码(未经测试!),使用 jQuery。
$('#form_name').on('submit',function(){
var user_id = $('#user-id').attr('data-user-id');
$.post('form_handle.php',{'user_id':user_id,[...],[...]});
});
其中 [...]
是要 post 发送给处理程序的任何其他表单数据。然后处理程序可以以正常方式检索 posted 值,例如$_POST['user_id']
.
您的用户 ID 应位于单独的隐藏字段中,例如:
<input type="hidden" name="user_id" value="123">
<input type="text" name="message" placeholder="type and send..." class="form-control">
您的 message
输入不应该有 user-id
的 id
并且根本不需要 data-user-id
。
JavaScript 使用了数据属性。隐藏输入将用户不需要看到的值传递给 PHP。两者都不是真正对用户隐藏的。
我有一个表格,其中包括
<input id="user-id" type="text" data-user-id="" name="message" placeholder="type and send..." class="form-control">
data-user-id
HTML5 数据属性。当我提交表单时,我想在 php class 中使用数据属性的值。如何在 php?
如果您将此表单直接 post 发送到 PHP 脚本,则无法访问数据属性。如果你想能够做到这一点,你需要在 Javascript 中监听表单提交,然后在提交时获取你需要的数据并将其 post 到 PHP 脚本处理一下。
这是一些示例代码(未经测试!),使用 jQuery。
$('#form_name').on('submit',function(){
var user_id = $('#user-id').attr('data-user-id');
$.post('form_handle.php',{'user_id':user_id,[...],[...]});
});
其中 [...]
是要 post 发送给处理程序的任何其他表单数据。然后处理程序可以以正常方式检索 posted 值,例如$_POST['user_id']
.
您的用户 ID 应位于单独的隐藏字段中,例如:
<input type="hidden" name="user_id" value="123">
<input type="text" name="message" placeholder="type and send..." class="form-control">
您的 message
输入不应该有 user-id
的 id
并且根本不需要 data-user-id
。
JavaScript 使用了数据属性。隐藏输入将用户不需要看到的值传递给 PHP。两者都不是真正对用户隐藏的。