简体   繁体   English

用php提交表单后如何返回上一页?

[英]How to return to the previous page after a submit form with php?

I have a form in html whose "action" attribute calls a contact.php file. 我在html中有一个表单,其“action”属性调用contact.php文件。 The problem is that after I submit the form, the file that is visible is a blank page with the address contact.php and I want to see again the form of the main page. 问题是,在我提交表单后,可见的文件是一个空白页面,其地址为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>";

 ?>

Use either $_SERVER["HTTP_REFERER"] or use a hidden field on the form with the url of the current page: 使用$_SERVER["HTTP_REFERER"]或使用当前页面的url在表单上使用隐藏字段:

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

myAction.php 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 */
  }

And then even then you should have fallbacks in case for some reason the client isn't respecting HTTP redirects, like some redirect javascript, a meta redirect and a link to the destination. 然后,即便如此,如果由于某种原因客户端不尊重HTTP重定向,例如一些重定向javascript,元重定向和到目标的链接,你应该有回退。

Add a JS redirect after your alert then 然后在警报之后添加JS重定向

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

In your contact.php file, just use something like at the end of the PHP code: 在您的contact.php文件中,只需使用PHP代码末尾的内容:

header("location:yourfilenamehere.php");    

This will redirect back to whatever page you specify. 这将重定向回您指定的任何页面。

You could do two things... 你可以做两件事......

1) Have your php logic in the form file and submit to that file. 1)将您的php逻辑放在表单文件中并提交到该文件。 On the top of the file, put something like: 在文件的顶部,添加如下内容:

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

followed by your form. 然后是你的表格。

2) use the header() function to redirect to your form. 2)使用header()函数重定向到您的表单。

header("Location: form.html"); 

I feel pretty bad about posting this answer as it is just concatenating two other SO answers. 我觉得发布这个答案非常糟糕,因为它只是连接了另外两个SO答案。

First part 第一部分

Charlie74's answer on this page 查理74在这个页面上的答案

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

Second part check for the alertmsg name in the query string 第二部分检查查询字符串中的alertmsg名称

See SO post for getParameterByName: 请参阅获取getParameterByName的SO帖子:

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, " "));
}

in the page you are redirecting call the getParameterByName function: 在您重定向的页面中调用getParameterByName函数:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM