0

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';
  • I think your global.js snippet is missing some code. i see no click event. – Kevin B May 07 '15 at 18:09
  • I would recommend looking into Meteor.js for reactivity. Its truly amazing for stuff like this, and its all in JavaScript! (https://www.meteor.com/) – Ian Wise May 07 '15 at 18:10

0 Answers0