我有这个PHP代码,它基本上检查电子邮件是否正确,如果是,它将电子邮件保存在* .txt文件中(我将它用于订阅表单),如果它没有弹出一个错误div,但它弹出整个网页,当它在底部时你必须滚动到实际看到错误。
<?php
if ( isset($_POST['submit']) ) {
if( isset($_POST['inputEmail']) ) {
$email = trim($_POST['inputEmail']);
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<div class='wrong'>This e-mail is invalid.</div>";
}
else {
$data = $email . "\n";
$ret = file_put_contents('emailAddresses.txt', $data, FILE_APPEND | LOCK_EX);
if ( $ret !== FALSE ) {
header('Location: thanks.html');
exit;
}
}
}
}
?>
答案 0 :(得分:0)
您要做的是使用javascript或jQuery使用AJAX发送您的请求。
使用JavaScript:
<script type="text/javascript">
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 ) {
if(xmlhttp.status == 200){
//Request worked, Do something
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
else if(xmlhttp.status == 400) {
alert('There was an error 400')
}
else {
alert('something else other than 200 was returned')
}
}
}
xmlhttp.open("GET", "ajax_info.txt", true);
xmlhttp.send();
}
</script>
使用jQuery:
$.ajax({
url: "test.php",
context: document.body,
success: function(){
$(this).addClass("done");
}
});