2
<script type="text/javascript" src="js/aboneol.js"></script>
<h4>haberdar olun</h4>
<div class="news_letter">
<span id="aboneolhata"></span>
<form action="" method="post" onsubmit="return false;" id="abone">
<input type="text" name="email" placeholder="E-posta adresiniz" />
<input type="submit" onclick="$.aboneol();" value="abone ol" />
</form>
</div>
Jquery的一部分:
$.aboneol = function(){
var deger = $("form#abone").serialize();
$.ajax({
url: "aboneol.php",
data: deger,
dataType: "json",
type: "post",
success: function(response){
if(response.error == 0){
$(".news_letter").html(response.message);
}else{
$("#aboneolhata").html(response.message);
}
}
});
}
這裏是PHP的一部分:
<?php
$email = $_POST["email"];
$email = trim($email);
if(empty($email)){
$response["error"] = 1;
$response["message"] = "Boş Bırakamazsınız..";
}else if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$response["error"] = 1;
$response["message"] = "Hatalı E-posta..";
}else{
require "database.php";
$pdo = Database::connect();
$sql = "INSERT INTO aboneler (email) VALUES (:email)";
$q = $pdo->prepare($sql);
$q->execute(array(
':email'=>$email,
));
$response["error"] = 0;
$response["message"] = $email;
}
echo json_encode($response);
?>
阿賈克斯發送電子郵件到PHP,並且http響應代碼是200.但成功方法不起作用。另外我不能看到任何成功的方法json響應。如何解決這個問題呢?
可以給你的ajax方法添加en錯誤回調。並記錄錯誤。 – semirturgay
你解決了這個問題嗎?不要忘記接受答案! –