我一直在努力学习AJAX,因此我可以简单地使用数据库中的新文本动态更新我的网页,因为似乎所有ajax教程都是涉及将数据写入数据库的更复杂的示例< / p>
我正在处理的页面只是一个PHP脚本,需要将注册和ID号发布到它,然后它会从数据库中显示消息(经常更新)。我目前有一个&#34;更新消息&#34;我的页面顶部的按钮发送命令以更新消息,但它需要页面刷新才能工作。
我只想使用ajax动态刷新消息。以下是我迄今为止所写的内容,基于我在Using Jquery Ajax to retrieve data from Mysql找到的内容,但由于我不知道如何传递注册和身份证号码,因此无法正常使用使用ajax的php脚本参数并显示响应。
请注意,sendPushNotification
函数不相关且工作正常(用于发送命令以更新消息)。
readmessages.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Inbox</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
});
function sendPushNotification(id){
var data = $('form#'+id).serialize();
$('form#'+id).unbind('submit');
$.ajax({
url: "send_message.php",
type: 'GET',
data: data,
beforeSend: function() {
},
success: function(data, textStatus, xhr) {
$('.txt_message').val("");
},
error: function(xhr, textStatus, errorThrown) {
}
});
return false;
}
function updateText(registration, rowid) {
$.ajax({ //create an ajax request to readmessages.php
type: "GET",
url: "readmessages.php",
dataType: "html", //expect html to be returned
success: function(response){
$("#responsecontainer").html(response);
}
});
}
</script>
</head>
<body>
<?php
// receive data from HTML readmessages request
$rName=$_POST["registration"]; //POST information required to read information from the database
$rowId=$_POST["rowid"];
require_once 'access.php';
if (!userIsLoggedIn()) {
include 'login.php';
exit();
}
include_once './db_functions.php';
$db = new DB_Functions();
?>
<form id="<?php echo $rowId ?>" name="" method="post" onsubmit="return sendPushNotification('<?php echo $rowId ?>')">
<input type="hidden" name="message" value="readmessages" />
<input type="hidden" name="regId" value="<?php echo $rName ?>"/>
<input type="submit" class="send_btn" value="Update Messages" onclick="return updateText(<?php echo $rName ?>, <?php echo $rowId ?>);"/> //Attempts to call function to update text once button is pressed (not functioning)
<?php
$messagelist = $db->getInbox($rName); //calls the database to retrieve messages
echo nl2br($messagelist); //Displays message list that I want to update
include './logout.php';
?>
</body>
</html>
非常感谢任何帮助!
编辑:更新了包含正确引号的行:
<input type="submit" class="send_btn" value="Update Messages" onclick="return updateText('<?php echo $rName ?>', '<?php echo $rowId ?>')"/>
答案 0 :(得分:4)
要在AJAX
和PHP
之间进行通信,您必须了解两件事:
1)如果您使用PHP's
$_POST
,则AJAX
必须表示发布数据,并且
2)如果您使用的是PHP's
$_GET
,则您的AJAX
也必须表示获取数据。
所以,你做不到:
$.ajax({ //create an ajax request to readmessages.php
type: "GET",//NOTICE THIS GET thingi...
url: "readmessages.php",
dataType: "html", //expect html to be returned
success: function(response){
$("#responsecontainer").html(response);
}
并做:
<?php
// receive data from HTML readmessages request
$rName=$_POST["registration"]; //NOTICE THE $_POST guy.. //POST information required to read information from the database
$rowId=$_POST["rowid"];
您宁可在双方使用'POST'
或'GET'
所以,如果你只想POST
,你的AJAX可能看起来像:
$.post('url_goes_here.php',{myDataXXX:comes_here},function(response_here){
console.log(response_here);
})
和你的PHP
<?php
var_dump($_POST['myDataXXX'])
?>
如果您只想简单GET
,您的AJAX可能看起来像:
$.get('url_goes_here.php',{myDataXXX:comes_here},function(response_here){
console.log(response_here);
})
和您的PHP
<?php
var_dump($_GET['myDataXXX'])
?>
希望有帮助...
答案 1 :(得分:3)
您需要包含数据和ajax请求,
data: {registration: registration, rowid: rowid},
并且还将类型设置为POST,因为在php端你正在检索POST变量。
所以喜欢这个....
function updateText(registration, rowid) {
$.ajax({ //create an ajax request to readmessages.php
type: "POST",
url: "readmessages.php",
dataType: "html", //expect html to be returned
data: {registration: registration, rowid: rowid},
success: function(response){
$("#responsecontainer").html(response);
}