I am very new to ajax and jquery, but I came across a code on the web which I am manipulating to suit my needs.
我对ajax和jquery非常陌生,但是我在web上遇到了一个代码,我正在处理它以满足我的需要。
The only problem is that I want to be able to respond to the ajax from PHP.
唯一的问题是我希望能够从PHP响应ajax。
This ajax POSTS to a php page (email.php).
此ajax将发布到php页面(email.php)。
How can I make the email.php reply back if the message is sent or if message-limit is exceeded (I limit the nr of messages sent per each user)?
我怎么写这封邮件?如果消息被发送或消息限制被超过(我限制每个用户发送的消息的nr), php将返回?
In other words, I want ajax to take a 1 or 0 from the php code, and for example:
换句话说,我希望ajax从php代码中提取1或0,例如:
if(response==1){ alert("message sent"); } else { alert("Limit exceeded"); }
Here is the last part of the code: (If you need the full code just let me know)
下面是代码的最后一部分:(如果您需要完整的代码,请告诉我)
var data_string = $('form#ajax_form').serialize();
$.ajax({
type: "POST",
url: "email.php",
data: data_string,
success: function() {
$('form#ajax_form').slideUp('slow').before('');
$('#success').html('<h3>Success</h3>Your email is has been sent.');
}//end success function
}) //end ajax call
return false;
})
Thanks
谢谢
6 个解决方案
#1
2
You must pass an argument to your "success" function.
你必须向你的“成功”函数传递一个参数。
success: function(data)
{
if(data == '1')
{
$('form#ajax_form').slideUp('slow').before('');
$('#success').html('<h3>Success</h3>Your email is has been sent.');
}
}
And in your php file, you should just echo the response you need
在php文件中,应该只响应需要的响应。
if(mail())
{
echo '1';
}
else
{
echo '0';
}
#2
6
The success
function of an $.ajax
call receives a parameter, usually called data
though that's up to you, containing the response, so:
$的成功函数。ajax调用接收一个参数,通常称为data,但这取决于您,其中包含响应,因此:
success: function(data) {
// Use the data
}
(It also receives a couple of other parameters if you want them; more in the docs.)
(如果你需要,它还会接收一些其他参数;更多的文档)。
The data
parameter's type will vary depending on the content type of the response your PHP page sends. If it sends HTML, data
will be a string containing the HTML markup; if your page sends JSON, the data
parameter will be the decoded JSON object; if it's XML, data
will be an XML document instance.
数据参数的类型取决于PHP页面发送的响应的内容类型。如果它发送HTML,数据将是一个包含HTML标记的字符串;如果您的页面发送JSON,数据参数将是解码后的JSON对象;如果是XML,数据就是XML文档实例。
You can use 1
or 0
if you like (if you do, I'd probably set the content type to "text/plain"), so:
如果您愿意,可以使用1或0(如果您愿意,我可能会将内容类型设置为“text/plain”),因此:
success: function(data) {
if (data === "1") {
// success
}
else if (data === "0") {
// failure
}
else {
// App error, expected "0" or "1"
}
}
...but when I'm responding to Ajax requests, nine times out of ten I send JSON back (so I set the Content-Type
header to application/json
), because then if I'm using a library like jQuery that understands JSON, I'll get back a nice orderly object that's easy to work with. I'm not a PHP guy, but I believe you'd set the content type via setContentType
and use json_encode
to encode the data to send back.
…但是,当我响应Ajax请求时,十有八九我会发送JSON回来(因此我将Content-Type header设置为application/ JSON),因为如果我使用像jQuery这样理解JSON的库,我将得到一个很好的有序对象,很容易处理。我不是一个PHP人,但我相信您已经通过setContentType设置了内容类型,并使用json_encode对要发送回来的数据进行编码。
In your case, I'd probably reply with:
就你而言,我可能会回答:
{"success": "true"}
or
或
{"success": "false", "errMessage": "You reached the limit."}
so that the server-side code can dictate what error message I show the user. Then your success
function would look like this:
这样服务器端代码就可以指定我向用户显示的错误消息。那么你的成功函数会是这样的:
success: function(data) {
var msg;
if (typeof data !== "object") {
// Strange, we should have gotten back an object
msg = "Application error";
}
else if (!data.success) {
// `success` is false or missing, grab the error message
// or a fallback if it's missing
msg = data.errMessage || "Request failed, no error given";
}
if (msg) {
// Show the message -- you can use `alert` or whatever
}
}
#3
1
Anything you echo or return in the php file will be sent back to you jquery post. You should check out this page http://api.jquery.com/jQuery.post/ and think about using JSON formatted variables to return so like if you had this in your email script:
您在php文件中回显或返回的任何内容都将被发送回jquery post。您应该查看这个页面http://api.jquery.com/jQuery.post/,并考虑使用JSON格式的变量返回,比如如果您的电子邮件脚本中有这个:
echo '{ "reposonse": "1" }';
echo '{"reposonse": "1"};
This pass a variable called response with a value of 1 back to you jquery script. You could then use an if statement how you described.
这将传递一个名为response的变量,其值为1,返回到jquery脚本。然后可以使用if语句描述。
#4
0
just have email.php echo a 0 or 1, and then grab the data in the success event of the ajax object as follows...
只有电子邮件。php回显0或1,然后在ajax对象的成功事件中获取数据,如下所示……
$.ajax({
url: 'email.php',
success: function(data) {
if (data=="1"){
...
}else{
...
}
}
});
#5
0
what you do is, you let your ajax file (email.php) print a 1 if successful and a 0 if not (or whatever else you want)
您要做的是,让ajax文件(email.php)打印1(如果成功),打印0(如果不成功)
Then, in your success function, you do something like this:
然后,在你的成功函数中,你可以这样做:
function(data) {
函数(数据){
$('form#ajax_form').slideUp('slow').before('');
if(data==1){ alert("message sent"); } else { alert("Limit exceeded"); }
$('#success').html('<h3>Success</h3>Your email is has been sent.');
}
So you capture the response in the data var of the function. If you a bigger variety in your output, you can set you dataType to "json" and have your php file print a json_encoded string so that you can access your different variables in your response via for example data.success etc.
你可以在函数的数据var中获取响应。如果在输出中有更大的变化,可以将数据类型设置为“json”,并让php文件打印一个json_encoded字符串,以便通过例如数据访问响应中的不同变量。成功等。
#6
0
PHP can only return to AJAX calls, by its output. An AJAX call to a PHP page is essentially the same as a browser requesting for the page.
PHP只能通过输出返回到AJAX调用。对PHP页面的AJAX调用本质上与请求该页面的浏览器相同。
If your PHP file was something like,
如果PHP文件是这样的,
<?php
echo "1";
?>
You would receive the "1" in your JavaScript success callback,
您将在JavaScript成功回调中收到“1”,
that is,
也就是说,
success: function(data) {
// here data is "1"
}
As an added note, usually AJAX responses are usually done in JSON format. Therefore, you should format your PHP replies in JSON notation.
作为补充说明,AJAX响应通常采用JSON格式。因此,应该将PHP响应格式化为JSON表示法。
#1
2
You must pass an argument to your "success" function.
你必须向你的“成功”函数传递一个参数。
success: function(data)
{
if(data == '1')
{
$('form#ajax_form').slideUp('slow').before('');
$('#success').html('<h3>Success</h3>Your email is has been sent.');
}
}
And in your php file, you should just echo the response you need
在php文件中,应该只响应需要的响应。
if(mail())
{
echo '1';
}
else
{
echo '0';
}
#2
6
The success
function of an $.ajax
call receives a parameter, usually called data
though that's up to you, containing the response, so:
$的成功函数。ajax调用接收一个参数,通常称为data,但这取决于您,其中包含响应,因此:
success: function(data) {
// Use the data
}
(It also receives a couple of other parameters if you want them; more in the docs.)
(如果你需要,它还会接收一些其他参数;更多的文档)。
The data
parameter's type will vary depending on the content type of the response your PHP page sends. If it sends HTML, data
will be a string containing the HTML markup; if your page sends JSON, the data
parameter will be the decoded JSON object; if it's XML, data
will be an XML document instance.
数据参数的类型取决于PHP页面发送的响应的内容类型。如果它发送HTML,数据将是一个包含HTML标记的字符串;如果您的页面发送JSON,数据参数将是解码后的JSON对象;如果是XML,数据就是XML文档实例。
You can use 1
or 0
if you like (if you do, I'd probably set the content type to "text/plain"), so:
如果您愿意,可以使用1或0(如果您愿意,我可能会将内容类型设置为“text/plain”),因此:
success: function(data) {
if (data === "1") {
// success
}
else if (data === "0") {
// failure
}
else {
// App error, expected "0" or "1"
}
}
...but when I'm responding to Ajax requests, nine times out of ten I send JSON back (so I set the Content-Type
header to application/json
), because then if I'm using a library like jQuery that understands JSON, I'll get back a nice orderly object that's easy to work with. I'm not a PHP guy, but I believe you'd set the content type via setContentType
and use json_encode
to encode the data to send back.
…但是,当我响应Ajax请求时,十有八九我会发送JSON回来(因此我将Content-Type header设置为application/ JSON),因为如果我使用像jQuery这样理解JSON的库,我将得到一个很好的有序对象,很容易处理。我不是一个PHP人,但我相信您已经通过setContentType设置了内容类型,并使用json_encode对要发送回来的数据进行编码。
In your case, I'd probably reply with:
就你而言,我可能会回答:
{"success": "true"}
or
或
{"success": "false", "errMessage": "You reached the limit."}
so that the server-side code can dictate what error message I show the user. Then your success
function would look like this:
这样服务器端代码就可以指定我向用户显示的错误消息。那么你的成功函数会是这样的:
success: function(data) {
var msg;
if (typeof data !== "object") {
// Strange, we should have gotten back an object
msg = "Application error";
}
else if (!data.success) {
// `success` is false or missing, grab the error message
// or a fallback if it's missing
msg = data.errMessage || "Request failed, no error given";
}
if (msg) {
// Show the message -- you can use `alert` or whatever
}
}
#3
1
Anything you echo or return in the php file will be sent back to you jquery post. You should check out this page http://api.jquery.com/jQuery.post/ and think about using JSON formatted variables to return so like if you had this in your email script:
您在php文件中回显或返回的任何内容都将被发送回jquery post。您应该查看这个页面http://api.jquery.com/jQuery.post/,并考虑使用JSON格式的变量返回,比如如果您的电子邮件脚本中有这个:
echo '{ "reposonse": "1" }';
echo '{"reposonse": "1"};
This pass a variable called response with a value of 1 back to you jquery script. You could then use an if statement how you described.
这将传递一个名为response的变量,其值为1,返回到jquery脚本。然后可以使用if语句描述。
#4
0
just have email.php echo a 0 or 1, and then grab the data in the success event of the ajax object as follows...
只有电子邮件。php回显0或1,然后在ajax对象的成功事件中获取数据,如下所示……
$.ajax({
url: 'email.php',
success: function(data) {
if (data=="1"){
...
}else{
...
}
}
});
#5
0
what you do is, you let your ajax file (email.php) print a 1 if successful and a 0 if not (or whatever else you want)
您要做的是,让ajax文件(email.php)打印1(如果成功),打印0(如果不成功)
Then, in your success function, you do something like this:
然后,在你的成功函数中,你可以这样做:
function(data) {
函数(数据){
$('form#ajax_form').slideUp('slow').before('');
if(data==1){ alert("message sent"); } else { alert("Limit exceeded"); }
$('#success').html('<h3>Success</h3>Your email is has been sent.');
}
So you capture the response in the data var of the function. If you a bigger variety in your output, you can set you dataType to "json" and have your php file print a json_encoded string so that you can access your different variables in your response via for example data.success etc.
你可以在函数的数据var中获取响应。如果在输出中有更大的变化,可以将数据类型设置为“json”,并让php文件打印一个json_encoded字符串,以便通过例如数据访问响应中的不同变量。成功等。
#6
0
PHP can only return to AJAX calls, by its output. An AJAX call to a PHP page is essentially the same as a browser requesting for the page.
PHP只能通过输出返回到AJAX调用。对PHP页面的AJAX调用本质上与请求该页面的浏览器相同。
If your PHP file was something like,
如果PHP文件是这样的,
<?php
echo "1";
?>
You would receive the "1" in your JavaScript success callback,
您将在JavaScript成功回调中收到“1”,
that is,
也就是说,
success: function(data) {
// here data is "1"
}
As an added note, usually AJAX responses are usually done in JSON format. Therefore, you should format your PHP replies in JSON notation.
作为补充说明,AJAX响应通常采用JSON格式。因此,应该将PHP响应格式化为JSON表示法。