Basically I have this function that retrieves a value from my MySQL database once I click it (ajax)
And from that point on, it updates itself every second incase the value within the database changes
It does update itself as of now, but I have to click the button to execute the function in the first place. I can't seem to figure out how to remove it
Javascript/Ajax global.js
setInterval(function(){
var name = $('input#name').val();
if ($.trim(name) != '') {
$.post('ajax/name.php', {name: name}, function(data) {
$('div#name-data').text(data);
});
}
},1000);
HTML:
<input type="submit" id="name-submit" value="Grab">
<div id="name-data"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="js/global.js"></script>
name.php
require '../db/connect.php';
$query = mysql_query("
SELECT `names`.`location`
FROM `names`
WHERE `names`.`name` = '" . mysql_real_escape_string(trim($_POST['name'])) . "'");
echo (mysql_num_rows($query) !== 0) ? mysql_result($query, 0, 'location') : 'Name not found';