简体   繁体   English

如何使用PHP将HTML5数据属性保存到数据库?

[英]How to save HTML5 data- attribute to database using PHP?

I was wondering if and how I can save an HTML5 data- -attribute directly to my database using PHP? 我想知道是否以及如何使用PHP将HTML5 data-属性直接保存到数据库中?

I have aa button that creates a list item in a <ol> (ordered list). 我有一个按钮,可在<ol> (有序列表)中创建列表项。 Within every created list item, I store some information using the HTML5 data- attribute. 在每个创建的列表项中,我使用HTML5 data-属性存储一些信息。 Now I would like to save this with PHP to my database. 现在,我想用PHP将其保存到我的数据库中。

Is it possible to do that, or should I find another way. 有可能做到这一点,还是我应该找到另一种方式。

  • I would really like to be able to use this data-attribute, as it doesn't take up session, cookie-space or something else with limited amount of data. 我真的很想能够使用此数据属性,因为它不占用会话,cookie空间或其他数据量有限的内容。 Also this makes it possible to do everything on the client until it is saved. 而且,这使得可以在客户端上进行所有操作,直到将其保存。

I would recommend you use AJAX. 我建议您使用AJAX。

Using AJAX : 使用AJAX:

Use a bit of Javascript before you open the PHP file. 打开PHP文件之前,请使用一些Javascript。

You could use something like variable = document.getElementById('IdOfYour<ol>tag').getAttribute('data-something') to get the contents of your data attribute. 您可以使用诸如variable = document.getElementById('IdOfYour<ol>tag').getAttribute('data-something')之类的内容来获取数据属性的内容。

Then you open your PHP file using xmlhttp.open('GET','YourPHPFile.php?dataAttr=' + variable ,false/true') 然后使用xmlhttp.open('GET','YourPHPFile.php?dataAttr=' + variable ,false/true')打开PHP文件

And in your PHP file you could just use $_GET['dataAttr'] to access the data from your Data Attribute 在您的PHP文件中,您可以仅使用$_GET['dataAttr']从“数据”属性访问数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM