好的,我在 index.php 上有一个电子邮件表单。我正在使用 mail_check.php 来验证这两个字段是否已填写。 (还有更多内容正在验证,但不包括在内,因为它不是问题)
主要问题是来自 mail_check.php 我希望将其发送回 index.php ,并在 div id中添加一条消息= “mailrespond”即可。我选择了PHP和Javascript来实现这一目标。
的index.php
<div id="mailrespond"></div>
<form method="post" action="mail_check.php">
<span>Name: <input type="text" name="name" maxlength="30" /></span><br />
<span>Email: <input type="text" name="email" maxlength="30" /></span><br />
<input type="submit" value="Sign Up" name="registration">
</form>
</div>
mail_check.php
if(isset($_POST['registration']))
$name = $_POST['name'];
$email = $_POST['email'];
if(empty($email) || empty($name)){
// if email and name are empty
//header ("location: index.php");
?>
<script language="javascript" type="text/javascript">
window.location.replace("index.php");
function msg() {
// creating elements are a safer method then innerHTML
dv = document.createElement('div'); // creates a Div element
dv.setAttribute('class', 'error_msg'); // adds error styling
txt = document.createTextNode('please enter your name and email '); // create the message
dv.appendChild(txt); // place the text node on the element
document.getElementById("mailrespond").appendChild(dv);
}
window.onload = msg;
</script>
<?php }
代码继续,但我的问题是我没有收到反馈消息。我对这一切都有点新意 - 如果你能提供帮助,我将不胜感激! :)
答案 0 :(得分:0)
你正在重定向到msg函数中的index.php,所以它什么也没做。
你应该用纯PHP来做,在$ _SESSION中设置一个组件,用PHP重定向到index.php,在index.php中检查组件是否存在。
答案 1 :(得分:0)
&lt; ---- @downvoter请留下理由!感谢
试试这个(并且不要调用表单字段 name ,因为表单也可以有名称)
<强>的index.php 强>
<!DOCTYPE html>
<html>
<head>
<title>Mail respond</title>
<script>
window.onload=function() {
document.getElementById("mailform").onsubmit=function(){
if (this.fullname.value=="" || this.email.value=="") {
alert("Please fill in name and email");
return false;
}
return true; // allow form submission
}
}
</script>
</head>
<body>
<div id="mailrespond"></div>
<form method="post" action="mail_check.php" id="mailform">
<span>Name: <input type="text" name="fullname" maxlength="30" /></span><br />
<span>Email: <input type="text" name="email" maxlength="30" /></span><br />
<input type="submit" value="Sign Up" name="registration">
</form>
</div>
</body>
</html>
<强> mail_check.php 强>
<?PHP
if(isset($_POST['registration']))
$name = $_POST['fullname'];
$email = $_POST['email'];
if(empty($email) || empty($name)){
// if email and name are empty - only seen if JavaScript is turned off
?>
<h3>You did not fill in name and email</h3>
<p>please wait to be redirected or click <a href='index.php'>here</a></p>;
<meta http-equiv="refresh" content="3; url=http://example.com/index.php" />
<script type="text/javascript">
window.onload=function() {
setTimeout(function() {
window.location.replace("index.php");
},3000);
}
</script>
<?php } ?>