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 6746773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:21:45+00:00 2026-05-26T12:21:45+00:00

I am preparing and sending a JSON string from my PHP file to my

  • 0

I am preparing and sending a JSON string from my PHP file to my Javascript function like this:

$json = array();
$json['slice'] = false;
$json['G500'] = false;
$json['KG1'] = false;   

$encoded = json_encode($json);
die($encoded);

However, in my JS function, if I do this, it is unable to decode the JSON object:

var d = req.responseText;                                           
var jsonObject = eval(d);

The only way, I can get it to eval the JSON object is by adding parentheses manually

jsonObject = eval("(" + d + ")");

I have the same problem going in reverse as well. Sending a JSON object to PHP and trying to decode it there fails. I believe I would need to remove the parentheses in my PHP script before attempting to decode.

Why is this happening? Is there something I can do to work around this incompatibility?

EDIT:

PHP to JS is now working if I use JSON.parse. I’m still having trouble the other way around.

This is how I’m sending the data to the PHP:

var JSONstring = 
{
    "Product": document.getElementById('item').value, 
    "Size": document.getElementById('size').value,
    "Quantity": document.getElementById('quantity').value                                                   
};

url = "maintainOrder.php?json=" + JSON.stringify(JSONstring);

req.open("GET", url, true);

However, the PHP script is unable to decode it.

$newItem = json_decode($_GET['json']);

array_push($_SESSION['order'],$newItem);
  • 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-26T12:21:45+00:00Added an answer on May 26, 2026 at 12:21 pm

    For the php to javascript issue refer to the Tomalak Geret’kal answer.

    For the javascript to php maybe I have the solution:

    If you want an associative array in php then you have to pass assoc parameter as true into json_decode (default to false)

    Example:

    $array = json_decode($jsonString, true);

    I was bitten a couple of times by this: by default json_decode try to create an object if it receive a javascript object (it make perfect sense if you think of) and you have to force it to render an associative array if you need this behaviour

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

Sidebar

Related Questions

I'm preparing a string that will be eval 'ed. The string will contain a
Preparing data in json format on backend and populating data to templates via ajax
I am preparing for my interview tomorrow -- I need the answer to this
Problem: When I do something like this: for (int i = 0; i <
Preparing for exam and got stuck at this question: Allowed operators are <<,+,& no
When sending a string of comma separated ids, as a varchar, to a MySQL
I am doing a very simple select from a table like SELECT column FROM
I'm preparing to do an .NET exam and I came across this question. I
I'm preparing to SCJP and looks like I don't understand class cast principles. class
I am preparing a custom action that copies a file to a specific location.

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.