<div id = "Class" class = "Class_ticket" onclick = "caller()" value = "1">
I am trying to get value
of div
where ID is Class
when Div
is clicked.
function caller(){
$.ajax({
url: "test.php",
type: "get",
data: {
Class_info: $('#Class').val()
}
}).done(function(data) {
$('#Result').text(data);
alert(data);
});
}
Looks like it isn't reading the value of Div
when clicked. Am I using AJAX wrongly?
test.php
<?php
$a = $_GET['Class_info'];
echo($a);
?>
CodePudding user response:
div
tag dont have value attribute. So you can use $('#Class').attr('value')
;
check snippet below..
function caller(){
alert($('#Class').attr('value'));
$.ajax({
url: "test.php",
type: "get",
data: {
Class_info: $('#Class').attr('value')
}
}).done(function(data) {
$('#Result').text(data);
alert(data);
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id = "Class" class = "Class_ticket" onclick = "caller()" value = "1">test</div>
CodePudding user response:
You can use this little trick:
<div id "" onclick="caller()" value="1">
<input type="hidden" value="1" id="Class" />
</div>
note that the input is hidden!
And here is your ajax:
function caller() {
$.ajax({
url: "test.php",
type: "get",
data: {
Class_info: $('#Class').val()
}
}).done(function(data) {
$('#Result').text(data);
alert(data);
});
}
CodePudding user response:
Try this:
function caller(){
$.ajax({
url: "test.php?Class_info=test",
type: "get",
data: {
Class_info: $('#Class').val()
}
}).done(function(data) {
$('#Result').text(data);
alert(data);
});
}
I added ?info=test
which in theory PHP should be able to take
if PHP takes the value, then instead of test put a variable containing the desired value