Ver código fonte

Merge branch 'master' of ssh://104.236.132.72:20202/shorturl-miles

miles 10 anos atrás
pai
commit
f84076bd9f
2 arquivos alterados com 67 adições e 7 exclusões
  1. 55 3
      index.php
  2. 12 4
      u2s.php

+ 55 - 3
index.php

@@ -3,11 +3,63 @@
 <head>
 <meta charset="UTF-8">
 <title>Input Your website</title>
+<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
+	<style>
+ .css-input { margin-bottom: 0; text-shadow:0px 0px 0px rgba(42,42,42,.75); text-align:left; border-radius:8px; border-width:3px; border-style:solid; background-color:#ffffff; border-color:#419fd1; box-shadow: -7px 0px 10px 0px rgba(7,76,214,.0); font-weight:normal; font-family:sans-serif; font-size:16px;width:500px  } 
+		 .css-input:focus { outline:none; } 
+.btn {
+  display: inline-block;
+  padding: 6px 12px;
+  margin-bottom: 0;
+  font-size: 14px;
+  font-weight: 400;
+  line-height: 1.42857143;
+  text-align: center;
+  white-space: nowrap;
+  vertical-align: middle;
+  -ms-touch-action: manipulation;
+  touch-action: manipulation;
+  cursor: pointer;
+  -webkit-user-select: none;
+  -moz-user-select: none;
+  -ms-user-select: none;
+  user-select: none;
+  background-image: none;
+  border: 1px solid transparent;
+  border-radius: 4px;
+  color: #fff;
+  background-color: #337ab7;
+  border-color: #2e6da4;
+  margin-left: 10px;
+}
+</style>
+ <script type="text/javascript">
+        $(document).ready(function () {
+            $("#button").click(function () {
+              if($("#url").val() == '') return false;
+                $.ajax({
+                    //cache: true,
+                    type: "POST",
+                    url: 'u2s.php',//提交的URL
+                    data: $('#shorturl').serialize(), // 要提交的表单,必须使用name属性
+                    //async: false,                    
+                    success: function (data) {
+                        $("#result").html(data);//输出提交的表表单
+                    },
+                    error: function (request) {
+                        alert("Connection error");
+                    }
+                });
+            return false;
+            });
+        });        
+    </script>
 </head>
 <body>
-<form action="u2s.php"  method="POST">
-	<input type="text" name="url" />
-	<input type="submit" />
+<h2>请输入需要转换的网址</h2>
+<form id="shorturl" action method="POST">
+<input type="text" id="url" name="url" class="css-input" ><button class="btn" id="button">提交</button>
 </form>
+<div id="result"></div>
 </body>
 </html>

+ 12 - 4
u2s.php

@@ -16,13 +16,21 @@ $shortUrl = new ShortUrl($pdo);
 try {
     $code = $shortUrl->urlToShortCode($_POST["url"]);
     $http = isset($_SERVER['HTTPS'])?'https':'http';
-    printf('<p><strong>Short URL:</strong> <a href="%s/%2$s">%2$s</a></p>',$http.'://'.$_SERVER['HTTP_HOST'],
-        SHORTURL_PREFIX . $code);
+//     printf('<p><strong>Short URL:</strong> <a href="%s/%2$s">%2$s</a></p>',$http.'://'.$_SERVER['HTTP_HOST'], SHORTURL_PREFIX . $code);
+    $arr = array(
+    		'status' => 1,
+    		'url' => $http.'://'.$_SERVER['HTTP_HOST']."/".$code,
+    );
+    echo json_encode($arr);
     exit;
 }
 catch (Exception $e) {
     // log exception and then redirect to error page.
-//     header("Location: /error");
-    header("Location: index.php");
+//     header("Location: index.php");
+    $arr = array(
+    		'status' => 0,
+    		'msg' => "地址有误!",
+    );
+    echo json_encode($arr);
     exit;
 }