数据插入在ajax请求中失败

时间:2017-07-03 23:47:31

标签: php ajax

here; s我的index.php文件: 你能找到我的数据没有插入数据库的错误吗?我有另一个db.php文件,我在这里没有提到,工作正常

<!DOCTYPE html>
<html>
<head>
    <title>ajax insert</title>

    <style type="text/css">
        body{
            background: #fafcfc ; 
        }
        #wrapper{
            width : 50%;
            height : auto;
            margin : 10px auto;
            background:  gray;
            border: 1px solid #cbcbcb; 
        }
        #comment_input_form{
            width : 50%;
            margin : 100px auto;
            background: #fafcfc ;
            padding : 20px;
        }
        #comment_input_form li{
        list-style-type: none;
        margin :7px; 
        }
        input[type=text],textarea{
            width: 100%;
        }
    </style>
</head>

<body>
     <div id="wrapper">

         <div id="comment_input_form">

            <li> Name: </li>
            <li> <input type="text" id="name"> </li>
            <li> Comment: </li>
            <li> <textarea type="text" id="comment"></textarea> </li>
            <li> <input type="submit" id="save" value="POST"></li>

         </div>

     </div>
</body>

</html>

<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script type="text/javascript">

      $(document).ready(function(){
         $("#save").click(function(){
           var name = $("#name").val();
           var comment = $("#comment").val();
              $.ajax({
                 url : "server.php",
                 type : "POST" ,

                 data : {
                     "done" : 1,
                     "username" : name,
                     "comment" : comment
                 },
                 success:function(data){
                    $("#name").val('');
                    $("#comment").val('');
                 }
              });
         });
      });


</script>

,这是我的server.php文件

<?php

  include ("db.php");


   if(isset($_POST['done'])) {
    $name =mysqli_escape_string($_POST['username']);
    $comment =mysqli_escape_string($_POST['comment']);

    mysqli_query("insert into test3 values ('{$name}', '{$comment}')");
    exit();
   }

?>

0 个答案:

没有答案
相关问题