jquery - Pass array with ajax and php -
i need add class elements based on time without page reload
i creating array when page loads. have start time, end time , identifier.
$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"), );
then current time , grab identifier array. tried running script in separate file time.php using setinterval
can't pass $hrs
.
<ul> <li class="one">1</li> <li class="two">2</li> <li class="three">3</li> </ul> var classname = "<?php echo $class_name"; ?> $(classname).addclass("red");
what proper way of running won't need refresh page? did alerts error:
* working code below ****
<script> var hrs = '<?php echo json_encode($hrs); ?>'; //get active class 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"); } }) </script>
time.php
$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'] $h) { if ($h[0] <= $now , $now <= $h[1]) { $class_name = $h[2];fa break; } } $class = array( 'current_class' => ( $class_name ), ); echo json_encode($class);
replace:
var hrs = '<?php echo $hrs; ?>';
to:
var hrs = '<?php echo json_encode($hrs) ?>';
Comments
Post a Comment