如何将表单方法转换为ajax

时间:2019-02-07 09:33:01

标签: jquery html ajax

我想将我的chatbox表单操作转换为ajax,以便每当单击发送按钮将其转换为实时chatbox时都不会刷新页面。抱歉,我是编程新手。

<script> ajax code here</script>
       if(isset($_POST['msg'])){
        $msgs=$_POST['msg'];
        $sql='INSERT INTO messages (msg,userid,sendto,username,uniqids,idOfSender) VALUES (?,?,?,?,?,?);';
        $stmt=mysqli_stmt_init($conn);
        if(!mysqli_stmt_prepare($stmt,$sql)){
            echo "error in send.php line 15";
        }else{
            mysqli_stmt_bind_param($stmt,'ssssss',$msgs,$sellerid,$buyerid,$sellerid,$finalId,$buyerId);
            mysqli_stmt_execute($stmt);
            header('Location.. //send.php?success'); 

        }}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>

<body>
<h2><?php echo $rows['Fullname'];?></h2>

这是显示数据的表:

    <table>
    <div id='output' style= 'background-color:white; box-shadow:0px 1px 1 px #000; width:350px;height:250px; margin-bottom:20px; overflow:scroll;'>
<?php 
    $sqli='SELECT * FROM messages WHERE uniqids=?;';
    $sstmt=mysqli_stmt_init($conn);
    if(!mysqli_stmt_prepare($sstmt,$sqli)){
        echo "failed in line 42 send.php";

    }else{
        mysqli_stmt_bind_param($sstmt,'s',$finalId);
        mysqli_stmt_execute($sstmt);
        $result=mysqli_stmt_get_result($sstmt);
        if(mysqli_num_rows($result)>0){
            while($rows=mysqli_fetch_assoc($result)){
                ?>
                <h4><?php echo $rows['sendto'] ?></h4>
                <h5><?php echo $rows['msg'];?></h5> 

                <?php
            }}}}
?>
    </div>

这里有一个“发送”按钮

    <form method='POST' action=''>
    <textarea name='msg'></textarea>
    <br>
    <input type='submit' name='sendbtn' value='SEND'> 
</form>
<?php  
}}}
?>

0 个答案:

没有答案