将php数组作为链接参数传递给ajax函数

时间:2016-10-12 13:08:13

标签: javascript php arrays ajax

我正在尝试将php数组传递给ajax函数,因为我需要ajax函数来填充一些动态创建的表。所以为了做到这一点,我想我必须传递我打算在数组中传递的值,并立即传递它们,这样我的ajax函数只能声明一次,以避免最后一个动态表的问题只在最后填充(虽然我不太确定这是否是原因)。以及如何$ _GET我将发送的数组参数,但到目前为止结果是相反的,因为它是第一个填充的动态表。所以这让我相信我的ajax只发送了第一个数组值。继承我的代码:

<script>
function cat_product(val1){
    if(window.XMLHttpRequest){
        xhttp = new XMLHttpRequest();
    }else{
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.onreadystatechange = function(){
        if(xhttp.readyState == 4 && xhttp.status == 200){
            window.alert(val1.length);//this proves to me that the ajax works
            document.getElementById("cat_products").innerHTML = xhttp.responseText;
        }
    };

    xhttp.open("POST","ajax/cat_product.php?id="+val1,true);
    xhttp.send();
}

</script>

    <?php
    $store_key = $_SESSION['store_key'];   
        $query = "SELECT category_key FROM product_category WHERE store_key = '{$store_key}' ORDER BY category_name ASC";
        $category_set = mysqli_query($connect,$query);//for loop
        confirm_query($connect,$category_set);
        $number = mysqli_num_rows($category_set);

      $_SESSION['sesssion_category'] = array();
      $_SESSION['sesssion_productCategory'] = array();
       $_SESSION['value'] = array();
      for($i=0;$i<$number;$i++){
     $sessionCategory = mysqli_fetch_array($category_set);

      $_SESSION['sesssion_category'][] = $sessionCategory['category_key'];
        }



     foreach ($_SESSION['sesssion_category'] as $value) {

    $_SESSION['value'][] = $value; // this is the array where i store the values i want to pass
     /* this where i create another array who's key are named after each values
from $_SESSION['sesssion_category'] and allocate items that matches each key  from the database */
    if($_SESSION['store_key'] == $_SESSION['user_id']){
       $query = "SELECT DISTINCT product_key FROM products WHERE store_key = '{$store_key}' AND category_key = '{$value}' ORDER BY branch_code,product_name ASC";
      $product_set = mysqli_query($connect,$query);
     confirm_query($connect,$product_set);
     while($product = mysqli_fetch_array($product_set)){
     $_SESSION['sesssion_productCategory'][$value][] =  $product['product_key'];
        }

    }else{
     $query = "SELECT DISTINCT product_key FROM products WHERE store_key = '{$store_key}' AND branch_code = '{$_SESSION['branch_code']}' AND category_key = '{$value}' ORDER BY branch_code,product_name ASC";
     $product_set = mysqli_query($connect,$query);
     confirm_query($connect,$product_set);
     while($product = mysqli_fetch_array($product_set)){
     $_SESSION['sesssion_productCategory'][$value][] =  $product['product_key'];
        }

    }



    ?>
    <ol id="chaps">
    <li><button class="button button-3d button-leaf" ><?php echo ucfirst(get_category($value));?></button>

    <table id="datatable1" class="table table-striped table-bordered assignments hide" cellspacing="0" width="100%">
    <caption>Product Details</caption>
    <thead>
     <tr>
       <th>#</th>
       <th>Product Name</th>
       <th>Total Pieces</th>
       <?php 
       if($_SESSION['store_key'] == $_SESSION['user_id']){
       echo "<th>Action</th>";  
       }else{
        echo ""; 
        }
        ?>
      </tr>
    </thead>

      <tfoot>
    <tr>
     <th>#</th>
     <th>Product Name</th>
     <th>Total Pieces</th>
       <?php 
     if($_SESSION['store_key'] == $_SESSION['user_id']){
      echo "<th>Action</th>";  
     }else{
       echo ""; 
      }
       ?>
    </tr>
    </tfoot>
    <tbody id="cat_products">

    </tbody>
    </table>


      </li>
    </ol>

    <?php
    }

    ?>
    </div>   


    </div>

    </div>




<!--<script>
var x = <?php echo '["' . implode('", "', $_SESSION['value']) . '"]' ?>;
window.onload = cat_product.apply(this,x);
</script>-->
<script>
var x = <?php echo json_encode($_SESSION['value']) ?>;
window.onload = cat_product.apply(this,x);
</script>

我尝试了(<?php echo '["' . implode('", "', $_SESSION['value']) . '"]'?&gt;;`),但它就像是不可靠。

然后是我处理我的ajax的ajax页面 cat_product.php

    $value = $_GET['id'];//$value = explode(",", $_GET["id"]);


    foreach($_SESSION['value'] as $sessionValue){

    if($sessionValue == $value){
    //create an array of $_SESSION['sesssion_productCategory'] category keys
     $ary = array_keys($_SESSION['sesssion_productCategory']);
    foreach($ary as $cat_key){
    //Count the created array inorder not to exceed number of products
    for($j=0;$j<count($ary);$j++){
    //query for displaying products
    if($sessionValue == $cat_key){
    $value2 = $sessionValue;
foreach($_SESSION['sesssion_productCategory'][$value2] as $key){
    ?>
    <tr>
    </tr>    
    <?php
                                         }
                                    }
                                }
                            }
                        }
                    }
        ?>

1 个答案:

答案 0 :(得分:2)

尝试将数组编码为JSON并在参数中发送对象。

var myArray = ["one", "two", "three"];

sendarray = JSON.stringify(myArray);

xhttp.send("myArray="+encodeURIComponent(sendarray));

在解析POST数据的页面上,调用$ _REQUEST [&#39; myArray&#39;]上的json_decode()函数。

这也可用于将javascript对象发布到php页面。祝你好运。