用ajax和php传递数组

用ajax和php传递数组

问题描述:

I need to add a class to some elements based on the time without page reload

I am creating this array when the page loads. I have a start time, an end time and an 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 I get the current time and grab the identifier from the array. I tried running this script in a separate file time.php using setInterval but I 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 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:

* Working code below ****

<script>

var hrs = '<?php echo json_encode($hrs); ?>';

//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");
    }
})

</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'] as $h) {
    if ($h[0] <= $now and $now <= $h[1]) {
        $class_name = $h[2];fa
        break;
    }
    } 

$class = array(
        'current_class' => ( $class_name ),
    );
    echo json_encode($class);

我需要根据没有页面重新加载的时间为某些元素添加一个类 p>

我在页面加载时创建此数组。 我有一个开始时间,一个结束时间和一个标识符。 p>

  $ 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“,”三“),
); 
  code>  pre> 
 
 

然后我得到了 当前时间并从数组中获取标识符。 我尝试使用 setInterval code>在单独的文件time.php中运行此脚本,但我无法传递 $ hrs code>。 p>

 &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>

strong> em> *工作代码 * em> *** p>

 &lt; script&gt; 
 
var hrs ='&lt;?php echo json_encode($ hrs);  ?&gt;'; 
 
 //从time.php获取活动类
 $ .ajax({
 url:'http://domain.com/time.php',
n type:'POST'  ,
data:{val:hrs},
 dataType:'json',
 success:function(result){
 className = result ['current_class']; 
 
},
 error:function(  ){
 alert(“error”); 
} 
})
 
&lt; / script&gt; 
  code>  pre> 
 
 

time.php p>

  $ hrs = json_decode($ _ POST ['val']); 
 
 date_default_timezone_set('Europe / Bucharest'); 
 $ date = date('Y / m / d  H:i'); 
 
 $ test = strtotime($ date); 
 
 $ now = date(“Ymd H:i”,$ test); 
 
 $ class_name =“”; \  n 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); \  n  code>  pre> 
  div>

Replace:

var hrs = '<?php echo $hrs; ?>';  

To:

var hrs = '<?php echo json_encode($hrs) ?>';

If you are sending data using POST

type : 'POST',

You have to access it using $_POST, you are using $_GET.

$_POST['val']

Neither language has any clue about the internals of the other language, your best bet to pass a full array is to encode the AJAX side array to JSON, pass that to your PHP script, and then use json_decode to decode it into a PHP array.

You could also try passing the parameters with square brackets on the end, with the same name as php interprets this as an array. i.e.

file.php?p[]=1&p[]=2&p[]=3&p[]=4&p[]=5

Would result in $_GET['p'] being a 5 item array containing 1 - 5.

The same is also true of post, if you post multiple p[]'s, you will end up with $_POST['p'] as an array of all your elements.