简体   繁体   中英

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?

I have aa button that creates a list item in a <ol> (ordered list). Within every created list item, I store some information using the HTML5 data- attribute. Now I would like to save this with PHP to my database.

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. Also this makes it possible to do everything on the client until it is saved.

I would recommend you use AJAX.

Using AJAX :

Use a bit of Javascript before you open the PHP file.

You could use something like variable = document.getElementById('IdOfYour<ol>tag').getAttribute('data-something') to get the contents of your data attribute.

Then you open your PHP file using xmlhttp.open('GET','YourPHPFile.php?dataAttr=' + variable ,false/true')

And in your PHP file you could just use $_GET['dataAttr'] to access the data from your Data Attribute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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