PHP a little doubt about Ajax

Source: Internet
Author: User
We all know there are three kinds of data from Ajax, text, Jason,xml
I now want him to pass back the data type four Jason
I define this in the controller:
$a = 10;
$Jason = ' {' number ': ' $a '} '
Why can't you write like that? He returned a value of $ A instead of 10?
Some say you put 10 in it, I understand, but what if you take the value out of the database?
For example
$a = $row [' content '];
echo $a;//This can be taken out of the database value
$Jason = ' {' content ': ' $a '} ';//this is not possible;
If I write $jason= ' {' content ': ' $row [' content '] '} ';//is to replace $ A directly with $row[' content ';
He will prompt for grammatical errors;
Can't the value in Jason be replaced with a variable?
If it can't be replaced, then how to remove the value from the database, the above $jason format how to change ah?


Reply to discussion (solution)

JSON is not returned in the form of an array, can you use variables?

$Jason = ' {' content ': ' $a '} ';
/This is not possible because the variables in single quotes are not replaced by values.
Can write
$Jason = "{\" content\ ": \" $a \ "}";
Or
$Jason = sprintf (' {' content ': '%s '} ', $a);

In fact, PHP has provided the Json_encode function, you can easily convert a variety of variables into a JSON format string


;

Try $jason= ' {' number ': '. $a. '} '

Single quotation marks do not resolve variables and most escapes, only double quotation marks can be

Directly using PHP's Json_encode () to leverage the

Directly using PHP's Json_encode () to leverage the


There are 2 problems, the page encoding GBK, the results shown with Json_encode () is empty? What's the deal?
The 2nd one uses the Json_encode () Chinese character not to display normally, to JS's display is the binary number

Directly using PHP's Json_encode () to leverage the


There are 2 problems, the page encoding GBK, the results shown with Json_encode () is empty? What's the deal?
The 2nd one uses the Json_encode () Chinese character not to display normally, to JS's display is the binary number

The JSON data for AJAX is dynamic I data, so that the database can return utf-8 data.
Any encoded data to the browser has been converted to Unicode encoding, Json_encode first done, and nothing bad

You can use PHP's Json_encode. If not UTF8 data, you can use Iconv (' GBK ', ' UTF ', $content); Turn around and then Json_encode.

$a =10; $arr = Array (' number ' = $a); $Jason = Json_encode ($arr); {"Number": 10}echo $Jason;


$a =10; $arr = Array (' content ' = = $a); $Jason = Json_encode ($arr); {"Content": 10}echo $Jason;
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.