Get data-id value in php


To get value of data attribute, use −

$(“yourSelector”).data()

The following is our input type with data attribute −

<input type="text" id="txtValue" src="" data-value="90">

Example

Following is the code −

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Document</title>
</head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<body>
   <input type="text" id="txtValue" src="" data-value="90">
</body>
<script>
   var value = $('#txtValue').data('value');
   console.log("The value is=" + value);
</script>
</html>

Output

The output is as follows −

Get data-id value in php

Get data-id value in php

Updated on 03-Oct-2020 14:57:06

  • Related Questions & Answers
  • How to use jQuery to get the value of HTML attribute of elements?
  • How to use attr() method in jQuery to get the value of an attribute?
  • How to get an attribute value in jQuery?
  • How to get the value of id attribute in jQuery?
  • How to get the value of src attribute in jQuery?
  • How to get the value of custom attribute in jQuery?
  • jQuery [attribute!=value] Selector
  • How to use *= operator in jQuery attribute selector?
  • How to use GET method to send data in jQuery Ajax?
  • Get value of any attribute from XML data in JavaScript?
  • How to find an element based on a data-attribute value in jQuery?
  • How to change the value of an attribute using jQuery?
  • How to use OR Operation in jQuery Attribute Selectors?
  • How to find an element based on a data-attribute value using jQuery
  • How do we use # in jQuery Attribute Selector?

First of all, remove id="myId" from <a></a>. ID must be unique. Use class.

First_Page.php

<?php
$teacherClass = new TeacherSearch();
$teachers = $teacherClass->showAllTeachers();
if (is_array($teachers)) {
  foreach ($teachers as $teacher) {
    $src = $teacher['userID'];
    ?>
    <div class="teacher-info">
      <p class="teacherLabel">
        NAME:<?php echo $teacher['name']; ?>
      </p>
      <p class="teacherLabel">
        HEADLINE:
        <?php echo $teacher['headline'];?>
      </p>
      <p class="teacherLabel">
        LOCATION: <?phpecho $teacher['location'];?>
      </p>
      <!--BUTTON GOES HERE-->
      <a class="openModal" data-id="<?php echo $teacher['userID'] ?>" data-toggle="modal" href="#myModal">
        Hire <?php echo $teacher['name']; ?>
      </a>
    </div>
  <?php }
}?>

Put below code in footer before end of </body> tag.

<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">

    </div>
  </div>
</div>

JS

<script>
  $('.openModal').click(function(){
      var id = $(this).attr('data-id');
      $.ajax({url:"modal_ajax.php?id="+id,cache:false,success:function(result){
          $(".modal-content").html(result);
      }});
  });
</script>

Create a new file modal_ajax.php.

[NOTE: Remember, this page name modal_ajax.php is used in script tag also. if you are planning to change the name of this page, change page name there in script tag too. Both are related.]

<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal">&times;</button>
  <h4 id="modalTitle" class="modal-title"></h4>
</div>
<div class="modal-body">
  <?php echo "ID: ".$_GET['id'];?>
</div>
<div class="modal-footer">
  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>

Already answered similar question. Please have a look Passing data via Modal Bootstrap and getting php variable?

How To Set Tow Data-Id In Tag With Code Examples

Good day, folks. In this post, we’ll examine how to find a solution to the programming challenge titled How To Set Tow Data-Id In Tag.

<a id="click" data-id="dataid1" data-id1="dataid2" data-id2="dataid4" href="javacript:void(0)">
	Click Here
</a>

$('#click').click(function(){
  var dataID = $(this).attr('data-id');
  var dataID1 = $(this).attr('data-id1');
  var dataID2 = $(this).attr('data-id2');
	
 });	
    
    

We were able to fix the How To Set Tow Data-Id In Tag problem by looking at a number of different examples.

How can I set the data-ID attribute?

How to Get the data-id Attribute

  • The attr() Method. To get the content of the data-id attribute use the attr() method, which will return a string: $(this).attr("data-id") // returns the string "457"
  • The data() Method. In case of jQuery >= 1.4.3 version, you can use the data() method:
  • The attr() and data() Methods. The .

How to get data attr in JavaScript?

Approach: First, select the element which is having data attributes. We can either use the dataset property to get access to the data attributes or use the . getAttribute() method to select them by specifically typing their names.08-Dec-2021

What is a data attribute HTML?

The data-* attribute is used to store custom data private to the page or application. The data-* attribute gives us the ability to embed custom data attributes on all HTML elements.

How do I get the data-ID of an element?

“get element by data-id javascript” Code Answer's

  • var element = document. getElementById('myDivID');
  • var dataID = element. getAttribute('data-id');
  • var dataID = $('myDivID'). data('data-id');

How set data attribute in HTML?

Creating an HTML Data Attribute Any HTML element can have any number of data attributes added to its opening tag. We simply type data- followed by the name of our attribute into the element's opening tag alongside any other attributes we're already using.22-Nov-2020

What is data item ID?

The itemid global attribute provides microdata in the form of a unique, global identifier of an item. An itemid attribute can only be specified for an element that has both itemscope and itemtype attributes.14-Sept-2022

How use data filter attribute in HTML?

How to use it:

  • Download and place the JavaScript file jquery-data-filter. min. js after jQuery.
  • Categorize your data using data attributes as follows: < div id = "language-list" > < p class = "filter-me" data-html = "HTML5" >
  • Filter the list by data name: // no results.
  • Filter the list by data values:

How use data attribute in HTML and JavaScript?

To create a data attribute in HTML, simply start the property with data- and pick a name that makes sense for you. You will want to use the assignment operator, = , to set the data attribute value to a string . If your data attribute requires two or more words, just add a dash between each word.21-Dec-2020

How add data attribute to select option?

HTML <option> data-* Attribute A data-* attribute on an <option> tag attaches additional data to the option item. To create a custom attribute, replace * with a lowercase string, such as data-id , data-status , or data-location .

Can I use attr ()?

attr() Note: The attr() function can be used with any CSS property, but support for properties other than content is experimental, and support for the type-or-unit parameter is sparse. The attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the stylesheet.13-Sept-2022

How do you find the value of data

How to Get the data-id Attribute.
The attr() Method. To get the content of the data-id attribute use the attr() method, which will return a string: $(this).attr("data-id") // returns the string "457" ... .
The data() Method. In case of jQuery >= 1.4.3 version, you can use the data() method: ... .
The attr() and data() Methods. The ..

What is data

The data-reactid attribute is a custom attribute that react can easily identify its components within the DOM. Just like the HTML “classes” and “id” attributes, “data-reactid” helps in uniquely identifying the element .

How to fetch data

Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to find the data-id attribute of an HTML element.

How to get data field value in jQuery?

You can use this jquery data() syntax for get data-id attribute value. $("selector"). data("textval"); You can use this jquery data() syntax for get data-textval attribute value.