0

Im using Zend Framework ..in one of my phtml file's i have this code

<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;
   }      

How can i correctly pass the js elements to the php function

The php function(Already thought of doing it via ajax..its quite complex)

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));
        }
    }
15
  • What are you trying to accomplish with the statement? I don't think this will work as php tags are already parsed and rendered when the js comes into scene on client side and the foobar is called. Commented Jun 12, 2012 at 3:45
  • What isn't being passed correctly? That should work shouldn't it? Commented Jun 12, 2012 at 3:45
  • @jcolebrand its not working as it shows syntax error Commented Jun 12, 2012 at 3:46
  • @The village idiot the php function returns a json array to the js variable Commented Jun 12, 2012 at 3:46
  • @ubercooluk Could you explain a little better what you're trying to do? What I see is, you're trying to pass JS vars id, type to the PHP function getparentids back to the JS function foobar. If that's so, you'll need some Ajax as all php in the page is processed before outputting any HTML/JS code. Commented Jun 12, 2012 at 3:50

1 Answer 1

1

If you use jQuery, you can do the following to execute the JSON request:

$.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);
        }
});

You can use your existing PHP code with this solution. The url you point to would just have to print the JSON result, as you are currently doing in getparentids().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.