2012-01-29 87 views
1

JS: 的JS爲email領域的客戶端驗證爲什麼這不提交數據庫的價值?

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"> </script> 
<script type="text/javascript" src="src/js/validate.js"> </script> 
    <script type="text/javascript"> 
    $.validator.setDefaults({ 
     submitHandler: function() { alert("submitted!"); } 
    }); 

    $().ready(function() { 
     // validate alphaRegister form on keyup and submit 
     $("#alphaRegister").validate({ 
      rules: { 
       email: { 
        required: true, 
        email: true 
       } 
      }, 
      messages: { 
       email: "*" 
      } 
     }); 
    }); 
    </script> 

HTML 形式

<form id="alphaRegister" action="src/php/newSubscriber.php" method="post"> 
     <input type="email" name="email" id="cemail" value="" class="required" /> 
     <div style="float:right; margin:0 5px 2px 0;"><input type="submit" id="submit" name="submit" value="" /></div> 
    </form> 

當我進入一個[email protected],它只是說submitted!!

P HP文件 文件處理HTML

<?php 
$host="localhost"; // Host name 
$username="myuser"; // Mysql username  
    $username2="root"; #localhost 
$password="mypass"; // Mysql password 
    $password2=""; #localhost 
$db_name="mydbname"; // Database name 
$tbl_name="mydbtblname"; // Table name 

// Connect to server and select databse. 
mysql_connect("$host", "$username2", "$password2")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot connect to database"); 

/* Obliterate bad input */ 
$goodEmail = mysql_real_escape_string($_POST['email']); 

$sql= "INSERT INTO alphaSubscribers (alphaSEmAddr) VALUES('$goodEmail')"; 

$result = mysql_query($sql); 
if (!$result) { 
    die('Invalid Email, please retry.'); 
} 
else { 
    header('Location: /index.php') ; 
    echo "<meta HTTP-EQUIV='REFRESH' content='0; url=/index.php'>"; 
} 

?> 

任何的輸入看看有什麼林做錯了什麼?

+0

你得到什麼錯誤?在這裏粘貼 – 2012-01-29 21:07:44

+0

添加後,它不會提交到數據庫.. – CodeTalk 2012-01-29 21:43:55

回答

3

您正在通過GET發送表單,但測試了POST參數。你的表單標籤更改爲

<form id="alphaRegister" action="src/php/newSubscriber.php" method="post"> 

而且刪除javascript submitHandler,否則表單可能無法得到所有提交:

$.validator.setDefaults({ 
    submitHandler: function() { alert("submitted!"); } // <= remove that line 
}); 
+0

請參閱修改。 – CodeTalk 2012-01-29 21:41:37

+0

添加後,它不提交到數據庫.. – CodeTalk 2012-01-29 21:43:42

+0

刪除提交處理程序,看到更新的答案 – konsolenfreddy 2012-01-29 21:48:44

0

您的形式使用GET方法,而後端PHP代碼正在處理它作爲$_POST

你的HTML方法應該是method="POST">

0

jquery docs:使用submitHandler來實現你自己的表單提交,例如。通過Ajax。

您提交處理程序驗證程序可以防止它被以常規方式提交。

中刪除此代碼,然後再試一次:

$.validator.setDefaults({ 
    submitHandler: function() { alert("submitted!"); } 
}); 
相關問題