I'm posting some data to a php file with jquery. The PHP file just saves the data and prints the success of fail message. I would like to get the message generated by php file and embed in a div. I'm unable to figure out how i can return the message from PHP file.
我用jquery将一些数据发布到php文件中。PHP文件只是保存数据并打印失败消息的成功。我想要得到php文件生成的消息并嵌入到div中。我不知道如何从php文件返回消息。
Here is my code:
这是我的代码:
jQuery:
jQuery:
$(document).ready(function(){
$('#submit').click(function() {
$.ajax({
type : 'POST',
url : 'post.php',
data: {
email : $('#email').val(),
url : $('#url').val(),
name : $('#name').val()
},
});
});
});
PHP:
PHP:
<?php
//save data
$message = "saved successfully"
?>
2 个解决方案
#1
8
Try -
试一试,
$(document).ready(function(){
$('#submit').click(function() {
$.ajax({
type : 'POST',
url : 'post.php',
data: {
email : $('#email').val(),
url : $('#url').val(),
name : $('#name').val()
},
success:function (data) {
$("#yourdiv").append(data);
}
});
});
});
This uses the success
callback of the ajax
function to return your data back to the page within the data
parameter. The callback function then appends your data to a div on the page.
这将使用ajax函数的成功回调,将数据返回到数据参数中的页面。然后回调函数将数据附加到页面上的div中。
You'll also need to -
你也需要-。
echo $message
In your php page to give jQuery some data to work with.
在php页面中为jQuery提供一些数据。
#2
1
Did you look at the properties of the ajax method yet?
你看过ajax方法的属性了吗?
From the jQuery site:
从jQuery网站:
$.ajax({
url: 'ajax/test.html',
success: function(data) {
$('.result').html(data);
alert('Load was performed.');
}
});
Of course, your PHP need to echo something (a JSON object preferably) that returns to the script.
当然,PHP需要回显返回到脚本的一些东西(最好是JSON对象)。
#1
8
Try -
试一试,
$(document).ready(function(){
$('#submit').click(function() {
$.ajax({
type : 'POST',
url : 'post.php',
data: {
email : $('#email').val(),
url : $('#url').val(),
name : $('#name').val()
},
success:function (data) {
$("#yourdiv").append(data);
}
});
});
});
This uses the success
callback of the ajax
function to return your data back to the page within the data
parameter. The callback function then appends your data to a div on the page.
这将使用ajax函数的成功回调,将数据返回到数据参数中的页面。然后回调函数将数据附加到页面上的div中。
You'll also need to -
你也需要-。
echo $message
In your php page to give jQuery some data to work with.
在php页面中为jQuery提供一些数据。
#2
1
Did you look at the properties of the ajax method yet?
你看过ajax方法的属性了吗?
From the jQuery site:
从jQuery网站:
$.ajax({
url: 'ajax/test.html',
success: function(data) {
$('.result').html(data);
alert('Load was performed.');
}
});
Of course, your PHP need to echo something (a JSON object preferably) that returns to the script.
当然,PHP需要回显返回到脚本的一些东西(最好是JSON对象)。