I’m a pretty novice programmer who is making an application where there are multiple divs with a class of jcontainer. Each jcontainer has an id associated with it, which corresponds with a field called apptid in a database. In the Jquery, I send out an AJAX request every five seconds to a php page to retrieve a field called currentlocation in the database (that corresponds with the apptid), which happens for each jcontainer.
Here is my code:
HTML:
<div class='jcontainer' data-param='jcontainer_IDNUMBER'>
<button class='checkIn' data-param='button_IDNUMBER'>Check In</button>
<form method='post' class='myForm' action=''>
<select name='locationSelect' class='locationSelect' data-param='location_IDNUMBER'>
<option value='1'>Exam Room</option>
<option value='2'>Exam Room 2</option>
<option value='3'>X-Ray Room</option>
<option value='1000'>Check Out</option>
</select>
</form>
<div class='finished' style='color:#ff0000;'>Checked Out</div>
</div>
jQuery:
<script type="text/javascript">
$(document).ready(function() {
setInterval(function(){
$('.jcontainer').each(function() {
var $e = $(this);
var dataid = $e.data("param").split('_')[1] ;
$.ajax({
url: 'heartbeat.php',
method: 'post',
contentType: "application/json",
dataType: "json",
data: dataid,
success: function(data){
var msg = $.parseJSON(response);
alert(msg);
}
});
});
},5000);
//other code that I didn't post, because it's not really relevant, but can post if needed be
and php page:
<?php
$hostname = 'localhost';
$username = '******';
$password = '*************';
$apptid = $_POST['dataid'];
$db = new PDO("mysql:host=$hostname;dbname=sv", $username, $password);
$statement = $db->prepare("SELECT currentlocation FROM schedule WHERE apptid = :apptid");
$statement->execute(array(':apptid' => $apptid));
$row = $statement->fetch();
$currentlocation = array('CurrentLocation' => $row['currentlocation']);
echo json_encode($currentlocation);
?>
The problem is I can’t get any response from the alert(msg). Why is this? I checked Chrome Developer and no errors came up.
The end goal is to have the currentlocation number recorded in a jquery variable. ex -> var currentLocation = Number
Thanks for any and all help, and if you need more details to clear things up, I can happily post!
This block of code:
should read
cause the
responsevariable is not declared and undefined so you should use thedatavariable in your parseJSON method as that is the actual data received. Hope this solves it for you.