Note that there are some explanatory texts on larger screens.

plurals
  1. POPass array with ajax and php
    primarykey
    data
    text
    <p>I need to add a class to some elements based on the time without page reload</p> <p>I am creating this array when the page loads. I have a start time, an end time and an identifier.</p> <pre><code>$hrs = array( array("2013-07-27 21:00", "2013-07-27 22:00", "one"), array("2013-07-27 22:00", "2013-07-27 23:00", "two"), array("2013-07-27 23:00", "2013-07-28 00:00", "three"), ); </code></pre> <p>Then I get the current time and grab the identifier from the array. I tried running this script in a separate file time.php using <code>setInterval</code> but I can't pass <code>$hrs</code>.</p> <pre><code>&lt;ul&gt; &lt;li class="one"&gt;1&lt;/li&gt; &lt;li class="two"&gt;2&lt;/li&gt; &lt;li class="three"&gt;3&lt;/li&gt; &lt;/ul&gt; var className = "&lt;?php echo $class_name"; ?&gt; $(className).addClass("red"); </code></pre> <p>what is the proper way of running this so I won't need to refresh the page? I did something like this but it alerts error:</p> <p><em><strong></em>* Working code below <em>*</em>***</strong></p> <pre><code>&lt;script&gt; var hrs = '&lt;?php echo json_encode($hrs); ?&gt;'; //get active class from time.php $.ajax({ url : 'http://domain.com/time.php', type : 'POST', data : { val : hrs }, dataType : 'json', success : function (result) { className = result['current_class']; }, error : function () { alert("error"); } }) &lt;/script&gt; </code></pre> <p>time.php</p> <pre><code> $hrs = json_decode($_POST['val']); date_default_timezone_set('Europe/Bucharest'); $date = date('Y/m/d H:i'); $test = strtotime($date); $now = date("Y-m-d H:i", $test); $class_name = ""; foreach ($_POST['val'] as $h) { if ($h[0] &lt;= $now and $now &lt;= $h[1]) { $class_name = $h[2];fa break; } } $class = array( 'current_class' =&gt; ( $class_name ), ); echo json_encode($class); </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