0

好的,我在index.php上有一个电子邮件表单。我正在使用mail_check.php来验证两个字段是否都已填写。(还有更多正在验证的内容,但不包括在内,因为这不是问题

主要问题是,我想从mail_check.php将一条消息发送回index.php ,并在div id="mailrespond"中放置一条消息。我选择了 PHP 和 Javascript 来实现这一点。

索引.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 }

代码继续,但我的问题是我没有收到反馈消息。我对这一切有点陌生-如果您能提供帮助,将不胜感激!:)

4

2 回答 2

0

您在 msg 函数中重定向到 index.php,所以它什么也不做。

您应该通过纯 PHP 来完成,在 $_SESSION 中设置一个组件,通过 PHP 重定向到 index.php 并在 index.php 中检查该组件是否存在。

于 2013-09-02T11:25:20.367 回答
0

<---- @downvoter 请留下理由!谢谢

试试这个(不要调用表单字段名称,因为表单也可以有名称)

索引.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 } ?>
于 2013-09-02T11:31:45.127 回答