簡體   English   中英

將javascript變量傳遞給php函數並正確引用

[英]passing a javascript variable to a php function and quoting it correctly

我在我的一個phtml文件中使用Zend Framework ..我有此代碼

<script>
 function foobar(id,type){
   var idarray =  <?php AppNamespace_General::getparentids( ?>id, type<?php ) ?>; // here  the id and type are from js
//the php function returns a json array to the js variable
 ......
  location.href = baseurl +'/somepage/id/'+id;
   }      

我如何正確地將js元素傳遞給php函數

php函數(已經考慮過通過ajax..its來完成它)

public static function getparentids($id, $type, $elmarray = '') {

        if (empty($elmarray)) { //avoiding redeclaration of array
            $elmarray = array();
        }
        switch (strtolower($type)) {
            case 'group':
            case 'product':
            case 'specification':

                $gp_handler = new PackAssist_Model_DbTable_Groups();
                $q = "SELECT * FROM t_groups WHERE group_id = $id";
                $sql = $gp_handler->getAdapter()->query($q);

                break;
            case 'part':
                $pt_handler = new PackAssist_Model_DbTable_Parts();
                $q = "SELECT * FROM t_parts WHERE part_id = $id";
                $sql = $pt_handler->getAdapter()->query($q);
                break;
        }
        $result = $sql->fetchAll();
        $i = 0;
        if (count($result) > 0) {
            foreach ($result as $row) {
                if (isset($row['group_parent_id']) && $row['group_parent_id'] != 0) {
                    if (in_array($row['group_id'], $elmarray)) {
                        $e = $row['group_parent_id'];
                    } else if ($row['group_parent_id'] != 0) {
                        $e = $row['group_id'];
                    }
                } else if (isset($row['part_group_id'])) {
                    $e = $row['part_group_id'];
                } else if ($row['group_parent_id'] == 0) {
                    break;
                }
                if (isset($e) && !empty($e)) {
                    array_push($elmarray, $e);
                }
                self::getparentids($e, 'group', $elmarray);
                $i++;
            }
        } else {
            array_push($elmarray, $id);
        }
        array_pop($elmarray); //removing the group of super parent group which we dont need

        if ($i == 0) { // just encode the array only once
            echo json_encode(array_reverse($elmarray));
        }
    }

如果使用jQuery,則可以執行以下操作來執行JSON請求:

$.ajax({
    type: 'GET',
    url: '/path/to/script.php',
    data: '{ id: '+id+', type: '+type+' }',
    contentType: 'application/json',
    dataType: 'json',
    success: function(data) {
         dataObject = JSON.parse(data);
         // process data
    },
        error: function(e) {
            console.log(e.message);
        }
});

您可以將現有的PHP代碼與此解決方案一起使用。 您指向的url只需要打印JSON結果,就像您當前在getparentids()所做的getparentids()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM