如何将这个php转换成ajax?
如何将以下php更改为ajax如何将这个php转换成ajax?,php,ajax,Php,Ajax,如何将以下php更改为ajax <?php $selectMSGs = 'SELECT * FROM group_messages WHERE group_id="'.$_GET["id"].'" ORDER BY msg_id DESC'; $selectMSGs2 = $connect->query($selectMSGs); while ($rowMSGs = $selectMSGs2->fetch_assoc()) { ech
<?php
$selectMSGs = 'SELECT * FROM group_messages WHERE group_id="'.$_GET["id"].'" ORDER BY msg_id DESC';
$selectMSGs2 = $connect->query($selectMSGs);
while ($rowMSGs = $selectMSGs2->fetch_assoc()) {
echo "<div class='lead MessageOfPerson'>";
echo "<div class='NameOfPerson'>";
echo "<p>";
echo $rowMSGs['msg_sender'];
echo "</div>";
echo "<div class='MessageText'>";
echo nl2br($rowMSGs['group_msg']);
echo "</p>";
echo "</div>";
echo "</div>";
echo "<hr>";
}
?>
我已经有一些其他的ajax课程,而不是我自己的,已经在这个页面上运行了,有可能将这个php与我当前的ajax和php文件结合起来吗?这些文件如下所示:
阿贾克斯
php
好的,在做了大约20个小时之后,我终于发现了如何在没有帮助的情况下完成它:!我能够合并新旧ajax和php 新的ajax成为
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
function updateChat(){
$.ajax({
//TODO set localhost to the strato livechat file
url: "http://localhost:8080/LiveChat/USB/code/php/groups/liveChat.php",
type: "get",
data: "msg_id="+$('#msg_id').val(),
dataType: "json",
success: function(response, status, http){
$.each(response, function(index, item){
$("#ChatBox").html("<div class='lead MessageOfPerson'> <div class='NameOfPerson'> <p>" + item.msg_sender + "</p> </div> </div> <div class='MessageText'> <p>" + item.group_msg + "</p> </div> <hr/>" + $('#ChatBox').html());
$('#msg_id').val(item.msg_id);
});
},
error: function(http, status, error){
alert( 'Error updating chat, ' + error);
}
});
}
//auto update chat
setInterval( updateChat, 2000 );
</script>
php变成了:
<?php
// Lets fetch the value of msg_id
$data = $_REQUEST;
$msg_id = $data['msg_id'];
// Connect to MySQL Server TODO change to strato
$con = mysqli_connect( "localhost" , "root" , "" , "db2723249" );
// If msg_sender and group_msg is available then
// add it in table chats
if(
isset( $data['msg_sender'] ) &&
isset( $data['group_msg'] )
) {
$insert = "
INSERT INTO group_messages (group_id, group_msg, msg_sender)
VALUES( '".$data['msg_sender']."' , '".$data['group_msg']."' )
";
$insert_result = mysqli_query( $con , $insert );
}
$select = "SELECT * FROM group_messages WHERE msg_id > '".$msg_id."' ";
$result = mysqli_query( $con , $select );
$arr = array();
$row_count = mysqli_num_rows( $result );
if( $row_count > 0 ) {
while( $row = mysqli_fetch_array( $result ) ) {
array_push( $arr , $row );
}
}
// Close the MySQL Connection
mysqli_close( $con );
// Return the response as JSON
echo json_encode( $arr );
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
function updateChat(){
$.ajax({
//TODO set localhost to the strato livechat file
url: "http://localhost:8080/LiveChat/USB/code/php/groups/liveChat.php",
type: "get",
data: "msg_id="+$('#msg_id').val(),
dataType: "json",
success: function(response, status, http){
$.each(response, function(index, item){
$("#ChatBox").html("<div class='lead MessageOfPerson'> <div class='NameOfPerson'> <p>" + item.msg_sender + "</p> </div> </div> <div class='MessageText'> <p>" + item.group_msg + "</p> </div> <hr/>" + $('#ChatBox').html());
$('#msg_id').val(item.msg_id);
});
},
error: function(http, status, error){
alert( 'Error updating chat, ' + error);
}
});
}
//auto update chat
setInterval( updateChat, 2000 );
</script>
<?php
// Lets fetch the value of msg_id
$data = $_REQUEST;
$msg_id = $data['msg_id'];
// Connect to MySQL Server TODO change to strato
$con = mysqli_connect( "localhost" , "root" , "" , "db2723249" );
// If msg_sender and group_msg is available then
// add it in table chats
if(
isset( $data['msg_sender'] ) &&
isset( $data['group_msg'] )
) {
$insert = "
INSERT INTO group_messages (group_id, group_msg, msg_sender)
VALUES( '".$data['msg_sender']."' , '".$data['group_msg']."' )
";
$insert_result = mysqli_query( $con , $insert );
}
$select = "SELECT * FROM group_messages WHERE msg_id > '".$msg_id."' ";
$result = mysqli_query( $con , $select );
$arr = array();
$row_count = mysqli_num_rows( $result );
if( $row_count > 0 ) {
while( $row = mysqli_fetch_array( $result ) ) {
array_push( $arr , $row );
}
}
// Close the MySQL Connection
mysqli_close( $con );
// Return the response as JSON
echo json_encode( $arr );
?>