Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7092279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:17:38+00:00 2026-05-28T08:17:38+00:00

Firstly, I have search Stack Overflow for the answer, but I have not found

  • 0

Firstly, I have search Stack Overflow for the answer, but I have not found a solution that works.

I am using an MVC framework (yii) to generate some views and throw them in an array. Each view is a card, and I have an array of cards ($deck) as well as an array of arrays of cards ($hands, the list of hands for each player). I’m simply trying to set a javascript variable on the front-end to store the hands created in PHP. My view has, it is worth noting, multiple lines. In fact, my current test view consists only of:

test
test

I therefore used json_encode, but it’s giving me the following error when I use $.parseJSON():

Uncaught SyntaxError: Unexpected token t

I read elsewhere that it is required (for whatever reason) to use json_encode twice. I have tried this, but it does not help.

With a single json_encode, the output of echoing $hands (followed by an exit) looks pretty healthy:

[["test\ntest","test\ntest","test\ntest","test\ntest", etc...

But when I do not exit, I get a syntax error every time.

Edit: Here is a sample of my code. Note that $cards is an array of HTML normally, but in my simplified case which still errors, includes only the two lines of ‘test’ as mentioned above.

    $deck = array();
    foreach ($cards as $card) {
        $deck[] = $this->renderPartial('/gamePieces/cardTest', 
                array('card'=>$card), true);
    }
    $hands = Cards::handOutCards($deck, $numCards , $numPlayers);
    $hands = json_encode($hands);

    echo $hands; exit;

With JavaScript, I am doing the following:

var hands = $.parseJSON('<?php echo json_encode($hands); ?>');

It errors on page load.

Any help would be appreciated!

Thanks,

ParagonRG

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:17:38+00:00Added an answer on May 28, 2026 at 8:17 am
    var hands = $.parseJSON('<?php echo json_encode($hands); ?>');
    

    This will result in something like:

    var hands = $.parseJSON('{"foobar":"baz'"}');
    

    If there are ' characters in the encoded string, it’ll break the Javascript syntax. Since you’re directly outputting the JSON into Javacript, just do:

    var hands = <?php echo json_encode($hands); ?>;
    

    JSON is syntactically valid Javascript. You only need to parse it or eval it if you receive it as a string through AJAX for instance. If you’re directly generating Javascript source code, just embed it directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, I've never used threads, but have found lots of examples on the internet
Firstly I have tried to manage it with the viewWillDisapper-method, but this not detailed
I have created firstly ASP.NET MVC 2 . and write more functionality. After I
Firstly, I do not have any malicious intent out of this question. I would
I have a modeling question related to profiles. Firstly, I have looked into using
I have a form with many inputs, but not all inputs must be filled
Firstly, whilst I'm aware that gzipping HTML would probably have as much (more?) of
I am using Visual Studio 2010 Professional Edition, and Windows Vista. Firstly, I have
my question has two parts, but they are related. Firstly - I have Contenteditable
Firstly, I do not have the option to use Regular Expressions. It pains me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.