Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Further to MikeW's excellent but incomplete answer, you need a way to request the time from the server and receive it back in the DOM.</p> <p><strong><em>There is only one way to do that: AJAX.</em></strong></p> <p>As Mike pointed out, the PHP code that you typed above only runs once: when the page is first generated. After the page has been generated and the document is "ready", you must use AJAX.</p> <p>Below is a fully-working, copy/pastable example to demonstrate one way this could work.</p> <p>Note that I had to over-ride the <code>orderedtime</code> variable because I don't know how/when you set that.</p> <p>HTML/javascript side: <strong>index.php</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"&gt;&lt;/script&gt; &lt;link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" /&gt; &lt;style&gt; #timeDiv{width:40%;height:200px;background:wheat;padding:10px;} &lt;/style&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { //var orderedtime = "&lt;?php echo $_SESSION['ordertime']; ?&gt;"; var orderedtime = ''; if (orderedtime === null || orderedtime == ''){ console.log("orderedtime is not set"); }else{ doAjax(); } window.setInterval(function(){ doAjax(); },2000); }); //END document.ready function doAjax() { $.ajax({ type: "POST", url: "get_the_time.php", data: "ordertime=" + orderedtime, success: function(myData) { $('#thetime').html(myData); } }); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="timeDiv"&gt; The time is: &lt;span id="thetime"&gt;&lt;/span&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <hr> <p>PHP side: <strong>get_the_time.php</strong></p> <pre><code>&lt;?php if (isset($_POST['ordertime']) != true) { $d = date("h:i:s"); }else{ $ot = $_POST['ordertime']; $d = date('h:i:s', (strtotime($ot) + (1 * 60))); } echo $d; </code></pre> <hr> <p><strong>IMPORTANT NOTE:</strong> </p> <p>When using AJAX, the response sent from the server is received inside the <code>success:</code> function, <strong><em>and no where else.</em></strong></p> <p>If you later wish to use that data, assigning it into a variable inside the success function will not work. The best way I have found is to stick the received data into an element of some kind (a hidden input field works great for this), and then retrieve it from there when needed.</p> <p>For example:</p> <pre><code>&lt;input type="hidden" id="myHiddenField" /&gt; $.ajax({ type: "POST", url: "get_the_time.php", data: "ordertime=" + orderedtime, success: function(myData) { $('#thetime').html(myData); $('#myHiddenField').val(myData); } }); </code></pre> <p>Then, inside some other javascript function, you can grab that data and assign it to some variable, thus:</p> <pre><code>var someVar = $('#myHiddenField').val(); </code></pre> <p>Hope this helps, late as it is.</p> <hr> <p><kbd><a href="https://stackoverflow.com/questions/18724011/post-not-posting-data/18724684#18724684">This stackoverflow post</a></kbd> has further information/explanation regarding AJAX. Check out the simplified AJAX examples at the bottom.</p>
    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.
    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