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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:26:52+00:00 2026-06-11T15:26:52+00:00

My Code var json = xmlhttp.responseText; //ajax response from my php file obj =

  • 0

My Code

var json = xmlhttp.responseText; //ajax response from my php file
obj = JSON.parse(json);
alert(obj.result);

And in my php code

 $result = 'Hello';

 echo '{
        "result":"$result",
        "count":3
       }';

The problem is: when I alert obj.result, it shows "$result", instead of showing Hello.
How can I solve this?

  • 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-06-11T15:26:53+00:00Added an answer on June 11, 2026 at 3:26 pm

    The basic problem with your example is that $result is wrapped in single-quotes. So the first solution is to unwrap it, eg:

    $result = 'Hello';
    echo '{
        "result":"'.$result.'",
        "count":3
    }';
    

    But this is still not “good enough”, as it is always possible that $result could contain a " character itself, resulting in, for example, {"result":""","count":3}, which is still invalid json. The solution is to escape the $result before it is inserted into the json.

    This is actually very straightforward, using the json_encode() function:

    $result = 'Hello';
    echo '{
        "result":'.json_encode($result).',
        "count":3
    }';
    

    or, even better, we can have PHP do the entirety of the json encoding itself, by passing in a whole array instead of just $result:

    $result = 'Hello';
    echo json_encode(array(
        'result' => $result,
        'count' => 3
    ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

jQuery Code: $(document).ready(function(){ $.getJSON('dat.js', function(data) { var obj = JSON.parse(data); alert(obj[0].title); }); }); My
See this code: var jsonString = '{id:714341252076979033,type:FUZZY}'; var jsonParsed = JSON.parse(jsonString); console.log(jsonString, jsonParsed); When
Retrieve code from json. C#code:- var collection = getsortcat.Select(x => new { idterm =
Hi I have this code to parse JSON on JQUERY var json_text2 = $.parseJSON('{data:[[1340650436,2.00000],[1340736844,4.00000]],label:Waist
I use json-rpc client from json-rpc.net in my windows phone application. Code: var client
I'm using the following code to retrieve a json string from my webserver: var
Json String: (response) {19:{id:19,name:Product,sku:123,price:59.50,cost:25.00},20:{id:20,name:Test,sku:456,price:50.00,cost:40.00}} JavaScript Code: var json = $.parseJSON(response); var items = new
How to get the values from JSON in javascript following is my code var
How can JSON be used to parse xmlhttp.responseText? I can't seem to get textboxes
I now have a working JSON formatted file from my PHP scripts. The next

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.