Home > front end >  Ajax for PHP data, how to sent the HTML code?
Ajax for PHP data, how to sent the HTML code?

Time:09-30

By the time to learn about ajax, use the jquery methods,
The front is as follows:
 $(" # submit "). Click (function () {
{var y=$(" # year "). Val (); Var m=$(" # month "). Val ();
//$. Get (" ajax_data. PHP, "
$.post (" ajax_data. PHP, "
//{year: "2019", the month: Math. The random ()},
{year: y, the month: m, mh: Math. The random ()},
The function (data) {
$(" # right_below "). The HTML (data);
//alert (data);
Var STR=JSON. Stringify (data);
Var str1=STR. Replace (/\ \ \/=/g, "");
Alert (str1);
});
}
return false;
}
);

To the background and the value of the year and month, and then read the background SQL after winning value, after json_encode echo come out, give to the front-end js: ( a little HTML enthusiast in PHP, behind js when alert, it gives out the )
 
<? PHP
Session_cache_limiter (' private, must revalidate - ');
session_start();
?>

<meta charset="utf-8" & gt;

<? PHP

$year=$_POST [' year '];
The month=$_REQUEST [' month '];

//$cur_year=(date (" Y "));//pick up now in
//$cur_mon=(date (the "m"));//take now month

$cur_year=$year;//pick up now in
$cur_mon=$month;
$sal=new Sale ($conn, $params, $options);
$sal_qty=$sal - & gt; Get_sal_qty ($cur_year, $cur_mon, ', 1);
//echo $cur_year. $cur_mon;
Echo $year, $month;
//print_r ($sal_qty);
$a=json_encode ($sal_qty);
Echo $a;
Return $a;


The diagram below:
Is displayed on the web, I again alert window, can have in this alert HTML code,
The same two days before doing small program wx. Request (), get back the value of in addition to the value of the echo, also have the HTML code, what reason is this?
The following figure is the use of jquery will read back the data displayed in the HTML, there are no HTML code,


But the following alert data, a few lines of code in front of the HTML to figure out, what reason is this?


There are small programs, also is such, get the HTML code to come over??

In addition, the server-side PHP or Java end forward multidimensional array data of the database is usually do (send it to json), not taught, self-study is groping, so sometimes often delayed, are familiar to give directions, thank you!

CodePudding user response:

Ajax get PHP file, get is a PHP file to the output of the source code, so as long as there is a PHP & lt; ? PHP?> Label and echo output json data is ok, don't have other HTML tags,

 
<? PHP
Session_cache_limiter (' private, must revalidate - ');
session_start();

$year=$_POST [' year '];
The month=$_REQUEST [' month '];

//$cur_year=(date (" Y "));//pick up now in
//$cur_mon=(date (the "m"));//take now month

$cur_year=$year;//pick up now in
$cur_mon=$month;
$sal=new Sale ($conn, $params, $options);
$sal_qty=$sal - & gt; Get_sal_qty ($cur_year, $cur_mon, ', 1);
//echo $cur_year. $cur_mon;
//print_r ($sal_qty);
$a=json_encode ($sal_qty);
Echo $a;
?>

CodePudding user response:

reference 1/f, the sky wave response:
ajax get PHP file, get is a PHP file to the output of the source code, so as long as there is a PHP & lt; ? PHP?> Label and echo output json data is ok, don't have other HTML tags,

 
<? PHP
Session_cache_limiter (' private, must revalidate - ');
session_start();

$year=$_POST [' year '];
The month=$_REQUEST [' month '];

//$cur_year=(date (" Y "));//pick up now in
//$cur_mon=(date (the "m"));//take now month

$cur_year=$year;//pick up now in
$cur_mon=$month;
$sal=new Sale ($conn, $params, $options);
$sal_qty=$sal - & gt; Get_sal_qty ($cur_year, $cur_mon, ', 1);
//echo $cur_year. $cur_mon;
//print_r ($sal_qty);
$a=json_encode ($sal_qty);
Echo $a;
?>

So that is what it boils down to
The small program reply request, also is same??

And PHP to the front-end data commonly, this is for? Into a json data?
But I json encode, why is [], no {}??

CodePudding user response:

When PHP array attribute name begins with 0 consecutive Numbers or no property name, the default conversion to []
When PHP array attribute name is a string or discontinuous number, default into {}, can also be in json to encode () function of the second parameter set constant JSON_FORCE_OBJECT cast {}
Specific you can look at the PHP official manual
https://www.php.net/manual/zh/function.json-encode.php
  • Related