2

Here is what I'm trying to do:

In "app/design/frontend/default/default/template/catalogsearch/advanced/form.phtml" I have the following php statement

<?php $x=$this->getStoreCategories(); ?>

If I am not wrong $x would be an object and when i display it in php I am able to view it.

I need to convert this object into a javascript object (JSON) as i need to pass it using jQuery Ajax

But when I execute

<script>
var obj = JSON.parse('<?php echo json_encode($x) ?>');
alert(obj.toSource());
</script>

The alert gives me an empty object

Can anyone please help me out

Thanks in advance

2
  • 1
    What makes you think javascript on the client can parse and execute PHP code on your server? Commented Aug 3, 2012 at 11:52
  • do a var_dump on $x and specify what it contains. Commented Aug 3, 2012 at 12:26

1 Answer 1

1

You could do;

<script>
var obj = <?php echo json_encode($x) ?>;
</script>

When the page outputs, your JSON object will be in the page. Or am I missing something about what you want to do?

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.