2016-07-19 34 views
1

我試圖將表單動作「轉換」爲ajax調用。 我的形式:ajax調用並驗證php中的電子郵件

<!-- <form method="POST" onSubmit="return doSubmitLogic()" action="action.scripts.php" > 
<input type="hidden" name="actiune" value="login" /> 
<div> 
    <label> Email </label> 
    <input type="email" name = "email" id="email" /><span id="emailErr"></span > 
</div> 
<div> 
    <label> Password </label> 
    <input type="password" name = "password" id="password" /> <span id="passErr"></span > 
</div> 

<div> 
    <input id ="submitBtn" type="submit" name="button" value="Send"/> 
</div> 

而我試圖用我的AJAX做:

$(document).ready(function(){ 
$("#submitBtn").click(function(e){ 

    e.preventDefault(); 

    $.ajax({ 
     type: "POST", 
     url: "functions.php", 
     data: { 
       Email: $("#email").val(), 
       Password: $("#password").val(), 
       }, 

     success: function(result){ 
      alert(result); 
     }, 
     error: function (error){ 
      alert("Error"); 
     } 

    }); 
}); 
}); 

也是我的一段代碼 「的functions.php」 是由組成許多檢查行動是否具有特定的價值,如果是這樣做的話。所以:

if ($_POST['actiune']==="login") { 
    $email = $_POST['email']; 
    $password = $_POST['password']; 
    $encript_pass = md5($password); 

    $query = "select * from user where email='$email' and password='$encript_pass'"; 
    $result = mysql_query($query) or die ("Error in query: $query " . mysql_error()); 
    $row = mysql_fetch_array($result); 



    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 
     echo("$email is a valid email address"); 
    } else { 
     echo("$email is not a valid email address"); 
    } 
} 

當我運行它時,它給我一個警告說錯誤。任何syggestions?

+0

你的第一個也是最大的錯誤是,你把未經過濾的郵件發送到你的sql語句!這樣做非常不安全!你的錯誤說了什麼? – eisbehr

+0

沒什麼。它說只是錯誤。我認爲它來自ajax的錯誤 – johnwilliam25

+0

***你真的不應該使用[MD5密碼哈希](http://security.stackexchange.com/questions/19906/is-md5-considered-insecure)** *並且您確實應該使用PHP的[內置函數](http://jayblanchard.net/proper_password_hashing_with_PHP.html)來處理密碼安全性。在散列之前,請確保你[不要越過密碼](http://stackoverflow.com/q/36628418/1011527)或使用其他任何清理機制。這樣做會改變密碼並導致不必要的附加編碼。 –

回答

1

你需要通過actiune與您的數據了。

data: { 
     email: $("#email").val(), 
     password: $("#password").val(), 
     actiune: 'login' 
}, 

我也刪除從外地轉換爲大寫你通過,因爲你需要在你的PHP不PasswordEmail

+0

以及如何做到這一點? – johnwilliam25

+0

給你的元素一個id,並將其值作爲另一個參數在你的「數據」字典中的ajax調用 – Trickzter

+0

剛剛編輯我的答案 – Vuldo

1

passwordemail只是這個替換您的代碼,請注意電子郵件&下的「actiune」密碼

$(document).ready(function(){ 
$("#submitBtn").click(function(e){ 

    e.preventDefault(); 

    $.ajax({ 
     type: "POST", 
     url: "functions.php", 
     data: { 
       Email: $("#email").val(), 
       Password: $("#password").val(), 
       actiune: $('input[name="actiune"]').val() 
       }, 

     success: function(result){ 
      alert(result); 
     }, 
     error: function (error){ 
      alert("Error"); 
     } 

    }); 
}); 
});