提交表单后重定向到index.html - 如何显示消息

时间:2015-06-26 11:48:17

标签: javascript php html

在我的网站表单联系人上,当我按下提交按钮时,网站会打开一个空白页面(php文件),为了删除它,我添加了

header("Location: ../index.html");

到我的php文件,它工作,它提交我的数据后它保持相同的页面,但它瞬间转到index.html,甚至有代码显示消息没有显示成功或失败的消息。< / p>

不确定为什么,因为这个方法,我看到很多话题都在谈论使用AJAX,但我发现这让我感到困惑,是否在我的网站上必须使用? 有人可以帮助我让它进入我的网站吗?

免费主机上的网站:

tential.co.nf

HTML:

    <!DOCTYPE html>
<html>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="js/fixedbar.js"></script>
    <script src="js/slider.js"></script>
    <meta charset="utf-8">
    <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300" rel="stylesheet" type="text/css">
    <link href="styles.css" rel="stylesheet" type="text/css">
    <title> Layout </title>
  </head>
  <body>


    <div class="header" id="top">
      <img class="logo" src="img/logo.png">
      <div class="menu">
        <a href="#" class="current">Home</a>
        <a href="#tour">Product Tour</a>
        <a href="#">Pricing</a>
        <a href="#">Try</a>
        <a href="#vision">Vision</a>
      </div>
      <div class="move">
        <div class="center">
          <h1>Move work forward!</h1>
          <p>Optential keeps your team organized, connected, and focused on results.</p>
        </div>
      </div>
      <div class="mail1">
        <form action="form/form.php" method="post">
          <h1>Try Now!</h1>
          <input name="email" class="email" type="text" placeholder="Enter your email address ...">
          <input type="submit" value="Get started for free">
        </form>
      </div>
    </div>

    <div class="mail2">
        <form action="form/form.php" method="post">
        <h1>Try Now!</h1>
        <input type="text" placeholder="Your Email here...">
        <input type="submit" id ="btn" value="Get started for free">
        <a class="top" href="#top">Top</a>
      </form>
    </div>


    <div id="slider">
        <div class="images">
          <div class="controls">
          <img src="img/3.png" alt="Image-1" />
          <img src="img/2.png" alt="Image-2" />
          <img src="img/1.png" alt="Image-3" />
          <img src="img/4.png" alt="Image-4" />
        </div> 
      </div>
    </div>

    <div class="barra2"></div>

    <div class="mobile">
      <div id="pc"> 

      </div>

      <div id="pctexto">

      </div>
    </div>

    <div class="contact">
      <div class="textocon">
        <div>
          <h1>Optential</h1>
          <p>A new management system<br>for a new management paradigm!</p>
        </div>
      </div>
      <form method="POST" action="form/contactengine.php">
        <div class="col1">
          <h1>Contact us!</h1>
          <input type="text" name="Name" size="50" placeholder="Name"/>
          <input type="text" name="Email" size="50" placeholder="Email"/>
          <input type="text" name="Subject" size="50" placeholder="Subject"/>
        </div>
        <div class="col2">
          <textarea name="Message" rows="5" cols="70" placeholder="Message..."></textarea>
          <input type="submit" id="btn"value="Send email"/>
        </div>
      </form>
      <div class="info">
        <div>
          <h1>Mail Us !</h1>
          <p>Rua Andrade Corvo, 242</p>
          <p>sala 206</p>
          <p>4700-204 Braga</p>
          <p>Portugal</p>
        </div>
        <div>
          <h1>Call Us !</h1>
          <a href="#"><p>+351 987654323</p></a>
          <a href="#"><p>+351 987654323</p></a>
          <a href="#"><p>+351 987654323</p></a>
        </div>
        <div>
          <h1>Email Us! </h1>
          <a href="#"><p>code@angel.com</p></a>
          <a href="#"><p>code_hr@angel.com</p></a>
          <a href="#"><p>code_support@angel.com</p></a>
        </div>
        <div>
          <h1>Join Us! </h1>
          <a href="#"><img src="img/facebook.png"></a>
          <a href="#"><img src="img/gplus.png"></a>
          <a href="#"><img src="img/twitter.png"></a>
          <a href="#"><img src="img/instag.png"></a>
        </div>
      </div>
    </div>
<script src="js/slider.js"></script>
  </body>
</html>

PHP:

    <?php

$EmailFrom = "no-reply@site.com";
$EmailTo = "duarte.andrade@team.androidpt.com";
$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Subject = Trim(stripslashes($_POST['Subject'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $Subject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  echo "<script type='text/javascript'>alert('Submitted successfully! Thanks for contacting Us')</script>";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
    header("Location: ../index.html");
?>

2 个答案:

答案 0 :(得分:1)

您可以使用元刷新标记。

让您的php显示包含您的成功消息的页面,并且还包括:

<meta http-equiv="refresh" content="5; url=http://example.com/">

内容中的“5”=秒。设置此值以显示成功消息的显示时间。另外,将url =设置为您要转到下一页的页面。

在您的具体情况下,请尝试修改您的代码:

if ($success){
    echo "<script type='text/javascript'>alert('Submitted successfully! Thanks for contacting Us')</script>";
    echo '<meta http-equiv="refresh" content="0; url=http://example.com/">';
}

警告框将保持刷新,直到警报框关闭为止。

答案 1 :(得分:0)

不是将表单发送到其他php文件进行处理,而是可以将代码保存在同一页面上,这样就可以提交表单,如果已经成功提交,它将保留在页面上并提示成功而不是将表单发送到某些PHP代码(存在于其他一些文件/页面中)并重定向回形成html页面。希望它清楚。下面是代码 -

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $EmailFrom = "no-reply@site.com";
    $EmailTo = "duarte.andrade@team.androidpt.com";
    $Name = Trim(stripslashes($_POST['Name'])); 
    $Email = Trim(stripslashes($_POST['Email'])); 
    $Subject = Trim(stripslashes($_POST['Subject'])); 
    $Message = Trim(stripslashes($_POST['Message'])); 

    // validation
    $validationOK=true;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
      exit;
    }

    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "\n";
    $Body .= "Subject: ";
    $Body .= $Subject;
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= $Message;
    $Body .= "\n";

    // send email 
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

    // redirect to success page 
    if ($success){
      echo "<script type='text/javascript'>alert('Submitted successfully! Thanks for contacting Us')</script>";
    }
    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    }
}
?><!DOCTYPE html>
<html>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="js/fixedbar.js"></script>
    <script src="js/slider.js"></script>
    <meta charset="utf-8">
    <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300" rel="stylesheet" type="text/css">
    <link href="styles.css" rel="stylesheet" type="text/css">
    <title> Layout </title>
  </head>
  <body>


    <div class="header" id="top">
      <img class="logo" src="img/logo.png">
      <div class="menu">
        <a href="#" class="current">Home</a>
        <a href="#tour">Product Tour</a>
        <a href="#">Pricing</a>
        <a href="#">Try</a>
        <a href="#vision">Vision</a>
      </div>
      <div class="move">
        <div class="center">
          <h1>Move work forward!</h1>
          <p>Optential keeps your team organized, connected, and focused on results.</p>
        </div>
      </div>
      <div class="mail1">
        <form action="form/form.php" method="post">
          <h1>Try Now!</h1>
          <input name="email" class="email" type="text" placeholder="Enter your email address ...">
          <input type="submit" value="Get started for free">
        </form>
      </div>
    </div>

    <div class="mail2">
        <form action="form/form.php" method="post">
        <h1>Try Now!</h1>
        <input type="text" placeholder="Your Email here...">
        <input type="submit" id ="btn" value="Get started for free">
        <a class="top" href="#top">Top</a>
      </form>
    </div>


    <div id="slider">
        <div class="images">
          <div class="controls">
          <img src="img/3.png" alt="Image-1" />
          <img src="img/2.png" alt="Image-2" />
          <img src="img/1.png" alt="Image-3" />
          <img src="img/4.png" alt="Image-4" />
        </div> 
      </div>
    </div>

    <div class="barra2"></div>

    <div class="mobile">
      <div id="pc"> 

      </div>

      <div id="pctexto">

      </div>
    </div>

    <div class="contact">
      <div class="textocon">
        <div>
          <h1>Optential</h1>
          <p>A new management system<br>for a new management paradigm!</p>
        </div>
      </div>
      <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <div class="col1">
          <h1>Contact us!</h1>
          <input type="text" name="Name" size="50" placeholder="Name"/>
          <input type="text" name="Email" size="50" placeholder="Email"/>
          <input type="text" name="Subject" size="50" placeholder="Subject"/>
        </div>
        <div class="col2">
          <textarea name="Message" rows="5" cols="70" placeholder="Message..."></textarea>
          <input type="submit" id="btn"value="Send email"/>
        </div>
      </form>
      <div class="info">
        <div>
          <h1>Mail Us !</h1>
          <p>Rua Andrade Corvo, 242</p>
          <p>sala 206</p>
          <p>4700-204 Braga</p>
          <p>Portugal</p>
        </div>
        <div>
          <h1>Call Us !</h1>
          <a href="#"><p>+351 987654323</p></a>
          <a href="#"><p>+351 987654323</p></a>
          <a href="#"><p>+351 987654323</p></a>
        </div>
        <div>
          <h1>Email Us! </h1>
          <a href="#"><p>code@angel.com</p></a>
          <a href="#"><p>code_hr@angel.com</p></a>
          <a href="#"><p>code_support@angel.com</p></a>
        </div>
        <div>
          <h1>Join Us! </h1>
          <a href="#"><img src="img/facebook.png"></a>
          <a href="#"><img src="img/gplus.png"></a>
          <a href="#"><img src="img/twitter.png"></a>
          <a href="#"><img src="img/instag.png"></a>
        </div>
      </div>
    </div>
<script src="js/slider.js"></script>
  </body>
</html>