我是php初學者。我正在使用ajax調用提交表單。因此,在提交表單用戶留在同一頁面之後,不要從ajax調用去reserve_seat.php。PHP顯示ajax提交錯誤消息
<form action="reserve_seat.php" method="POST">
First name:<br>
<input type="text" name="name" value="Mickey">
<br>
Email:<br>
<input type="text" name="email" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
<script>
$(function() {
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'reserve_seat.php',
data: $('form').serialize(),
success: function() {
alert('form was submitted');
}
});
});
});
</script>
現在提交表單後,我做了一些驗證電子郵件,如果電子郵件驗證失敗,我想說明的形式上面的錯誤消息。可能會插入顯示該電子郵件無效的div。我怎樣才能做到這一點?下面
我的驗證碼if(!empty($_POST['name']) && !empty($_POST['email'])){
$name = $_POST['name'];
$email = $_POST['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$insert = 'INSERT into reservation(name,email) VALUES ("'.$name.'", "'.$email.'")';
mysql_query($insert);
} else {
//need help here to show error message
}
}
根據成功或失敗,您的ajax調用reserve_seat.php返回什麼?你需要先回到那裏,然後在你的成功回調中,你可以執行一個動作,顯示帶有錯誤信息的div等。 – pmahomme 2014-11-21 22:24:39
請使用預準備語句,你的命令正在遭受sql注入:http:// en.wikipedia.org/wiki/SQL_injection。這意味着某人可以在你的sql server中執行任意命令。 – 2014-11-21 22:27:03