Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Seems to me like you're just trying to fetch a name and an ID, but can't imagine why you'd need to do that every 1000 milliseconds - just seems like a lot of requests for data that isn't going to change?</p> <p>Personally I'd just suggest making the API call in your PHP before the page loads and you can <code>var name = "&lt;?=echo $name?&gt;";</code> as avalkab has said in order to access the variables in JavaScript after the page has been loaded.</p> <p>Should be able to use</p> <pre><code>$data = json_decode(file_get_contents("api.php?id=5&amp;parent=6")); // Specify or build query however you intended </code></pre> <p>If you are waiting for the user to interact (onclick or something) to call your javascript then if you expect to use data retrieved by AJAX to change data on the whole page - you might just want to reload the page, or revise your AJAX to make use of the data returned?</p> <p>EDIT: Since you are trying to change a colour you can simply use this line with jQuery</p> <pre><code>$("#kvadrat1").css({"background-color": vname}); </code></pre> <p>So your code would be like so:</p> <pre><code>//Call the yourAjaxCall() function every 1000 millisecond setInterval("yourAjaxCall()",1000); function yourAjaxCall() { //----------------------------------------------------------------------- // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/ //----------------------------------------------------------------------- $.ajax({ url: 'api.php', //the script to call to get data data: "", //you can insert url argumnets here to pass to api.php //for example "id=5&amp;parent=6" dataType: 'json', //data format success: function(data) //on recieve of reply { var id = data[0]; //get id var vname = data[1]; //get name $("#kvadrat1").css({"background-color" : vname}); } }); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload