this is the code that I have on index.html:
这是我在index.html上的代码:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$.ajax({
type: "POST",
url: 'test.php',
data: "check",
success: function(data){
alert(data);
}
});
</script>
</head>
<body>
<div></div>
</body>
</html>
How do I program test.php to get the "data" that is sent in the AJAX API?
如何编写test.php以获取在AJAX API中发送的“数据”?
6 个解决方案
#1
3
You are asking a very basic question here. You should first go through some Ajax tutorials. Just to help you a little (assuming you are aware of GET and POST methods of sending data), 'data' in data: "check" is different than 'data' in function (data) are different. For clarity, you should name them different as here:
你在这里问一个非常基本的问题。您应该首先阅读一些Ajax教程。只是为了帮助你一点(假设你知道发送数据的GET和POST方法),数据中的“数据”:“检查”与功能(数据)中的“数据”不同。为清楚起见,您应将它们命名为不同,如下所示:
$.ajax({
type: "POST",
url: 'test.php',
data: "check",
success: function(response){
alert(response);
}
});
This makes it clear that one is data that you are sending to the test.php file in POST parameters and other is the response you are getting from the test.php file after it is run. In fact, the data parameter that you POST to test.php has to be a hash like here (I am assuming the key as "type" here:
这清楚地表明,一个是您在POST参数中发送到test.php文件的数据,另一个是您在运行后从test.php文件获取的响应。实际上,你POST到test.php的数据参数必须是像这里的哈希(我在这里假设键为“type”:
$.ajax({
type: "POST",
url: 'test.php',
data: {"type":"check"},
success: function(response){
alert(response);
}
});
There can obviously be more key-val pairs in data.
显然,数据中可能存在更多的键 - 值对。
So, assuming your test.php file is something like this:
所以,假设你的test.php文件是这样的:
if(isset($_POST['type'])){
//Do something
echo "The type you posted is ".$_POST['type'];
}
In this case your alert should read: "The type you posted is check". This will change based on what value you send for 'type' key in AJAX call.
在这种情况下,您的警报应显示为:“您发布的类型是检查”。这将根据您在AJAX调用中为“type”键发送的值而更改。
#2
7
You can try like this
你可以这样试试
$.ajax({
type: "POST",
url: 'test.php',
data: {"data":"check"},
success: function(data){
alert(data);//This will alert Success which is sent as the response to the ajax from the server
}
});
And in test.php
在test.php中
if(isset($_POST['data']) && $_POST['data'] == 'check'){
//$_POST['data'] contain the value that you sent via ajax
//Do something
echo 'Success';
}
You can check this for more
您可以查看更多内容
#3
5
If you want to deals with more datas with jquery ajax. I prefer json data type.
如果你想用jquery ajax处理更多的数据。我更喜欢json数据类型。
Simply Use like this.
$.ajax({
type: "POST",
url: 'test.php',
dataType: 'json',
data: {"data":"check"},
success: function(data){
alert(data.value1);
alert(data.value2);
}
});
In your PHP Code
if(isset($_POST['data']) && $_POST['data'] == 'check'){
//Data 1
$data['value1'] = 'Data 1 Got Successfully';
//Data 2
$data['value2'] = 'Data 2 Got Successfully';
$resonse = json_encode($data);
echo $response;
}
#4
2
Your HTML would be
你的HTML会是
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$.ajax({
type: "POST",
url: 'test.php',
data: {"myvar":"check",},
success: function(data){
alert(data);
}
});
</script>
</head>
<body>
<div></div>
</body>
</html>
Notice that data is an array, so you will have to pass a variable and its value in data. You can pass multiple variables in data, separating them with comma.
请注意,数据是一个数组,因此您必须在数据中传递变量及其值。您可以在数据中传递多个变量,用逗号分隔它们。
And to pick the data sent by your ajax, in test.php:
并在test.php中选择ajax发送的数据:
<?php
if(isset($_POST['myvar']))
{
$myVariable = $_POST['myvar'];
echo $myVariable; //This would output the string passed in ajax, check
}
?>
$_POST is dependent on the type used in the AJAX call. If type is GET, in php it would be $_GET. A simple thing instead would be $_REQUEST, which irrespective of whether the AJAX call is of type GET or POST.
$ _POST取决于AJAX调用中使用的类型。如果type是GET,那么在PHP中它将是$ _GET。一个简单的事情是$ _REQUEST,无论AJAX调用是GET类型还是POST类型。
#5
2
$.ajax({//create an ajax request to load_page.php
type: "POST",
url: "test.php",
data:{"data":"check"},
success: function(data) {
if (data) {
alert(data);
}
else {
alert('Successfully not posted.');
}
}
});
In test.php
在test.php中
<?php
if(isset($_POST['data'])){
echo 'successful';
}
?>
#6
1
Output of the PHP file will sent to your AJAX succes function.
PHP文件的输出将发送到您的AJAX succes函数。
#1
3
You are asking a very basic question here. You should first go through some Ajax tutorials. Just to help you a little (assuming you are aware of GET and POST methods of sending data), 'data' in data: "check" is different than 'data' in function (data) are different. For clarity, you should name them different as here:
你在这里问一个非常基本的问题。您应该首先阅读一些Ajax教程。只是为了帮助你一点(假设你知道发送数据的GET和POST方法),数据中的“数据”:“检查”与功能(数据)中的“数据”不同。为清楚起见,您应将它们命名为不同,如下所示:
$.ajax({
type: "POST",
url: 'test.php',
data: "check",
success: function(response){
alert(response);
}
});
This makes it clear that one is data that you are sending to the test.php file in POST parameters and other is the response you are getting from the test.php file after it is run. In fact, the data parameter that you POST to test.php has to be a hash like here (I am assuming the key as "type" here:
这清楚地表明,一个是您在POST参数中发送到test.php文件的数据,另一个是您在运行后从test.php文件获取的响应。实际上,你POST到test.php的数据参数必须是像这里的哈希(我在这里假设键为“type”:
$.ajax({
type: "POST",
url: 'test.php',
data: {"type":"check"},
success: function(response){
alert(response);
}
});
There can obviously be more key-val pairs in data.
显然,数据中可能存在更多的键 - 值对。
So, assuming your test.php file is something like this:
所以,假设你的test.php文件是这样的:
if(isset($_POST['type'])){
//Do something
echo "The type you posted is ".$_POST['type'];
}
In this case your alert should read: "The type you posted is check". This will change based on what value you send for 'type' key in AJAX call.
在这种情况下,您的警报应显示为:“您发布的类型是检查”。这将根据您在AJAX调用中为“type”键发送的值而更改。
#2
7
You can try like this
你可以这样试试
$.ajax({
type: "POST",
url: 'test.php',
data: {"data":"check"},
success: function(data){
alert(data);//This will alert Success which is sent as the response to the ajax from the server
}
});
And in test.php
在test.php中
if(isset($_POST['data']) && $_POST['data'] == 'check'){
//$_POST['data'] contain the value that you sent via ajax
//Do something
echo 'Success';
}
You can check this for more
您可以查看更多内容
#3
5
If you want to deals with more datas with jquery ajax. I prefer json data type.
如果你想用jquery ajax处理更多的数据。我更喜欢json数据类型。
Simply Use like this.
$.ajax({
type: "POST",
url: 'test.php',
dataType: 'json',
data: {"data":"check"},
success: function(data){
alert(data.value1);
alert(data.value2);
}
});
In your PHP Code
if(isset($_POST['data']) && $_POST['data'] == 'check'){
//Data 1
$data['value1'] = 'Data 1 Got Successfully';
//Data 2
$data['value2'] = 'Data 2 Got Successfully';
$resonse = json_encode($data);
echo $response;
}
#4
2
Your HTML would be
你的HTML会是
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$.ajax({
type: "POST",
url: 'test.php',
data: {"myvar":"check",},
success: function(data){
alert(data);
}
});
</script>
</head>
<body>
<div></div>
</body>
</html>
Notice that data is an array, so you will have to pass a variable and its value in data. You can pass multiple variables in data, separating them with comma.
请注意,数据是一个数组,因此您必须在数据中传递变量及其值。您可以在数据中传递多个变量,用逗号分隔它们。
And to pick the data sent by your ajax, in test.php:
并在test.php中选择ajax发送的数据:
<?php
if(isset($_POST['myvar']))
{
$myVariable = $_POST['myvar'];
echo $myVariable; //This would output the string passed in ajax, check
}
?>
$_POST is dependent on the type used in the AJAX call. If type is GET, in php it would be $_GET. A simple thing instead would be $_REQUEST, which irrespective of whether the AJAX call is of type GET or POST.
$ _POST取决于AJAX调用中使用的类型。如果type是GET,那么在PHP中它将是$ _GET。一个简单的事情是$ _REQUEST,无论AJAX调用是GET类型还是POST类型。
#5
2
$.ajax({//create an ajax request to load_page.php
type: "POST",
url: "test.php",
data:{"data":"check"},
success: function(data) {
if (data) {
alert(data);
}
else {
alert('Successfully not posted.');
}
}
});
In test.php
在test.php中
<?php
if(isset($_POST['data'])){
echo 'successful';
}
?>
#6
1
Output of the PHP file will sent to your AJAX succes function.
PHP文件的输出将发送到您的AJAX succes函数。