huwhois 3 vuotta sitten
vanhempi
commit
7c1fa00fdc

+ 7 - 10
app/index/controller/Index.php

@@ -63,7 +63,7 @@ class Index extends Base
 
     public function guestBook()
     {
-        $list = GuestBook::paginate();
+        $list = GuestBook::order('id desc')->paginate();
         View::assign('list', $list);
 
         return View::fetch();
@@ -72,16 +72,13 @@ class Index extends Base
     public function saveGuestBook()
     {
         $param = $this->request->param('','',['strip_tags','htmlspecialchars']);
-
+    
         if (empty($param['name']) or empty($param['contact'])) {
-            View::assign('data', ['msg'=>"请填写必填字段(姓名,电子邮件)", 'code'=>1]);
-            return View::fetch('error');
+            return json(['msg'=>"请填写必填字段(姓名,电子邮件)", 'code'=>1]);
         }
 
         if (empty($param['content'])) {
-            View::assign('data', ['msg'=>"请留下内容", 'code'=>1]);
-
-            return View::fetch('error');
+            return json(['msg'=>"请留下内容", 'code'=>1]);
         }
 
         try {
@@ -93,13 +90,13 @@ class Index extends Base
                 'url' => $param['url'],
                 'time' => time(),
             ]);
+            $bgu->datetime = date("Y-m-d", $bgu->time);
+            return json(['msg'=>"保存成功", 'code'=>0, 'data'=>$bgu]);
         } catch (\Exception $e) {
             $msg = $e->getMessage();
 
-            View::assign('data', ['msg'=>$msg, 'code'=>1]);
-            return View::fetch('error');
+            return json(['msg'=>$msg, 'code'=>1]);
         }
 
-        return redirect((string)url('index/guestbook'), 302);
     }
 }

+ 1 - 0
public/static/index/css/index.css

@@ -153,6 +153,7 @@ button.submit {
     -webkit-appearance: none;
     -webkit-transition: background 0.2s;
     transition: background 0.2s;
+    cursor: pointer
 } 
 
 /*read*/

+ 20 - 5
view/index/index/guest_book.html

@@ -125,7 +125,7 @@
       </p>
       <p class="comment-form-author">
         <label for="author">Name*</label>
-        <input id="Name" name="Name" type="text" placeholder="Jane Doe" value="" size="26">
+        <input id="name" name="name" type="text" placeholder="Jane Doe" value="" size="26">
       </p>
       <p class="comment-form-email">
         <label for="contact">contact*</label>
@@ -141,6 +141,9 @@
         <input type="hidden" name="comment_parent" id="comment_parent" value="0"> -->
       </p>
     </form>
+    <div>
+      <p class="msg" style="display: none;color: orangered;">提交成功</p>
+    </div>
   </div><!-- #respond end-->
   <div class="blank"></div>
   <div class="guestbook">
@@ -158,16 +161,28 @@
 
 <script>
   $("#submit").click(function () {
-    $.post('/index/save_guest_book', {
+    $.post('/index/save_guest_book',  {
       'name': $("#name").val(),
       'content': $("#content").val(),
       'contact': $("#contact").val(),
       'url': $("#url").val(),
     }, function (res) {
-      if (res.code == 1) {
-        console.log(res);
+      console.log(res);
+      if (res.code == 0) {
+        $(".msg").show();
+        var html = '<div class="bloglist"><div><h2>'+res.data.name+'<span>'+res.data.datetime+'</span></h2>';
+          html += '</div><p>'+res.data.content+'</p></div>';
+        $(".guestbook").prepend(html);
+        setTimeout(()=>{
+          $(".msg").hide();
+        }, 1000);
       } else {
-        console.log(res);
+        $(".msg").html(res.msg);
+        $(".msg").show();
+        setTimeout(()=>{
+          $(".msg").hide();
+        }, 1000);
+        $(".msg").html('提交成功');
       }
     }, 'json');
   })