dourong4031 2015-12-13 20:38
浏览 303

在表单提交后显示成功消息

I'm trying to understand jquery and particularly inserting and displaying data in a mysql table using ajax.

I have been experimenting with this code which inserts and displays records from a mysql database. I'm now trying to get it to display the success message in the div with the id "info" whilst also displaying all the records. I seem to only do one but never both. Many thanks.

form.php

     <script   src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
  </script>
  <script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>

 <script type="text/javascript">
           $(document).ready(function(){

                function showComment(){
                  $.ajax({
                    type:"post",
                    url:"process_ajax.php",
                    data:"action=showcomment",
                    success:function(data){
                         $("#comment").html(data);

                    }
                  });
                }
                showComment();

                $("#button").click(function(){

                      var username=$("#username").val();
                      var review=$("#review").val();

                      $.ajax({
                          type:"post",
                 url:"process_ajax.php",
                                 data:"username="+username+"&review="+review+"&action=addcomment",
                   success:function(data){
                       $("#info").html(data);
                         }

                      });

                });
           });
   </script>
  </head>

   <body>
    <form>
     Username : <input type="text" name="username" id="username"/>
           </br>
           Review : <input type="text" name="review" id="review" />
           </br>
           <input type="button" value="Send Comment" id="button">
            </form>
         <div id="info" />
           <ul id="comment"></ul>

   </body>
   </html>

Process_ajax.php

  <?php
 include_once("db_conx.php");

 $action=$_POST["action"];
  if($action=="showcomment"){
  $show="Select * from user_reviews ORDER by date desc";
  $result = $db_conx->query($show);
 while($row=mysqli_fetch_array($result)){
    echo "<li><b>$row[username]</b> : $row[review]</li>";
    }
    }
   else if($action=="addcomment"){
 $username= ($_POST['username']);
 $review= ($_POST['review']);

  $stmt = $db_conx->prepare('INSERT user_reviews SET username = ?,   review=?');
$stmt->bind_param('ss', $username, $review);
$stmt->execute();
if ($stmt->errno) {
 echo "There was an error in saving your review. Please try again." .          $stmt->error;
   }else{
   echo "Your review has been saved";
 }
 }
 ?>
  • 写回答

1条回答 默认 最新

  • dpi10335 2015-12-13 20:51
    关注

    Save the record data as an array and the message as a normal var and create a json:

    $list = '';
    while($row=mysqli_fetch_array($result)){
        $list .= "
    <li><b>$row[username]</b> : $row[review]</li>";
    } 
    
    if ($stmt->errno) {
       $msg = "There was an error in saving your review. Please try again." .             $stmt->error;
    }else{
       $msg = "Your review has been saved";
    }
    
    echo json_encode(["list"=>$list, "msg"=>$msg]);
    

    Remember to put in the dataType into the Ajax:

    type:"post",
    dataType: "json"
    

    Then in your success function, you can access the vars: data.list and data.msg

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题