2016-10-12 80 views
-1

我想通過一個php數組到ajax函數,因爲我需要ajax函數來填充一些動態創建的表。所以爲了做到這一點,我想我必須傳遞我想要傳遞給數組的值,並且一次傳遞它們,這樣我的ajax函數就可以聲明一次,以避免上次動態表的問題只在最後填充(儘管我不太確定這是爲什麼)。還有如何$ _GET我將發送的數組參數,但到目前爲止結果是相反的,因爲它是第一個填充的動態表。所以這讓我相信只有第一個數組值被我的ajax發送。我的繼承人代碼:傳遞php數組作爲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']) . '"]'>;`?),但它像它的不可靠。

那麼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 
             } 
            } 
           } 
          } 
         } 
        } 
     ?> 
+0

你可以將你的php數組作爲json的