【问题标题】:How to return to the previous page after a submit form with php?php提交表单后如何返回上一页?
【发布时间】:2014-05-09 20:07:55
【问题描述】:

我有一个 html 表单,它的“action”属性调用了一个contact.php 文件。问题是我提交表单后,可见的文件是一个空白页面,地址为contact.php,我想再次看到主页的表单。

HTML:
<form id="myForm" action="php-contact/contact.php"
                       method="post" class="contact_form" autocomplete="off"
                       role="form">
                          <div class="form-group">
                            <label for="input-subject">subject</label>
                            <input name="subject" type="text" class="form-control" id="subject" placeholder="your subject" maxlength="20"/>
                          </div>

                          <div class="form-group">    
                              <label for="input-name">name</label>
                              <input name="name"  type="text" class="form-control" id="name" placeholder="your name" maxlength="20"/>
                          </div>

                          <div class="form-group">    
                            <label for="input-email">email address</label>
                            <input name="email"  type="text" class="form-control" id="email" placeholder="your email" maxlength="40"/>
                          </div> 

                          <div class="form-group" >   
                              <label for="input-message">message</label>
                              <textarea name="message" cols="10" rows="10"  class="form-control" id="comment" ></textarea>
                          </div>  

                          <button name="myFormSubmitted" type="submit" class="btn btn-primary btn-lg btn-block">send</button>
                      </form>

PHP:
  <?php
  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $message = $_POST['message'];

  $to = "pep.g@gmail.com";
  $message = '
  name: '.$name.'
  email: '.$email.'
   message: '.$message.'
  ';

 $headers = 'From: pep.website@website.com';

   if (
    mail($to, $subject, $message, $headers)
) 
      echo"<script>alert('message send succesfully')</script>";
   else
      echo"<script>alert('message not send')</script>";

 ?>

【问题讨论】:

  • 一个词:AJAX。

标签: javascript php html forms


【解决方案1】:

然后在警报之后添加一个 JS 重定向

echo "<script>
             alert('message sent succesfully'); 
             window.history.go(-1);
     </script>";

【讨论】:

  • 我尝试了你的解决方案,页面返回:alert('message sentsuccesfully'); window.history.go(-1); "; 否则回声""; ?>
  • 那么你的 PHP 代码在某个地方出现了问题。寻找合适的打开和关闭标签
  • 它的工作。我将脚本放在 php 代码之外....但是警报出现在空白页面上,并且仅在加载表单后出现。我想通过表单加载显示警报(如果表单已正确发送)
【解决方案2】:

在您的contact.php 文件中,只需使用PHP 代码末尾的类似内容:

header("location:yourfilenamehere.php");    

这将重定向回您指定的任何页面。

【讨论】:

  • 我在 php 代码和工作之后添加了这段代码: ...但返回页面顶部并且不要形成
  • 抱歉,不确定您要问什么?您的意思是将该评论放在另一个答案上吗?这与我提供的解决方案无关。
  • 没有。我希望当返回 index.html 页面时,它专门针对该表单所在的部分。
【解决方案3】:

你可以做两件事......

1) 在表单文件中包含您的 php 逻辑并提交到该文件。在文件的顶部,输入如下内容:

if(isset($_POST['name'])) {
// your sending logic
}

接着是你的表格。

2) 使用 header() 函数重定向到您的表单。

header("Location: form.html"); 

【讨论】:

  • 这在这里不起作用,因为它们已经以 JavaScript 警报的形式发送输出
  • @Hanky웃Panky 这是真的。虽然不会是我的第一选择。也许 header("位置:form.php?send=success");并检查 $_GET['send'] 以在表单上方显示成功消息
【解决方案4】:

我对发布这个答案感到非常难过,因为它只是连接了另外两个 SO 答案。

第一部分

Charlie74 在此页面上的回答

// add the message you want to show to the user
header("Location: yourfilenamehere.php?alertmsg=message+send+succesfully"); 
exit();

第二部分检查查询字符串中的 alertmsg 名称

See SO post for getParameterByName:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

在您要重定向的页面中调用getParameterByName 函数:

<script>
var msg = getParameterByName('alertmsg');

if (alertmsg.length > 0) {
    alert(msg);
}
</script>

【讨论】:

    【解决方案5】:

    使用$_SERVER["HTTP_REFERER"] 或在表单上使用带有当前页面网址的隐藏字段:

    <form action="myAction.php">
        <input type="hidden" name="destination" value="<?php echo $_SERVER["REQUEST_URI"]; ?>"/>
        <!-- other form inputs -->
        <input type="submit"/>
    </form>
    

    myAction.php

    <?php
      /* Do work */
      if(isset($_REQUEST["destination"])){
          header("Location: {$_REQUEST["destination"]}");
      }else if(isset($_SERVER["HTTP_REFERER"])){
          header("Location: {$_SERVER["HTTP_REFERER"]}");
      }else{
           /* some fallback, maybe redirect to index.php */
      }
    

    即使那样,您也应该有备用方案,以防客户端出于某种原因不遵守 HTTP 重定向,例如某些重定向 javascript、元重定向和指向目标的链接。

    【讨论】:

    • 在向数据库提交数据或将变量附加到 url 时,这是一种更好、更可靠的方法。您不希望浏览器只返回一个页面(这可能最终可能会多次提交表单或显示错误),您希望它物理地转到 url,有效地使用新数据重新加载页面,使用将附加变量附加到 url 的选项(如成功/错误消息)。
    猜你喜欢
    • 2014-05-26
    • 2017-08-28
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多